本文来告诉大家一个简单的方法实现全局的 鼠标钩子
实现封装方法十分简单,请看代码
public class MouseHookEventArgs : EventArgs
{
public bool Handle { get; set; }
/// <inheritdoc />
public MouseHookEventArgs(MouseMessages mouseMessage)
{
MouseMessage = mouseMessage;
}
public MouseMessages MouseMessage { get; }
public enum MouseMessages
{
MouseDown,
MouseMove,
MouseUp,
}
}
/// <summary>
/// 鼠标钩子
/// </summary>
public static class MouseHook
{
private delegate IntPtr MouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static MouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
{
WM_MOUSEMOVE = 0x0200
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
private POINT pt;
private uint mouseData;
private uint flags;
private uint time;
private IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
MouseProc 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);
/// <summary>
/// 开启全局钩子
/// </summary>
/// <param name="moduleName"></param>
public static void Start(string moduleName)
{
Debug.WriteLine($"模块 {moduleName} 开启全局鼠标钩子");
_hookID = SetHook(_proc);
}
public static void Stop()
{
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetHook(MouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
Kernel32.GetModuleHandle(curModule.ModuleName), 0);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 )
{
MouseHookEventArgs mouseHookEventArgs = null;
switch ((WM) wParam)
{
case WM.MOUSEMOVE:
mouseHookEventArgs=(new MouseHookEventArgs(MouseHookEventArgs.MouseMessages.MouseMove));
break;
case WM.LBUTTONDOWN:
mouseHookEventArgs=(new MouseHookEventArgs(MouseHookEventArgs.MouseMessages.MouseDown));
break;
case WM.LBUTTONUP:
mouseHookEventArgs=(new MouseHookEventArgs(MouseHookEventArgs.MouseMessages.MouseUp));
break;
}
//MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if (mouseHookEventArgs != null)
{
OnMouseEvent(mouseHookEventArgs);
if (mouseHookEventArgs.Handle)
{
return IntPtr.Zero;
}
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
public static event EventHandler<MouseHookEventArgs> MouseEvent;
private static void OnMouseEvent(MouseHookEventArgs e)
{
MouseEvent?.Invoke(null, e);
}
}
本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-C-%E5%85%A8%E5%B1%80-Windows-%E9%BC%A0%E6%A0%87%E9%92%A9%E5%AD%90.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者收藏我的博客导航
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 。
无盈利,不卖课,做纯粹的技术博客
以下是广告时间
推荐关注 Edi.Wang 的公众号
欢迎进入 Eleven 老师组建的 .NET 社区
以上广告全是友情推广,无盈利