.net – Windows窗体:捕获MouseWheel
发布时间:2020-12-14 01:40:54 所属栏目:Windows 来源:网络整理
导读:我有一个 Windows窗体(在C#.NET中工作). 表单顶部有几个面板,底部有一些ComboBoxes和DataGridViews. 我想在顶部面板上使用滚动事件,但是如果选择例如ComboBox焦点丢失了.面板包含各种其他控件. 当鼠标悬停在任何面板上时,我怎么能总是收到鼠标滚轮事件? 到
我有一个
Windows窗体(在C#.NET中工作).
表单顶部有几个面板,底部有一些ComboBoxes和DataGridViews. 我想在顶部面板上使用滚动事件,但是如果选择例如ComboBox焦点丢失了.面板包含各种其他控件. 当鼠标悬停在任何面板上时,我怎么能总是收到鼠标滚轮事件?
您所描述的内容听起来像是要复制Microsoft Outlook的功能,在这种情况下,您无需实际单击以聚焦控件以在其上使用鼠标滚轮.
这是一个需要解决的相对高级的问题:它涉及实现包含表单的IMessageFilter接口,查找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); } } 请注意,此代码对应用程序中的所有表单都有效,而不仅仅是主表单. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows-server-2012-r2 – Server 2012 R2中NIC组合的最佳
- 基于windows10 平台部署Kubernetes的demo程序Guestbook
- windows-xp – 在Windows XP中自动安装软件
- win10专业版激活方法——亲测可行!!!
- 在windows cmd的子目录中运行命名.exe
- windows-server-2008 – 新颖的eDirectory和Windows XP / S
- qt – 如果最大化,QWidget :: save / restoreGeometry会丢失
- windows下编译 libjpeg以及libjpeg-turbo
- windows – phpstorm git pull –rebase
- 使用P / Invoke时如何将Win32类型映射到C#类型?