dotnet C# 全局 Windows 鼠标钩子

Posted lindexi_gd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dotnet C# 全局 Windows 鼠标钩子相关的知识,希望对你有一定的参考价值。

本文来告诉大家一个简单的方法实现全局的 鼠标钩子

实现封装方法十分简单,请看代码

    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/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新

如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入

如有不方便在博客评论的问题,可以加我 QQ 2844808902 交流


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

以上是关于dotnet C# 全局 Windows 鼠标钩子的主要内容,如果未能解决你的问题,请参考以下文章

dotnet C# 全局 Windows 鼠标钩子

单击标题栏按钮时,全局低级鼠标钩子会导致冻结

c++:使用全局鼠标钩子重新定位窗口

注册一个全局钩子来检测鼠标是不是拖动文件/文本

钩子编程(HOOK) 屏蔽全部按键鼠标及系统功能键

csharp 全局键盘鼠标钩子