C#:按下鼠标左键/右键时如何获取鼠标坐标?

发布于 2024-08-18 03:06:19 字数 96 浏览 5 评论 0原文

按下鼠标左键/右键时如何获取鼠标坐标?

我使用低级鼠标挂钩,并且能够获取光标的当前位置,但我希望能够在按下任何鼠标按钮时检索该位置。

我该怎么做?

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 the position when any mouse button has been pressed.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一生独一 2024-08-25 03:06:19

为什么不捕获 MouseDown< /a> 事件,来自 MouseEventArgs、通过MouseEventArgs.Location获取点击的位置?

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

离笑几人歌 2024-08-25 03:06:19

在 WM_LBUTTONDOWN 上调用 GetMessagePos() 来获取您想要的内容。但我怀疑它是否能在低级鼠标钩子内工作。它适用于您的消息泵或窗口过程。

“GetMessagePos 函数检索 GetMessage 函数检索到的最后一条消息的光标位置。”

你确定你需要一个钩子吗?

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?

梦魇绽荼蘼 2024-08-25 03:06:19

在您的 MouseHook 方法中:

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); 
}

来自此处

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.

涙—继续流 2024-08-25 03:06:19

MouseHook 过程的 wParam 参数将包含消息标识符 WM_LBUTTONDOWN、WM_LBUTTONUP、WM_RBUTTONDOWN、WM_RBUTTONUP 等。从中您可以确定按钮在当前坐标处的状态。

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.

夜光 2024-08-25 03:06:19

http://www.codeproject.com/KB/system/globalsystemhook.aspx - 这解决了我的问题。使用演示项目中的 DLL 并设法获取坐标。

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.

桜花祭 2024-08-25 03:06:19

http://www.codeproject.com/KB/vb-interop/MouseHunter。 aspx - 我发现了这个迷人的小信息。遗憾的是,Visual Studio 2008 不接受已预编译的 dll,并且我无法在我的计算机上安装 Visual Basic 6 来尝试重新编译它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文