我有一个 Windows 窗体(在 C#.NET 中工作)。
该表单顶部有几个面板,底部有一些 ComboBoxes 和 DataGridViews。
我想在顶部面板上使用滚动事件,但是如果选择一个例如 ComboBox 焦点就会丢失。面板包含各种其他控件。
当鼠标悬停在任何面板上时,我怎么能总是收到鼠标滚轮事件?到目前为止,我尝试使用 MouseEnter / MouseEnter 事件,但没有运气。
我有一个 Windows 窗体(在 C#.NET 中工作)。
该表单顶部有几个面板,底部有一些 ComboBoxes 和 DataGridViews。
我想在顶部面板上使用滚动事件,但是如果选择一个例如 ComboBox 焦点就会丢失。面板包含各种其他控件。
当鼠标悬停在任何面板上时,我怎么能总是收到鼠标滚轮事件?到目前为止,我尝试使用 MouseEnter / MouseEnter 事件,但没有运气。
您所描述的内容听起来像是您想要复制例如 Microsoft Outlook 的功能,您无需实际单击即可将控件聚焦以使用鼠标滚轮。
这是一个需要解决的相对高级的问题:它涉及实现IMessageFilter
包含表单的接口、查找WM_MOUSEWHEEL
事件并将它们引导到鼠标悬停的控件。
这是一个示例(来自此处):
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 事件