我有一个Windows Form (在C#.NET中工作)。
表单顶部有几个面板,底部有一些ComboBoxes和DataGridViews。
我想使用顶部面板上的滚动事件,但是如果选择一个ComboBox,焦点就会丢失。这些面板包含各种其他控件。
当鼠标位于任何面板上时,我如何总是收到鼠标滚轮事件?到目前为止,我尝试使用MouseEnter / MouseEnter事件,但没有成功。
发布于 2011-01-23 03:11:36
您所描述的听起来像是您想要复制Microsoft Outlook的功能,在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);
}
}请注意,此代码对应用程序中的所有窗体都是活动的,而不仅仅是主窗体。
发布于 2011-01-23 03:00:10
每个控件都有一个鼠标滚轮事件,该事件在控件具有焦点的情况下移动鼠标滚轮时发生。
有关更多信息,请查看此处:Control.MouseWheel Event
https://stackoverflow.com/questions/4769854
复制相似问题