How could I click in a control and drag (not Drag and Drop) out of it but still follow the event as long as the button is clicked?

StackOverflow https://stackoverflow.com/questions/2445996

Question

I would like to allow the user to click within my UserControl and drag left/right to zoom in/out but I'd like the dragging not to be restricted to the actual control's boundaries. What sort of event or strategy would be the right way to track the mouse position outside the control and the form until the mouse click is released ?

Thanks in advance for any help or advice.

Was it helpful?

Solution

Set the Capture property to true in a MouseDown event handler. You'll keep getting MouseMove messages, even if the mouse has left the client area.

  public partial class UserControl1 : UserControl {
    public UserControl1() {
      InitializeComponent();
    }
    protected override void OnMouseDown(MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) this.Capture = true;
      base.OnMouseDown(e);
    }
    protected override void OnMouseMove(MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        // Your dragging logic here...
        Console.WriteLine(e.Location);
      }
      base.OnMouseMove(e);
    }
  }

OTHER TIPS

I think that you are after Mouse.Capture, or something similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top