Click here to Skip to main content
15,885,914 members
Home / Discussions / C#
   

C#

 
GeneralRe: Target Framework in VS Pin
Bill 10016-Aug-15 8:54
Bill 10016-Aug-15 8:54 
AnswerRe: Target Framework in VS Pin
Kevin Marois6-Aug-15 9:35
professionalKevin Marois6-Aug-15 9:35 
GeneralRe: Target Framework in VS Pin
Bill 100113-Aug-15 11:57
Bill 100113-Aug-15 11:57 
GeneralRe: Target Framework in VS Pin
Kevin Marois13-Aug-15 12:00
professionalKevin Marois13-Aug-15 12:00 
GeneralRe: Target Framework in VS Pin
Bill 100113-Aug-15 12:06
Bill 100113-Aug-15 12:06 
GeneralRe: Target Framework in VS Pin
Kevin Marois13-Aug-15 12:07
professionalKevin Marois13-Aug-15 12:07 
QuestionDragging A Control - Limits Pin
Kevin Marois6-Aug-15 7:24
professionalKevin Marois6-Aug-15 7:24 
AnswerRe: Dragging A Control - Limits Pin
Alan N7-Aug-15 8:07
Alan N7-Aug-15 8:07 
I'm less at sub-novice level with WPF but I would imagine that the general principles for solving this problem will much the same as with System.Windows.Forms. Rather than attempting to calculate if an arbitrary new position is valid we can simply limit the mouse movement so that all achievable positions become ok.

1) On mouse button down calculate and set a constraining rectangle for the cursor
2) In mouse move relocate the label based on an offset from the cursor position
3) On losing mouse capture remove the constraining reactangle

Here's one I prepared earlier. It should compile and run:
C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MouseMoveLabelDemo {
  public sealed class DemoForm : Form {
    [STAThread]
    private static void Main() {
      Application.Run(new DemoForm());
    }

    private readonly Point home;
    private Label itinerantLabel;
    private Size offset;

    public DemoForm() {
      home = new Point(10, 10);
      BackColor = Color.White;

      itinerantLabel = new Label();
      itinerantLabel.Text = "Move Me";
      itinerantLabel.BackColor = Color.Pink;
      itinerantLabel.BorderStyle = BorderStyle.FixedSingle;
      itinerantLabel.Location = home;
      itinerantLabel.Parent = this;
      itinerantLabel.MouseDown += Label_MouseDown;
      itinerantLabel.MouseMove += Label_MouseMove;
      itinerantLabel.MouseCaptureChanged += Label_CaptureLost;

      SizeChanged += DemoForm_SizeChanged;
    }

    private void DemoForm_SizeChanged(object sender, EventArgs e) {
      // In this simple demo we don't want to lose the label 
      // so just send it home whenever the form is resized
      itinerantLabel.Location = home;
    }

    private void Label_MouseDown(object sender, MouseEventArgs e) {
      Label lbl = (Label)sender;
      if (e.Button == MouseButtons.Left) {
        // Mouse is automatically captured on mouse down

        // All manipulations in Screen coordinates
        Point cursorPosition = Cursor.Position;
        Point labelPosition = PointToScreen(lbl.Location);

        // calculate offset of cursor from label origin
        offset = new Size(cursorPosition.X - labelPosition.X, cursorPosition.Y - labelPosition.Y);

        // Constrain cursor within a part of the ClientRectangle
        // (x1, y1) - (x2, y2)
        Rectangle cursorClippingRectangle = Rectangle.FromLTRB(
          // x1
          offset.Width,
          // y1
          offset.Height,
          // x2
          ClientRectangle.Width - (lbl.Width - offset.Width) + 1,
          // y2
          ClientRectangle.Height - (lbl.Height - offset.Height) + 1);
        Cursor.Clip = RectangleToScreen(cursorClippingRectangle);
      }
    }

    private void Label_MouseMove(object sender, MouseEventArgs e) {
      Label lbl = (Label)sender;
      if (lbl.Capture) {
        lbl.Location = PointToClient(Cursor.Position - offset);
      }
    }

    private void Label_CaptureLost(object sender, EventArgs e) {
      Cursor.Clip = Rectangle.Empty;
    }
  }
}

Alan.
QuestionInterop.COMSVCSLib.DLL - Application Error Pin
cpremesh6-Aug-15 2:58
cpremesh6-Aug-15 2:58 
QuestionExecute keyboard shortcuts through code Pin
Member 108502535-Aug-15 15:18
Member 108502535-Aug-15 15:18 
AnswerRe: Execute keyboard shortcuts through code Pin
OriginalGriff5-Aug-15 22:33
mveOriginalGriff5-Aug-15 22:33 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 1:40
Member 108502536-Aug-15 1:40 
GeneralRe: Execute keyboard shortcuts through code Pin
OriginalGriff6-Aug-15 1:43
mveOriginalGriff6-Aug-15 1:43 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 1:48
Member 108502536-Aug-15 1:48 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 1:59
Member 108502536-Aug-15 1:59 
GeneralRe: Execute keyboard shortcuts through code Pin
OriginalGriff6-Aug-15 2:11
mveOriginalGriff6-Aug-15 2:11 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 7:37
Member 108502536-Aug-15 7:37 
GeneralRe: Execute keyboard shortcuts through code Pin
OriginalGriff6-Aug-15 7:59
mveOriginalGriff6-Aug-15 7:59 
QuestionInterview question - adding using A RecursiveAlgorithm Pin
anthonyjames5-Aug-15 12:18
anthonyjames5-Aug-15 12:18 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
PIEBALDconsult5-Aug-15 13:15
mvePIEBALDconsult5-Aug-15 13:15 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
anthonyjames5-Aug-15 13:35
anthonyjames5-Aug-15 13:35 
AnswerRe: Interview question - adding using A RecursiveAlgorithm Pin
Dave Kreskowiak5-Aug-15 13:37
mveDave Kreskowiak5-Aug-15 13:37 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
anthonyjames5-Aug-15 13:43
anthonyjames5-Aug-15 13:43 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
Dave Kreskowiak5-Aug-15 13:56
mveDave Kreskowiak5-Aug-15 13:56 
QuestionIm curious of answers Pin
Jesse Glover5-Aug-15 9:30
Jesse Glover5-Aug-15 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.