问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/4758527/mouse-state-winapi