我定义了一个静态类Hook,封装了一下几个API函数
GetCurrentThreadId;
SetWindowsHookEx;
CallNextHookEx
UnhookWindowsHookEx
并且定义了委托
public delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam)然后,将SetHook函数添加到Form的OnLoad方法中
public void SetHook()
{
    if (this._nextHookPtr == IntPtr.Zero)
    {
        this._nextHookPtr = Hook.SetWindowsHookEx(
            (int)HookType.WH_MOUSE,
            new HookProc(MyHookProc),
            IntPtr.Zero, Hook.GetCurrentThreadId());
    }
}IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
    if (code < 0)
    {
        return Hook.CallNextHookEx(this._nextHookPtr, code, wparam, lparam);
    }    if (wparam.ToInt32() == 0x0200) //WM_MOUSEMOVE
    {
        int param = lparam.ToInt32();
        int x = param & 0x0000FFFF; // 取出LPARAM的低字节
        int y = param >> 16; // 取出LPARAM的高字节        this.Xtext.Text = x.ToString(); // Xtext是一个TextBox控件
        this.Ytext.Text = y.ToString();// Ytext是一个TextBox控件
    }    return (IntPtr)0;
}我在窗口中移动鼠标,钩子的确捕获了鼠标移动消息,但是计算出来的坐标是错误的,事实上,当我移动鼠标,X和Y的值基本不变。请高手指点,感激不尽!

解决方案 »

  1.   

    要获取鼠标坐标可不用“钩子”
    摘录:《程序员秘书》--源代码--鼠标和键盘--获取和设置鼠标位置
    textBox1.Text = Cursor.Position.X.ToString() + "," + Cursor.Position.Y.ToString();//鼠标当前位置
    立即成为编程经验丰富的程序员不是梦,详见:http://www.psec.net.cn
      

  2.   

    鼠标位置是那样捕捉的么?看看这个private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
            {
                // if ok and someone listens to our events
                if ((nCode >= 0) && (OnMouseActivity != 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 (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 = (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 (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;
                        else clickCount = 1;                //generate event 
                     MouseEventArgs e = new MouseEventArgs(
                                                        button,
                                                        clickCount,
                                                        mouseHookStruct.pt.x,
                                                        mouseHookStruct.pt.y,
                                                        mouseDelta);
                    //raise it
                    OnMouseActivity(this, e);
                }
                //call next hook
                return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
            }
      

  3.   

    一些要用到的Struct以及Event:        public event MouseEventHandler OnMouseActivity;        [StructLayout(LayoutKind.Sequential)]
            private class POINT
            {
                public int x;
                public int y;
            }        [StructLayout(LayoutKind.Sequential)]
            private class MouseHookStruct
            {
                public POINT pt;
                public int hwnd;
                public int wHitTestCode;
                public int dwExtraInfo;
            }        [StructLayout(LayoutKind.Sequential)]
            private class MouseLLHookStruct
            {
                public POINT pt;
                public int mouseData;
                public int flags;
                public int time;
                public int dwExtraInfo;
            }