鼠标滚轮事件(C#)

29
我无法在主窗体中获取鼠标滚轮事件。
作为演示,我想出了一个简单的例子:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);

        Form2 f2 = new Form2();
        f2.Show(this);
    }

    private void panel1_MouseWheel(object sender, MouseEventArgs e)
    {
        if(e.Delta != 0)
        Console.Out.WriteLine(e.Delta);
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        this.MouseMove += new MouseEventHandler(Form2_MouseMove);
        this.MouseWheel += new MouseEventHandler(Form2_MouseMove);
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Delta != 0)
            Console.Out.WriteLine(e.Delta);
    }
}

我在Form2中能够获取鼠标滚轮事件,但在Form1中却不行,有什么想法吗?

谢谢,

詹姆斯


2
鼠标滚轮的永恒挫败感在于微软决定将其视为键盘事件而非鼠标事件,因此鼠标滚轮消息会发送到具有键盘焦点的控件,迫使几乎每个使用鼠标滚轮的应用程序都要做一些变通处理。 - Qwertie
7个回答

44
我猜测OP希望在鼠标仅悬停在面板上时就能获取滚动事件,即使该面板没有焦点。
这种行为的实现方法在这里有详细说明:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/eb922ed2-1036-41ca-bd15-49daed7b637c/

并且在这里:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6bfb9287-986d-4c60-bbcc-23486e239384/

从链接的论坛中获取的代码片段之一:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1 {
  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m) {
      if (m.Msg == 0x20a) {
        // WM_MOUSEWHEEL, find the control at screen position m.LParam
        Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
        IntPtr hWnd = WindowFromPoint(pos);
        if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) {
          SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
          return true;
        }
      }
      return false;
    }

    // P/Invoke declarations
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(Point pt);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  }
}

这段代码基本上会拦截所有的wm_mousewheel事件,并将它们重定向到鼠标当前悬停的控件。面板不再需要获得焦点才能接收滚轮事件。

7
这应该被标记为答案。它适用于所有情况,并且可以轻松地进行调整(就像我为我的个人需求所做的一样)。 - Martin Bliss
1
"m.LParam.ToInt32() & 0xffff" 应改为 "(short)(ushort)(uint)(int)(long)m.LParam""m.LParam.ToInt32() >> 16" 应改为 "(short)(ushort)(((uint)(int)(long)m.LParam) >> 16)" - Bryce Wagner
1
也许其中一些强制转换是不必要的 :-) 但这样可以防止任何强制转换异常。(int)(long)将从64位IntPtr向下转换为int,(uint)将使其执行无符号移位(而不是有符号移位),(ushort)提取低16位,(short)将其转换为有符号值。 - Bryce Wagner
1
@glancep 抱歉,也许我没有清楚地表明异常发生的位置。它只会在64位模式下发生。如果您有一个64位的IntPtr,并且其值超出int.MinValue到int.MaxValue之外,IntPtr.ToInt32()将引发溢出异常。 - Bryce Wagner
2
然而,那是一个更好的构造函数。new Point((int)(long)m.LParam)明显比我上面发布的混乱代码要干净得多。 - Bryce Wagner
显示剩余4条评论

20
你的问题源于form1拥有焦点,而不是panel1。这意味着将触发form1的事件,而不是panel1的事件。
我通过对Form1构造函数进行以下更改来重新创建您的场景,并验证它是否触发了滚轮事件。
public Form1()
{
        InitializeComponent(); 

        /*  --- Old code that don't work ---
            this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
            this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
        */

        this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        this.MouseMove += new MouseEventHandler(panel1_MouseWheel);

        Form2 f2 = new Form2();
        f2.Show(this);
    }
}

13

添加另一个面板MouseEnter事件,并在其回调函数中获取输入焦点:

void MouseEnterEvent()
{
   this.Panel.Focus();
}

