我有一个Windows窗体(使用C#.NET)。Windows窗体:捕获MouseWheel
该窗体有几个面板顶端和一些ComboBoxes和DataGridViews底端。
我想使用顶部面板上的滚动事件,但是如果选择了例如组合框的焦点丢失了。面板包含各种其他控件。
当鼠标悬停在任何面板上时,我总能接收鼠标滚轮事件? 到目前为止,我尝试使用MouseEnter/MouseEnter事件,但没有运气。
我有一个Windows窗体(使用C#.NET)。Windows窗体:捕获MouseWheel
该窗体有几个面板顶端和一些ComboBoxes和DataGridViews底端。
我想使用顶部面板上的滚动事件,但是如果选择了例如组合框的焦点丢失了。面板包含各种其他控件。
当鼠标悬停在任何面板上时,我总能接收鼠标滚轮事件? 到目前为止,我尝试使用MouseEnter/MouseEnter事件,但没有运气。
像你想复制的功能,你描述的声音,例如Microsoft Outlook中,在这里你不需要实际点击集中控制使用鼠标轮子上。
这是一个相对要解决的高级问题:它涉及到实现包含窗体的界面,寻找WM_MOUSEWHEEL
事件并将它们引导到鼠标悬停的控件。
下面是一个例子(从here):
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());
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);
}
}
请注意,此代码是在您的应用程序的所有形式,而不仅仅是主要形式的活性。
每个控件都有一个鼠标滚轮事件,当鼠标滚轮移动而控件具有焦点时发生。
检查了这一点的详细信息:Control.MouseWheel Event
谢谢,看到它,但我不知道为什么处理WM_MOUSEWHEEL不是很理想。 – n0ter 2011-01-22 19:14:09