开发者

C#: How do I get the coordinates of my mouse when left/right mouse button has been pressed?

How do I get the coordinates of my mouse when left/right mouse button has been pressed?

I am using a low level mouse hook and am able to get the current position of my cursor, but I would like to be able to retrieve th开发者_StackOverflow中文版e position when any mouse button has been pressed.

How can I do this?


Why don't you just capture the MouseDown Event, and from the MouseEventArgs, obtain the position of the click by using MouseEventArgs.Location?


call GetMessagePos() on WM_LBUTTONDOWN to get what you want. But I doubt that it will work inside a low-level mousehook. It's meant to be used in your message pump or window proc.

"The GetMessagePos function retrieves the cursor position for the last message retrieved by the GetMessage function."

Are you sure you need a hook?


In your MouseHook method:

public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    //Marshall the data from the callback.
    MouseHookStruct MyMouseHookStruct = 
         (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

    if (nCode >= 0)
    {
        int xcoord = MyMouseHookStruct.pt.x;
        int ycoord = MyMouseHookStruct.pt.y;
    }

    return CallNextHookEx(hHook, nCode, wParam, lParam); 
}

From here.


The wParam argument of your MouseHook procedure will contain the message identifier WM_LBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONDOWN, WM_RBUTTONUP, etc.. from this you can determine what the button state is at the current coordinates.

  • MouseProc Function
  • LowLevelMouseProc Function
  • MOUSEHOOKSTRUCT Structure
  • About Hooks
  • Windows Hooks in the .NET Framework


http://www.codeproject.com/KB/system/globalsystemhook.aspx - this solved my problem. Used the DLL's from the demo project and managed to get the coordinates.


http://www.codeproject.com/KB/vb-interop/MouseHunter.aspx - I found this little charming piece of information. Sadly Visual Studio 2008 won't accept the dll that's been precompiled, and I can't get Visual Basic 6 to install on my machine to try to recompile it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