Click here to Skip to main content
15,499,571 members
Home / Discussions / C#
   

C#

 
GeneralRe: Target Framework in VS Pin
Bill 10016-Aug-15 9:54
MemberBill 10016-Aug-15 9:54 
AnswerRe: Target Framework in VS Pin
Kevin Marois6-Aug-15 10:35
professionalKevin Marois6-Aug-15 10:35 
GeneralRe: Target Framework in VS Pin
Bill 100113-Aug-15 12:57
MemberBill 100113-Aug-15 12:57 
GeneralRe: Target Framework in VS Pin
Kevin Marois13-Aug-15 13:00
professionalKevin Marois13-Aug-15 13:00 
GeneralRe: Target Framework in VS Pin
Bill 100113-Aug-15 13:06
MemberBill 100113-Aug-15 13:06 
GeneralRe: Target Framework in VS Pin
Kevin Marois13-Aug-15 13:07
professionalKevin Marois13-Aug-15 13:07 
QuestionDragging A Control - Limits Pin
Kevin Marois6-Aug-15 8:24
professionalKevin Marois6-Aug-15 8:24 
AnswerRe: Dragging A Control - Limits Pin
Alan N7-Aug-15 9:07
MemberAlan N7-Aug-15 9: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 3:58
Membercpremesh6-Aug-15 3:58 
QuestionExecute keyboard shortcuts through code Pin
Member 108502535-Aug-15 16:18
MemberMember 108502535-Aug-15 16:18 
AnswerRe: Execute keyboard shortcuts through code Pin
OriginalGriff5-Aug-15 23:33
mveOriginalGriff5-Aug-15 23:33 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 2:40
MemberMember 108502536-Aug-15 2:40 
GeneralRe: Execute keyboard shortcuts through code Pin
OriginalGriff6-Aug-15 2:43
mveOriginalGriff6-Aug-15 2:43 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 2:48
MemberMember 108502536-Aug-15 2:48 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 2:59
MemberMember 108502536-Aug-15 2:59 
GeneralRe: Execute keyboard shortcuts through code Pin
OriginalGriff6-Aug-15 3:11
mveOriginalGriff6-Aug-15 3:11 
GeneralRe: Execute keyboard shortcuts through code Pin
Member 108502536-Aug-15 8:37
MemberMember 108502536-Aug-15 8:37 
GeneralRe: Execute keyboard shortcuts through code Pin
OriginalGriff6-Aug-15 8:59
mveOriginalGriff6-Aug-15 8:59 
QuestionInterview question - adding using A RecursiveAlgorithm Pin
anthonyjames5-Aug-15 13:18
Memberanthonyjames5-Aug-15 13:18 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
PIEBALDconsult5-Aug-15 14:15
professionalPIEBALDconsult5-Aug-15 14:15 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
anthonyjames5-Aug-15 14:35
Memberanthonyjames5-Aug-15 14:35 
AnswerRe: Interview question - adding using A RecursiveAlgorithm Pin
Dave Kreskowiak5-Aug-15 14:37
mveDave Kreskowiak5-Aug-15 14:37 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
anthonyjames5-Aug-15 14:43
Memberanthonyjames5-Aug-15 14:43 
GeneralRe: Interview question - adding using A RecursiveAlgorithm Pin
Dave Kreskowiak5-Aug-15 14:56
mveDave Kreskowiak5-Aug-15 14:56 
QuestionIm curious of answers Pin
Jesse Glover5-Aug-15 10:30
MemberJesse Glover5-Aug-15 10: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.