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

发布于 2024-08-25 07:33:01 字数 126 浏览 3 评论 0原文

我想允许用户在我的 UserControl 中单击并向左/向右拖动以放大/缩小,但我希望拖动不限于实际控件的边界。 什么样的事件或策略是跟踪控件和窗体之外的鼠标位置直到释放鼠标单击的正确方法?

预先感谢您的任何帮助或建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2024-09-01 07:33:01

在 MouseDown 事件处理程序中将 Capture 属性设置为 true。即使鼠标已离开客户区,您也会不断收到 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);
    }
  }

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);
    }
  }
隱形的亼 2024-09-01 07:33:01

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文