1

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.

Sreyan
  • 357
  • 2
  • 12
  • 1
    With regard to the second part of your question, [Spy++](http://msdn.microsoft.com/en-us/library/dd460726.aspx) can show you the messages being sent to particular windows. – Andrew Lambert Apr 04 '12 at 17:05

2 Answers2

3
  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 (i.e. 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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • About 2), not all messages come from GetMessage: messages that are sent (synchronously) as part of other message processing goes to the window procedure as nested call, now by the loop. And by the way, WM_QUIT is one of these, since it is normally an *answer* to something else, like a click on the close button, or a response to a "close" command and the like. – Emilio Garavaglia Apr 04 '12 at 16:30
  • @Emilio: all correct, in facts, since he owns the window procedures, the correct way to see all the messages targeted to a window of his is to just check them inside the appropriate wndproc. – Matteo Italia Apr 04 '12 at 19:26
  • Is there any message that is sent once a windows is fully initialized when the entire display has been drawn to the screen ??? – Sreyan Apr 06 '12 at 09:36
  • Define "fully initialized" - it's an application-dependent thing, how is windows supposed to know? – Matteo Italia Apr 06 '12 at 11:19
  • when all resources are loaded. my situation is that i want to show a dialog box right after the window has been created. – Sreyan Apr 07 '12 at 18:06
  • Check out `WM_INITDIALOG` (called when the dialog is displayed) or `WM_CREATE` (called when the window is created). – Matteo Italia Apr 07 '12 at 22:57
1

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?

Community
  • 1
  • 1
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • Is there any message that is sent once a windows is fully initialized when the entire display has been drawn to the screen ??? – Sreyan Apr 06 '12 at 09:36
  • For what purpose? Not all windows are displayed. There is `WM_PAINT` or `WM_CREATE` - either might work for you. – tenfour Apr 06 '12 at 09:38