程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 鼠標全局鉤子

C# 鼠標全局鉤子

編輯:C#入門知識

C# 鼠標全局鉤子


/// 
    /// 鼠標全局鉤子
    /// 
    public class MouseHook
    {
        private const int WM_MOUSEMOVE   = 0x200;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_LBUTTONUP   = 0x202;
        private const int WM_RBUTTONUP   = 0x205;
        private const int WM_MBUTTONUP   = 0x208;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_MBUTTONDBLCLK = 0x209;

        /// 
        /// 點
        /// 
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }

        /// 
        /// 鉤子結構體
        /// 
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hWnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        public const int WH_MOUSE_LL = 14; // mouse hook constant

        // 裝置鉤子的函數
        [DllImport(user32.dll, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        // 卸下鉤子的函數
        [DllImport(user32.dll, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        // 下一個鉤掛的函數
        [DllImport(user32.dll, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

        // 全局的鼠標事件
        public event MouseEventHandler OnMouseActivity;

        // 鉤子回調函數
        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

        // 聲明鼠標鉤子事件類型
        private HookProc   _mouseHookProcedure; 
        private static int _hMouseHook = 0; // 鼠標鉤子句柄
        
        /// 
        /// 構造函數
        /// 
        public MouseHook()
        {

        }
        
        /// 
        /// 析構函數
        /// 
        ~MouseHook()
        {
            Stop();
        }

        /// 
        /// 啟動全局鉤子
        /// 
        public void Start()
        {
            // 安裝鼠標鉤子
            if (_hMouseHook == 0)
            {
                // 生成一個HookProc的實例.
                _mouseHookProcedure = new HookProc(MouseHookProc);

                _hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, _mouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), 0);

                //如果裝置失敗停止鉤子
                if (_hMouseHook == 0)
                {
                    Stop();
                    throw new Exception(SetWindowsHookEx failed.);
                }
            }
        }

        /// 
        /// 停止全局鉤子
        /// 
        public void Stop()
        {
            bool retMouse = true;

            if (_hMouseHook != 0)
            {
                retMouse    = UnhookWindowsHookEx(_hMouseHook);
                _hMouseHook = 0;
            }

            // 如果卸下鉤子失敗
            if (!(retMouse)) 
                throw new Exception(UnhookWindowsHookEx failed.);
        }

        /// 
        /// 鼠標鉤子回調函數
        /// 
        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            // 如果正常運行並且用戶要監聽鼠標的消息
            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                MouseButtons button = MouseButtons.None;
                int clickCount = 0;

                switch (wParam)
                {
                    case WM_LBUTTONDOWN:
                        button = MouseButtons.Left;
                        clickCount = 1;
                        break;
                    case WM_LBUTTONUP:
                        button = MouseButtons.Left;
                        clickCount = 1;
                        break;
                    case WM_LBUTTONDBLCLK:
                        button = MouseButtons.Left;
                        clickCount = 2;
                        break;
                    case WM_RBUTTONDOWN:
                        button = MouseButtons.Right;
                        clickCount = 1;
                        break;
                    case WM_RBUTTONUP:
                        button = MouseButtons.Right;
                        clickCount = 1;
                        break;
                    case WM_RBUTTONDBLCLK:
                        button = MouseButtons.Right;
                        clickCount = 2;
                        break;
                }

                // 從回調函數中得到鼠標的信息
                MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
                MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0);

                // 如果想要限制鼠標在屏幕中的移動區域可以在此處設置
                // 後期需要考慮實際的x、y的容差
                if(!Screen.PrimaryScreen.Bounds.Contains(e.X, e.Y))
                {
                    //return 1;
                }

                OnMouseActivity(this, e);
            }

            // 啟動下一次鉤子
            return CallNextHookEx(_hMouseHook, nCode, wParam, lParam);
        }
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved