GET_X_LPARAM gives a negative value

I created a mouse hook. I wanted to get the coordinates of the mouse click, but GET_X_LPARAM () gives me a negative value (and always the same when you click on different places). My problem is solved using GetCursorPos (), but I wonder why it does not work with GET_X_LPARAM / GET_Y_LPARAM. Here is the code:

LRESULT CALLBACK Recorder::mouseHook( int code, WPARAM wParam, LPARAM lParam ) { if( code < 0 ) return CallNextHookEx( m_mouseHook, code, wParam, lParam ); switch( wParam ) { case WM_LBUTTONDOWN:{ int _hereIsANegativeNumber = GET_X_LPARAM( lParam ); break;} } return CallNextHookEx( 0, code, wParam, lParam ); } 

This is how I set the hook:

 m_mouseHook = SetWindowsHookEx( WH_MOUSE_LL, &mouseHook, GetModuleHandle( NULL ), 0 ); 
+4
source share
2 answers

At the vertex of WH_MOUSE_LL lParam not the mouse coordinates - instead, it is a pointer to MSLLHOOKSTRUCT .

So, to get the coordinates:

 POINT pt = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam)->pt; 

See LowLevelMouseProc for more details.

+7
source

Since for WH_MOUSE_LL in LowLevelMouseProc, the procedure you get as the LPARAM variable is a pointer to MSLLHOOKSTRUCT and use the pt member to get the mouse coordinates

+3
source

Source: https://habr.com/ru/post/1432001/


All Articles