鼠标滚轮事件(C#)

我无法在主窗体中获取鼠标滚轮事件。

作为演示,我想出了一个简单的例子:

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任何想法?

干杯,

詹姆士

我怀疑只要鼠标hover在面板上,即使面板没有焦点,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事件,并将它们重定向到鼠标当前hover的控件。 面板不再需要具有焦点来接收车轮事件。

你的问题来自于form1具有焦点,而不是panel1。 …哪个方面意味着将触发form1的事件,而不是panel1的事件。

我重新创建了你的场景,对Form1中的构造函数进行了以下更改,并validation它是否触发滚轮事件。

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

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

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

感谢@nitrogenycs的回答,我写了一个简单的generics类来轻松解决这个问题:

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

从那里,您只需要从此类inheritance您的Form而不是Form,因为您需要“管理”MouseWheel的每个表单:

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

希望这会帮助别人(比我)。

也许这对你有用吗?

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

面板本身不具有焦点,只有放置在面板内的项目才能具有焦点。 一旦放入其中的某些东西并且该东西具有焦点,该面板将仅接收MouseWheel事件。 只需在面板上方移动并移动鼠标滚轮,即可将事件发送到表单,而不是面板。

这是两个例子之间的区别。

我认为你有点误解了我的问题。 在我的主窗体上使用下面的代码我不会得到MouseWheel事件:

 public Form1() { InitializeComponent(); this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel); this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel); } 

但我确实得到了以下事件:

 public Form1() { InitializeComponent(); this.MouseWheel += new MouseEventHandler(panel1_MouseWheel); } 

如果没有Form2的混淆,我希望更清楚。 我只是试图在我的主窗体中的Panel上获取MouseWheel事件。

干杯,

詹姆士

 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ğı } }