Most of the Win32 main loops I've seen are all structured like:
while (GetMessage(&message, NULL, 0, 0) > 0) { TranslateMessage(&message); DispatchMessage(&message); }
It was pointed out to me that MsgWaitForMultipleObjects
may be used to add some variety to a main loop. But is there a scenario where doing something between GetMessage
, TranslateMessage
and DispatchMessage
is actually useful?
GetMessage pulls the WM_LBUTTONDOWN message from the queue and fills in the MSG structure. Your program calls the TranslateMessage and DispatchMessage functions. Inside DispatchMessage, the operating system calls your window procedure. Your window procedure can either respond to the message or ignore it.
But TranslateMessage is said to translate the virtual keystrokes to characters and post them back to the caller thread's message queue. DispatchMessage directs the OS to call the Windows Procedure of appropriate target window.
The main difference between the two functions is that GetMessage does not return until a message matching the filter criteria is placed in the queue, whereas PeekMessage returns immediately regardless of whether a message is in the queue.
TranslateMessage() converts virtual keys messages to character input messages. It is a separate call for the remote chance that under certain circumstances you would want to not produce character input messages for certain virtual keys.
The more traditional message loop looks like this:
while (GetMessage(&msg, 0, 0, 0)) { if (!TranslateAccelerator(hwndMain, haccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
It is a pretty big hint to what you'd want to do before dispatching the message: catch messages that ought to be intercepted and treated specially before the window sees them. Keyboard shortcuts are a classic example, they need to be detected no matter what window has the focus.
Any GUI class library exposes it with a virtual method named something like App.PreProcessMessage, a virtual function that can be overridden so your program can implement its own shortcuts and whatnot.
They are different beasts.
For TranslateMessage function
Translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function. [...] The TranslateMessage function does not modify the message pointed to by the lpMsg parameter.
DispatchMessage, on the other hand, dispatches a message to a window procedure.
So DispatchMessage
does the actual work of processing the message. TranslateMessage
MAY or MAY NOT post a new message to the thread queue. If the message is translated then a character message is posted to the thread's message queue.
The TranslateMessage function does not modify the message pointed to by the lpMsg parameter.
They are separate calls so you, the programmer, can have a chance to avoid the message translation provided by TranslateMessage
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With