Beispiel #1
0
        /// <summary>
        /// 鼠标钩子处理方法
        /// </summary>
        /// <param name="nCode">指定是否需要处理该消息,消息类型WH_MOUSE_LL</param>
        /// <param name="wParam">该消息的附加消息 </param>
        /// <param name="lParam">该消息的附加消息 </param>
        /// <returns></returns>
        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (Flag == HookFlag.IsRunning && (nCode >= 0) && (MouseEvent != null))
            {
                //将数据从非托管内存块封送到新分配的指定类型的托管对象。
                MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
                MouseButtons    mousebutton     = MouseButtons.None;
                short           mouseDelta      = 0;
                switch (wParam)
                {
                case (int)WM_MOUSE.WM_LBUTTONDOWN:
                    mousebutton = MouseButtons.Left;
                    break;

                case (int)WM_MOUSE.WM_RBUTTONDOWN:
                    mousebutton = MouseButtons.Right;
                    break;

                case (int)WM_MOUSE.WM_MBUTTONDOWN:
                    mousebutton = MouseButtons.Middle;
                    break;

                case (int)WM_MOUSE.WM_MOUSEWHEEL:
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                    break;

                case (int)WM_MOUSE.WM_XBUTTONDOWN:
                    if (mouseHookStruct.MouseData == (uint)WM_MOUSE.MD_XBUTTON1)
                    {
                        mousebutton = MouseButtons.XButton1;
                    }
                    else if (mouseHookStruct.MouseData == (uint)WM_MOUSE.MD_XBUTTON2)
                    {
                        mousebutton = MouseButtons.XButton2;
                    }
                    break;
                }
                MouseHookEventArgs e = new MouseHookEventArgs(mousebutton, mouseHookStruct.Point, mouseDelta);
                if (MouseEvent(this, e))
                {
                    return(1);
                }
            }
            return(CallNextHookEx(this._hookHandle, nCode, wParam, lParam));
        }
Beispiel #2
0
        /// <summary>
        /// 鼠标消息回调函数
        /// 功能:修改所有鼠标消息,修改为左键按下;
        /// 按下(抬起)鼠标左键->向下发送Z键按下(抬起)
        /// 按下(抬起)鼠标右键->向下发送X键按下(抬起)
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                theMouseHookStruct     = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));  //定义结构体并赋值鼠标坐标
                myForm.theXYLabel.Text = "X:" + theMouseHookStruct.pt.x.ToString() + "Y:" + theMouseHookStruct.pt.y.ToString();
                Crosshair(theMouseHookStruct.pt);
                switch ((int)wParam)
                {
                case WM_LBUTTONDOWN:
                {
                    mySim.Keyboard.KeyDown(VirtualKeyCode.VK_Z);
                    return(1);
                }

                case WM_LBUTTONUP:
                {
                    mySim.Keyboard.KeyUp(VirtualKeyCode.VK_Z);
                    return(1);
                }

                case WM_RBUTTONDOWN:
                {
                    mySim.Keyboard.KeyDown(VirtualKeyCode.VK_X);
                    return(1);
                }

                case WM_RBUTTONUP:
                {
                    mySim.Keyboard.KeyUp(VirtualKeyCode.VK_X);
                    return(1);
                }

                default: break;
                }
            }
            return(CallNextHookEx(MouseHookID, nCode, wParam, lParam));
        }