c#鼠标钩子全解

using System;
using System.Runtime.InteropServices;//调用操作系统动态链接库
using System.Diagnostics;
using System.Windows.Forms;
namespace util
{
     public class 鼠标钩子类
    {
         //使用方法
         //private 鼠标钩子类 hook = new 鼠标钩子类();
         //hook.InstallHook(this.拦截函数);
         //pubic void 拦截函数(鼠标钩子类.鼠标信息结构体 hookstruct,out bool handle){}
         //if(hook !=null) hook.UninstallHook();

        public const int WH_MOUSE_LL = 14;//全局钩子鼠标为14
         public const int WM_MOUSEMOVE = 0x200; //移动鼠标时发生,同WM_MOUSEFIRST
         public const int WM_LBUTTONDOWN = 0x201; //按下鼠标左键
         public const int WM_LBUTTONUP = 0x202; //释放鼠标左键
         public const int WM_LBUTTONDBLCLK = 0x203;//双击鼠标左键
         public const int WM_RBUTTONDOWN = 0x204;//按下鼠标右键
         public const int WM_RBUTTONUP = 0x205;//释放鼠标右键
         public const int WM_RBUTTONDBLCLK = 0x206; //双击鼠标右键
         public const int WM_MBUTTONDOWN = 0x207; //按下鼠标中键
         public const int WM_MBUTTONUP = 0x208;//释放鼠标中键
         public const int WM_MBUTTONDBLCLK = 0x209;//双击鼠标中键
         public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
         public delegate void 调用端函数(鼠标信息结构体 param, out bool handle);
         private static int 是否以安装 = 0;
         private HookProc 执行函数引用;
         [StructLayout(LayoutKind.Sequential)]
         public class POINT
         {
             public int x;
             public int y;
         }
         [StructLayout(LayoutKind.Sequential)]
         public class 鼠标信息结构体
         {
             public POINT pt;
             public int hwnd;
             public int wHitTestCode;
             public int dwExtraInfo;
         }

         //设置钩子 
         //idHook为13代表键盘钩子为14代表鼠标钩子,lpfn为函数指针,指向需要执行的函数,hInstance为指向进程块的指针,threadId默认为0就可以了
         [DllImport("user32.dll")]
         private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

         //取消钩子 
         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
         private static extern bool UnhookWindowsHookEx(int idHook);

         //调用下一个钩子 
         [DllImport("user32.dll")]
         private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

         //获取当前线程ID(获取进程块)
         [DllImport("kernel32.dll")]
         private static extern int GetCurrentThreadId();

         //Gets the main module for the associated process.
         [DllImport("kernel32.dll")]
         private static extern IntPtr GetModuleHandle(string name);

         private IntPtr 进程块 = IntPtr.Zero;
         //构造器
         public 鼠标钩子类() { }
         //外部调用的鼠标处理事件
         private static 调用端函数 调用函数引用 = null;

         public void InstallHook(调用端函数 clientMethod)
         {
             调用函数引用 = clientMethod;
             // 安装鼠标钩子 
             if (是否以安装 == 0)
             {
                 执行函数引用 = new HookProc(执行函数);
                 进程块 = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
                 是否以安装 = SetWindowsHookEx(WH_MOUSE_LL,执行函数引用,进程块,0);
                 //如果设置钩子失败. 
                 if (是否以安装 == 0) UninstallHook();
             }
         }

         public void UninstallHook()
         {
             if (是否以安装 != 0)
             {
                 bool ret = UnhookWindowsHookEx(是否以安装);
                 if (ret) 是否以安装 = 0;
             }
         }

         //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。
         private static int 执行函数(int nCode, int wParam, IntPtr lParam)
         {
             if (nCode >= 0 && wParam == (int)WM_LBUTTONUP)
             {
                     鼠标信息结构体 MyMouseHookStruct = (鼠标信息结构体)Marshal.PtrToStructure(lParam, typeof(鼠标信息结构体));
                     if (是否以安装 != 0)
                     {
                         bool ret = UnhookWindowsHookEx(是否以安装);
                         if (ret) 是否以安装 = 0;
                     }
                 if (调用函数引用 != null)
                     {
                         bool handle = false;
                         //调用客户提供的事件处理程序。
                         调用函数引用(MyMouseHookStruct, out handle);
                         if (handle) return 1; //1:表示拦截键盘,return 退出
                     }
             }
             return CallNextHookEx(是否以安装, nCode, wParam, lParam);   //拦截下一个鼠标事件
         }

    }
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是C#全局鼠标钩子的完整源码: 首先,创建一个名为MouseHook的类,实现IMouseHook接口: ```csharp using System; using System.Runtime.InteropServices; namespace MouseHookExample { public class MouseHook : IMouseHook { private const int WH_MOUSE_LL = 14; private const int WM_LBUTTONDOWN = 0x0201; private const int WM_RBUTTONDOWN = 0x0204; private LowLevelMouseProc _proc; private IntPtr _hookId = IntPtr.Zero; public event EventHandler<MouseEventArgs> LeftButtonDown; public event EventHandler<MouseEventArgs> RightButtonDown; public void Start() { _proc = HookCallback; _hookId = SetHook(_proc); } public void Stop() { UnhookWindowsHookEx(_hookId); _hookId = IntPtr.Zero; } private IntPtr SetHook(LowLevelMouseProc proc) { using (var curProcess = System.Diagnostics.Process.GetCurrentProcess()) using (var 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)) { var hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var args = new MouseEventArgs(hookStruct.pt.x, hookStruct.pt.y); if (wParam == (IntPtr)WM_LBUTTONDOWN) { LeftButtonDown?.Invoke(this, args); } else if (wParam == (IntPtr)WM_RBUTTONDOWN) { RightButtonDown?.Invoke(this, args); } } return CallNextHookEx(_hookId, nCode, wParam, lParam); } private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); [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); [StructLayout(LayoutKind.Sequential)] private struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] private struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; } } } ``` 接下来,定义一个名为MouseEventArgs的类,用于传递鼠标事件的参数: ```csharp using System; namespace MouseHookExample { public class MouseEventArgs : EventArgs { public int X { get; } public int Y { get; } public MouseEventArgs(int x, int y) { X = x; Y = y; } } } ``` 最后,定义一个名为IMouseHook的接口,用于规范MouseHook类的实现: ```csharp using System; namespace MouseHookExample { public interface IMouseHook { event EventHandler<MouseEventArgs> LeftButtonDown; event EventHandler<MouseEventArgs> RightButtonDown; void Start(); void Stop(); } } ``` 现在,我们可以在其他类中使用该鼠标钩子: ```csharp using System; namespace MouseHookExample { class Program { static void Main(string[] args) { var mouseHook = new MouseHook(); mouseHook.LeftButtonDown += (sender, e) => Console.WriteLine($"Left button down at ({e.X}, {e.Y})"); mouseHook.RightButtonDown += (sender, e) => Console.WriteLine($"Right button down at ({e.X}, {e.Y})"); mouseHook.Start(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); mouseHook.Stop(); } } } ``` 现在,当用户按下鼠标左键或右键时,程序将在控制台中输出相应的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯数据架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值