简体   繁体   中英

Viewing Win32 messages

Hello everyone I am new to windows32 programming and I have a couple of questions-:

When I use the following code in a program it works fine -:

while(GetMessage(&msg,NULL,0,0))
  {
    TranslateMessage(&msg);                                  
    DispatchMessage(&msg);  
}

But when I replace null of GetMessage to hwnd(the handle of the window just created) the doesn't seem to close it still remains running in the background. Why does this happen when I replace NULL with hwnd means I am receiving messages for only one window then why doesn't it work????

while(GetMessage(&msg,hwnd,0,0))
  {
    TranslateMessage(&msg);                                  
    DispatchMessage(&msg);  
}

By the way the windows function is-:

LRESULT CALLBACK WinProc(HWND hWnd, UINT message,
                              WPARAM wparam, LPARAM lparam){

    switch(message){
                case WM_DESTROY:
                     PostQuitMessage(0);
                     break;
                default:
                return DefWindowProc(hWnd, message, wparam, lparam);
                }
    return 0;
} 

Secondly-:

Is there any way I can see all the messages sent to any particular window????

Thirdly-:

What is the reason behind writing __stdcall(WINAPI) when compiling my windows programs????

A quick reply would be appreciated.Thank You.

  1. GetMessage returns 0 (making the loop end) only when it receives a WM_QUIT , but a WM_QUIT is not associated to any particular window, so it is never received if you have a GetMessage that asks only messages for a certain hWnd .

  2. If it's a window of yours, you already see them inside their window procedure; if you want to filter them before dispatching them to their window procedure, you can check the msg structure that is populated by GetMessage before calling DispatchMessage .

  3. The whole Windows API uses the stdcall calling convention (I think because it is slightly faster/produces less boilerplate code than the usual cdecl ), so also your callbacks must follow that calling convention. Notice that you must use WINAPI (ie stdcall ) only on functions that are called by Windows API functions, for the other ones you are free to use whatever calling convention you like best.

PostQuitMessage generates WM_QUIT which is processed by the message queue, but not associated with a particular window. By filtering only hwnd messages in your call to GetMessage , you don't process WM_QUIT .

Regarding seeing all messages being sent to a window / thread / process, see https://stackoverflow.com/questions/4038730/i-am-looking-for-a-windows-spy-application

Finally, regarding __stdcall , see What does "WINAPI" in main function mean?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM