Windows Messaging in Win32

Started by
2 comments, last by Erik Rufelt 15 years ago
I'm a little confused with the message loop in windows Win32 API. This is the typical shown: while( GetMessage(&Msg, NULL, 0, 0) ) { TranslateMessage(&Msg); DispatchMessage(&Msg); } The GetMessage function is: BOOL GetMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax); But typically the window handle hWnd for the window created by your program is not given, rather NULL is passed (as I show above). So what is the Msg struct populated with? Info on the next message in the queue no matter what it was intended for? I assume if you passed hWnd it would be window specific, so why would you not always do that? More importantly, if the pointer returned points to a Msg struct that can contain info on any message, how do the Translate or Dispatch functions know to only process items meant for the window/app in question since they only receive a pointer to the Msg struct (i.e. they don't get hWnd to allow them to discard if it isn't relevant). I believe they call WndProc, but WndProc simply handles the final message, it doesn't decide if it applies. Or am I just looking at this all wrong? Thanks!
Advertisement
According to MSDN's entry on GetMessage():
Quote:If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.


And if you looked at the MSG structure you'd see a member called hwnd.
Quote:Original post by SiCrane
According to MSDN's entry on GetMessage():
Quote:If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.


And if you looked at the MSG structure you'd see a member called hwnd.


Ahh, so it is thread specific. I missed that in my trips to msdn.

I did look at the hWnd in the Msg structure, but I was/am unsure how that hWnd is later compared to the hWnd in question (the one for the current window) if the current hWnd isn't passed as an argument to translate or dispatch message. They get a pointer to Msg, but that hWnd value in Msg is for the next message for the thread, not necessarily for the next message for the intended window (assuming multiple windows in one thread). Or is the WndProc function intended to handle all messages for all windows in the thread? I'll bet it is now that I think about it.... I see hWnd in its arguments and in its body....

Again, sorry if I'm being dense.
The WndProc depends on the window class. There may be one or more windows using the same window-class, which you specify in CreateWindow. I assume you create a window class with RegisterClass?
If so, you are passing it the WndProc to use in a parameter in the WNDCLASS structure. It is also possible to change the WndProc for a window with SetWindowLongPtr, which can be used to subclass a window, and alter all or part of it's behavior.

There can be multiple different windows, some using the same WndProc and some using another, all handled by the very same message loop. DispatchMessage will automatically send the message to the correct WndProc, depending on what window the message belongs to. Messages not belonging to any window at all won't be sent to any WndProc.

This topic is closed to new replies.

Advertisement