如果您希望监听全局鼠标事件,即使在其他应用程序(如浏览器或其他软件)中按下鼠标按钮也能捕捉到这些事件,您需要使用低级别的Windows API钩子。WinForms本身并不直接支持全局事件监听,但通过调用Windows API,可以实现这一功能。
winform监听全局鼠标事件
如果您希望监听全局鼠标事件,即使在其他应用程序(如浏览器或其他软件)中按下鼠标按钮也能捕捉到这些事件,您需要使用低级别的Windows API钩子。WinForms本身并不直接支持全局事件监听,但通过调用Windows API,可以实现这一功能。
使用全局鼠标钩子
为了实现全局鼠标事件监听,您可以使用SetWindowsHookEx
函数来安装一个鼠标钩子。这将允许您的应用程序接收所有鼠标事件的通知,而不论鼠标指针位于哪个窗口之上。
步骤 1: 定义必要的常量和结构体
首先,定义一些常量和结构体,以便与Windows API进行交互
public class GlobalMouseHook : IDisposable
{
// 钩子类型 - 全局鼠标钩子
private const int WH_MOUSE_LL = 14;
// 鼠标消息
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_RBUTTONDOWN = 0x0204;
// 钩子句柄
private IntPtr _hookID = IntPtr.Zero;
// 钩子过程委托
private LowLevelMouseProc _proc;
// 钩子过程委托类型
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
// MSLLHOOKSTRUCT 结构
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
// POINT 结构
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
} // 设置钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
// 移除钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
// 调用下一个钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
// 获取当前进程模块句柄
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
private Mainform _mainForm; // 用来存储 MainForm 实例
public GlobalMouseHook(Mainform mainForm)
{
_mainForm = mainForm;
_proc = HookCallback;
_hookID = SetHook(_proc);
}
private IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && (wParam == (IntPtr)WM_LBUTTONDOWN || wParam == (IntPtr)WM_RBUTTONDOWN))
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if (wParam == (IntPtr)WM_LBUTTONDOWN)
{
_mainForm.richTextBox1.Text = "全局鼠标左键按下";
//MessageBox.Show($"全局鼠标左键按下:({hookStruct.pt.x}, {hookStruct.pt.y})");
}
else if (wParam == (IntPtr)WM_RBUTTONDOWN)
{
_mainForm.richTextBox1.Text = "全局鼠标右键按下";
//MessageBox.Show($"全局鼠标右键按下:({hookStruct.pt.x}, {hookStruct.pt.y})");
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
public void Dispose()
{
UnhookWindowsHookEx(_hookID);
}
}
// 创建 GlobalMouseHook 实例并传递当前 MainForm 实例 GlobalMouseHook mouseHook = new GlobalMouseHook(this);