【发布时间】:2009-09-03 07:02:08
【问题描述】:
如何轻松捕捉表单中所有控件的“鼠标按下”事件,而无需手动订阅每个事件? (C#) 类似于“KeyPreview”功能,但用于鼠标事件。
【问题讨论】:
如何轻松捕捉表单中所有控件的“鼠标按下”事件,而无需手动订阅每个事件? (C#) 类似于“KeyPreview”功能,但用于鼠标事件。
【问题讨论】:
我发现这是最适合我的解决方案。
创建一个从IMessageFilter派生的新类:
public class GlobalMouseHandler : IMessageFilter
{
private const int WM_LBUTTONDOWN = 0x201;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
{
// do something
((YourMainForm)Form.ActiveForm).YourMainForm_Click(null, null);
}
return false;
}
}
然后在你的主表单中添加这个来注册消息过滤器:
GlobalMouseHandler globalClick = new GlobalMouseHandler();
Application.AddMessageFilter(globalClick);
并添加此功能以在您的表单中执行您必须执行的任何操作:
public void YourMainForm_Click(object sender, EventArgs e)
{
// do anything here...
}
【讨论】:
解决方案 1
订阅表单中每个控件上的每个事件无疑是最最简单的方法,因为您只需使用 Ramesh 提供的代码。
但是,另一种技术涉及覆盖父控件上的默认 Windows 消息处理方法(“WndProc”) - 在这种情况下,是包含所有控件的表单。 这有一个副作用,当鼠标光标移动到另一个父控件中包含的控件上时,您将无法检测到。
例如,您将无法检测到鼠标光标何时位于包含在 TabControl 中的 TextBox 上。这是因为TabControl 将继续处理所有鼠标事件。
解决方案 2
以下解决方案将克服尝试使用称为 windows hooks 的技术检测鼠标光标位于哪个控件上的所有问题。
Hooks 本质上允许我们在鼠标和键盘事件被分派到具有焦点的窗口之前捕获它们。
这是一个示例:
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MouseHookStruct
{
public POINT pt;
public int hwnd;
public int hitTestCode;
public int dwExtraInfo;
}
[DllImport("user32.dll", SetLastError = true)]
static extern int SetWindowsHookEx(HookType hook, HookProc callback, IntPtr hInstance, uint dwThreadId);
[DllImport("user32.dll", SetLastError= true)]
static extern int CallNextHookEx(int hook, int code, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
static extern int GetLastError();
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
private static int hHook;
public Form1()
{
InitializeComponent();
hHook = SetWindowsHookEx(HookType.WH_MOUSE, MouseHookProc, IntPtr.Zero, (uint)GetCurrentThreadId());
if (hHook == 0)
MessageBox.Show("GetLastError: " + GetLastError());
}
private int MouseHookProc(int code, IntPtr wParam, IntPtr lParam)
{
//Marshall the data from the callback.
MouseHookStruct mouseInfo = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
if (code < 0)
{
return CallNextHookEx(hHook, code, wParam, lParam);
}
else
{
//Create a string variable that shows the current mouse coordinates.
String strCaption = "x = " + mouseInfo.pt.X.ToString("d") +
" y = " + mouseInfo.pt.Y.ToString("d");
//You must get the active form because it is a static function.
Form tempForm = Form.ActiveForm;
Control c = Control.FromHandle((IntPtr)mouseInfo.hwnd);
if (c != null)
label1.Text = c.Name;
else
label1.Text = "Control not found";
//Set the caption of the form.
tempForm.Text = strCaption;
return CallNextHookEx(hHook, code, wParam, lParam);
}
}
【讨论】:
表单中的其他控件无法侦听表单的鼠标事件处理程序。因为每个控件都有自己的鼠标事件监听器。
但是您可以将每个控件的鼠标事件订阅到表单鼠标事件中
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MDown);
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MDown);
this.ListBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MDown);
这样您就可以为所有控件鼠标事件设置一个处理程序。
【讨论】: