C#:按下鼠标左键/右键时如何获取鼠标坐标?
按下鼠标左键/右键时如何获取鼠标坐标?
我使用低级鼠标挂钩,并且能够获取光标的当前位置,但我希望能够在按下任何鼠标按钮时检索该位置。
我该怎么做?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么不捕获 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
?在 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?
在您的 MouseHook 方法中:
来自此处。
In your MouseHook method:
From here.
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.
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.
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.