4
感谢 @nitrogenycs 的回答,我编写了一个简单的通用类来轻松解决这个问题:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

namespace MyNamespace
{
  public class MouseWheelManagedForm : Form, IMessageFilter
  {
    private bool managed;

    public MouseWheelManagedForm () : this (true) {
   }

    public MouseWheelManagedForm (bool start) {
      managed = false;
      if (start)
        ManagedMouseWheelStart();
    }

    protected override void Dispose (bool disposing) {
      if (disposing)
        ManagedMouseWheelStop();
      base.Dispose(disposing);
    }

    /************************************
     * IMessageFilter implementation
     * *********************************/
    private const int WM_MOUSEWHEEL = 0x20a;
    // P/Invoke declarations
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint (Point pt);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

    private bool IsChild (Control ctrl) {
      Control loopCtrl = ctrl;

      while (loopCtrl != null && loopCtrl != this)
        loopCtrl = loopCtrl.Parent;

      return (loopCtrl == this);
    }

    public bool PreFilterMessage (ref Message m) {
      if (m.Msg == WM_MOUSEWHEEL) {
        //Ensure the message was sent to a child of the current form
        if (IsChild(Control.FromHandle(m.HWnd))) {
          // Find the control at screen position m.LParam
          Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);

          //Ensure control under the mouse is valid and is not the target control
          //otherwise we'd be trap in a loop.
          IntPtr hWnd = WindowFromPoint(pos);
          if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) {
            SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
            return true;
          }
        }
      }
      return false;
    }

    /****************************************
     * MouseWheelManagedForm specific methods
     * **************************************/
    public void ManagedMouseWheelStart () {
      if (!managed) {
        managed = true;
        Application.AddMessageFilter(this);
      }
    }

    public void ManagedMouseWheelStop () {
      if (managed) {
        managed = false;
        Application.RemoveMessageFilter(this);
      }
    }

  }
}

接下来,您只需要将您的表单从这个类继承,而不是继承自Form,对于每个您需要“管理”鼠标滚轮的表单:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MyApp
{
  public partial class MyForm : MyNamespace.MouseWheelManagedForm
  {
    public MyForm ()
    {
      InitializeComponent();
    }

  }
}

希望这能帮助其他人(而不仅仅是我)。


1
你可以使用“Contains”代替“IsChild”。 - colin lamarre

0

面板本身不能获得焦点,只有放置在面板内部的项目才能获得焦点。只有当某个东西被放置在面板内并且该物体具有焦点时,面板才会接收到鼠标滚轮事件。简单地将鼠标悬停在面板上并移动鼠标滚轮将事件发送到表单而不是面板。

这就是您两个示例之间的区别。


不是真的。在我编写的应用程序中,我有一个面板作为我的图形显示空间。虽然您无法单击聚焦面板,但可以在代码中使用<code>myPanel.Focus()</code>命令,以便该面板从其他所有内容中夺取焦点,以使我的显示代码正常工作。 - Jerry
2
@Jerry - 我已经测试过了,我的测试结果显示我是正确的。如果面板上没有任何内容,即使调用myPanel.Focus()也不会改变鼠标滚轮事件的传递。只有当面板上的某个内容获得焦点时,我才能获取到鼠标滚轮事件。 - Sam Meldrum

0

或许这对你有用?

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        Form2 f2 = new Form2();
        f2.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        f2.MouseMove += new MouseEventHandler(panel1_MouseWheel);
        f2.Show(this);
    }

    private void panel1_MouseWheel(object sender, MouseEventArgs e)
    {
        if(e.Delta != 0) Console.Out.WriteLine(e.Delta);
    }
}

-2
this.MouseWheel += pictureBox1_MouseWheel; //tanımlama
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
            {
                if (Convert.ToString(e.Delta) == "120")
                {
                    //yukarı
                }
                else if (Convert.ToString(e.Delta) == "-120")
                {
                    //aşağı
                }
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接