鼠标状态 winapi
有没有办法在 C++ 中使用 winapi 获取鼠标状态(位置、按钮状态)? 我不想使用 Windows 消息(WM_MOUSEMOVE、WM_LBUTTONDOWN 等)。
谢谢你!
Is there any way to get mouse state (position, buttons states) using winapi in C++?
I don't want to use windows messages (WM_MOUSEMOVE, WM_LBUTTONDOWN, etc).
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在寻找 GetCursorInfo 和GetKeyState。您可以使用虚拟键码调用后者指定感兴趣的鼠标按钮。
It sounds like you are looking for GetCursorInfo and GetKeyState. The latter you call with virtual key codes that specify the mouse button of interest.
如果您只需要光标位置,则可以使用 GetCursorPos()。请记住 GetCursorInfo() 和GetCursorPos() 返回屏幕坐标。使用 ScreenToClient() 转换为客户区偏移。
虽然OP不想使用Windows Messages,但我只是想提一下作为旁注。
我发现将光标位置作为消息处理程序的一部分(例如 WM_SETCURSOR),大多数文献建议使用 GetMessagePos() 检索消息发送时光标的位置。但是,它是鼠标移动之前的位置,而不是之后。因此,当尝试在某个区域上进行鼠标悬停检测时,位置返回“滞后”于像素之后。
If you only need cursor position, you can just use GetCursorPos(). Remember that both GetCursorInfo() and GetCursorPos() return screen coordinates. Use ScreenToClient() to convert to client area offsets.
Although the OP didn't want to use Windows Messages, I just wanted to mention something as a sidenote.
Something I found was that getting the cursor position as part of a message handler (for instance WM_SETCURSOR), most of the literature recommends using GetMessagePos() to retrieve the cursor's position at the time the message was sent. However, its the position before the mouse moved, not after. So the position returned 'lags' behind a pixel when trying to do mouseover detection over an area.