【问题标题】:Capture mouse movement event only when Left Click is pressed仅在按下左键时捕获鼠标移动事件
【发布时间】:2021-08-01 07:58:01
【问题描述】:

只有当鼠标在鼠标左键按下时鼠标移到控件上时,我才需要更新控件。我通常会简单地检查 e.Button 属性,但它在 MouseEnter 事件中不可用。

void MyControl_MouseEnter(object sender, EventArgs e)
    {
        // MouseEventArgs needed to check this

        // if (e.Button == MouseButtons.Left)
        // {
        //     update MyControl
        // }
    }

你将如何做到这一点?

【问题讨论】:

  • 在 MouseDown 和 MouseUp 中按下/释放鼠标按钮时,您能否解除技术,然后设置/清除上面代码中检查的标志?

标签: c# .net winforms events


【解决方案1】:

使用静态 Control.MouseButtons 属性。例如:

    private void panel1_MouseEnter(object sender, EventArgs e) {
        if (Control.MouseButtons == MouseButtons.Left) {
            // etc...
        }
    }

非常很难开始,无论用户点击什么来按下鼠标按钮都会捕获鼠标,从而阻止控件的 MouseEnter 事件触发。它也是用户完全无法发现的 UI。请考虑一个更好的捕鼠器。

【讨论】:

    【解决方案2】:

    这是一种(粗略的)方法。当您将鼠标拖到 button1 上时,它会将表单的标题文本更改为按下的任何鼠标按钮。您可以参考Control.MouseButtons 来查看哪个按钮处于按下状态。这里有更多关于MSDN的信息。

     public partial class Form1 : Form
        {
            MouseButtons _buttons;
    
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (_buttons != System.Windows.Forms.MouseButtons.None)
                {
                    this.Text = _buttons.ToString();
                }
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                _buttons = Control.MouseButtons;
            }
        }
    

    【讨论】:

    • +1 但是您在 Form 或 UserControl 级别使用 MouseMove 而不是 MouseDown 和 MouseUp 事件有什么原因吗?
    【解决方案3】:

    我在 SO 上的另一个问题中找到了答案:

    How can I detect a held down mouse button over a PictureBox?

    您将需要使用消息过滤器。实现IMessageFilter接口的PreFilterMessage,并使用Application.AddMessageFilter分配实例。

    您必须自己解释 Windows 消息...这并不难,但需要一些工作。

    实现可能如下所示:

            if (m.Msg == 0x200)
            {
                int x, y;
                x = m.LParam.ToInt32() & 0xFFFF;
                y = m.LParam.ToInt32() >> 16;
                if ((m.WParam.ToInt32() & 2) != 0)
                {
                    // here, the left mouse button is pressed, and you can use the coords
                    // and see if the mouse is over the control you want.
                }
            }
    

    【讨论】:

      【解决方案4】:

      我今天刚刚实现了类似的东西,仅在 Chrome 中进行了测试,但效果很好。基本概念是你只在 mousedown 和 mouseup 之间捕获 mousemove,如下:

      var image = document.getElementById('my_image');
      image.addEventListener('mousedown', function(e) {
          e.currentTarget.addEventListener('mousemove', doMyStuff);
      });
      image.addEventListener('mouseup', function(e) {
          e.currentTarget.removeEventListener('mousemove', doMyStuff);
      });
      
      function doMyStuff(e) {
          // do what you want, the mouse is moving while the user is holding the button down
      }
      

      【讨论】:

      • 您可以轻松地将 e.currentTarget.addEventListener 替换为 image.addEventListener,但我不确定它是否重要;不过,似乎从方便的参数中提取可能比使用闭包更有效。
      【解决方案5】:
         Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles 
              Button1.MouseDown
              If Control.MouseButtons = MouseButtons.Left Then
                  Label1.Text = "Left"
              ElseIf Control.MouseButtons = MouseButtons.Right Then
                  Label1.Text = "Right"
              ElseIf Control.MouseButtons = MouseButtons.Middle Then
                  Label1.Text = "Middle"
              Else
                  Label1.Text = "lelse"
              End If
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-12
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多