views:

31

answers:

2

Hello,

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.

A: 

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

Douglas
+1  A: 

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);
    }
  }
Hans Passant
Brilliant. Thank you nobugz and thank you Douglas.
Jelly Amma