简体   繁体   English

我如何单击控件并将其拖出(而不是拖放),但只要单击该按钮,仍然可以跟踪事件?

[英]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?

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. 我想允许用户在我的UserControl中单击并向左/向右拖动以放大/缩小,但是我希望拖动不限于实际控件的边界。 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. 在此先感谢您的帮助或建议。

Set the Capture property to true in a MouseDown event handler. 在MouseDown事件处理程序中将Capture属性设置为true。 You'll keep getting MouseMove messages, even if the mouse has left the client area. 即使鼠标离开了客户区,您也将继续收到MouseMove消息。

  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);
    }
  }

我认为您是追捕Mouse.Capture或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM