Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the mouse coordinates in the WM_LBUTTONDOWN message

Tags:

c++

winapi

the structure of MSG as followed:

typedef struct tagMSG {
  HWND   hwnd; 
  UINT   message; 
  WPARAM wParam; 
  LPARAM lParam; 
  DWORD  time; 
  POINT  pt; 
} MSG, *PMSG; 


the message procedure as followed:

long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)

My Question: In the message procedure, why it doesn't pass the POINT variable to window procedure, and how to find mouse's POINT ? By GetCursorPos() ? I find some example get it by LOWORD(lParam), HIWORD(lParam) directly.. can you tell me the information about it ? thank you...

i had see someone write this, is it right ? i not sure:

RECT rect1;
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
    HDC hDC;        
    WORD x,y;   
    PAINTSTRUCT ps;     

    x = LOWORD(lParam); 
    y = HIWORD(lParam);

    switch(iMessage)
    {
    case WM_LBUTTONDOWN:
        if(wParam&MK_CONTROL)
        {
            rect1.left = x; 
            rect1.top = y;
        }
        else if(wParam&MK_SHIFT)
        {
            rect1.left = x; 
            rect1.top = y;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        return(DefWindowProc(hWnd,iMessage,wParam,lParam));
    }
    return 0;
}
like image 224
Mr.Tu Avatar asked Dec 17 '22 00:12

Mr.Tu


1 Answers

In the process function, why it doesn't pass the POINT variable to process function, and how to find mouse's POINT ?

There are separate functions to retrieve that information if you really want it. Most message handlers don't deal with mouse input, and there's no point in passing around extra arguments for message handlers that will almost never use them. (Arguably WndProc could have been defined to take a MSG*; I don't know the reason for its design, but I'd guess that members were added to the MSG structure over time.)

By GetCursorPos() ?

No. GetCursorPos will return the current position of the cursor, which might be different from the position when the message was generated. You instead want GetMessagePos. (This is analogous to GetAsyncKeyState versus GetKeyState.)

Similarly, a message handler can obtain the message time through GetMessageTime.

like image 99
jamesdlin Avatar answered Mar 23 '23 01:03

jamesdlin