/// <summary>
        /// 判断是否执行鼠标的单击操作
        /// </summary>
        /// <param wParam="IntPtr">鼠标键值</param>
        /// <param lParam="IntPtr">指针</param>
        private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            EventHandler <MouseExEventArgs> handler = this.Events[EventMouseActivity] as EventHandler <MouseExEventArgs>;

            if ((nCode >= 0) && (handler != null))
            {
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); //定义鼠示托管对象
                MouseButtons      button          = MouseButtons.None;                                                            //实例化MouseButtons类,用于记录鼠标曾按下的数量
                short             mouseDelta      = 0;
                switch ((int)wParam)                                                                                              //鼠标的键值
                {
                case WM_LBUTTONDOWN:                                                                                              //鼠标左键
                    button = MouseButtons.Left;                                                                                   //记录鼠标左键的单击次数
                    break;

                case WM_RBUTTONDOWN:             //鼠标右键
                    button = MouseButtons.Right; //记录鼠标右键的单击次数
                    break;

                case WM_MOUSEWHEEL:                                                               //鼠标中键
                    mouseDelta = unchecked ((short)((mouseHookStruct.mouseData >> 16) & 0xffff)); //获取鼠标中键的键值
                    break;
                }
                //实例化MouseExEventArgs类
                MouseExEventArgs e = new MouseExEventArgs(button, 0, mouseHookStruct.pt.x, mouseHookStruct.pt.y, mouseDelta, mouseHookStruct.flags);
                handler(this, e);                                      //处理事件
            }
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam)); //调用下一个勾子过程
        }
예제 #2
0
        private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // if ok and someone listens to our events
            EventHandler <MouseExEventArgs> handler = this.Events[EventMouseActivity] as EventHandler <MouseExEventArgs>;

            if ((nCode >= 0) && (handler != null))
            {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                switch ((int)wParam)
                {
                case WM_LBUTTONDOWN:
                    // case WM_LBUTTONUP:
                    // case WM_LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    break;

                case WM_RBUTTONDOWN:
                    // case WM_RBUTTONUP:
                    // case WM_RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    break;

                case WM_MOUSEWHEEL:
                    // If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta.
                    // One wheel click is defined as WHEEL_DELTA, which is 120.
                    // (value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = unchecked ((short)((mouseHookStruct.mouseData >> 16) & 0xffff));
                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, mouseData is not used.
                    break;
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                {
                    if ((int)wParam == WM_LBUTTONDBLCLK || (int)wParam == WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;
                    }
                    else
                    {
                        clickCount = 1;
                    }
                }

                //generate event
                MouseExEventArgs e = new MouseExEventArgs(
                    button,
                    clickCount,
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    mouseDelta,
                    mouseHookStruct.flags
                    );

                //raise it
                handler(this, e);
            }
            //call next hook
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
예제 #3
0
        private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // if ok and someone listens to our events
            EventHandler<MouseExEventArgs> handler = this.Events[EventMouseActivity] as EventHandler<MouseExEventArgs>;
            if ((nCode >= 0) && (handler != null)) {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button = MouseButtons.None;
                short mouseDelta = 0;
                switch ((int)wParam) {
                case WM_LBUTTONDOWN:
                    // case WM_LBUTTONUP:
                    // case WM_LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    break;
                case WM_RBUTTONDOWN:
                    // case WM_RBUTTONUP:
                    // case WM_RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    break;
                case WM_MOUSEWHEEL:
                    // If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta.
                    // One wheel click is defined as WHEEL_DELTA, which is 120.
                    // (value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = unchecked((short)((mouseHookStruct.mouseData >> 16) & 0xffff));
                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, mouseData is not used.
                    break;
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                    if ((int)wParam == WM_LBUTTONDBLCLK || (int)wParam == WM_RBUTTONDBLCLK)
                        clickCount = 2;
                    else
                        clickCount = 1;

                //generate event
                MouseExEventArgs e = new MouseExEventArgs(
                   button,
                   clickCount,
                   mouseHookStruct.pt.x,
                   mouseHookStruct.pt.y,
                   mouseDelta,
                   mouseHookStruct.flags
                );

                //raise it
                handler(this, e);
            }
            //call next hook
            return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
        }