I'm a little bit confused about how message loops work in win32 programming. In my WinMain
I always put the following:
while ( GetMessage ( &msg, NULL, 0, 0 ) > 0 )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
This is a while loop that pretty much runs until your application stops. Does that mean you have one message loop per application rather per window?
The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows.
A message loop allows PPAPI calls to be issued on a thread. You may not issue any API calls on a thread without creating a message loop. It also allows you to post work to the message loop for a thread.
An event loop, or sometimes called a message loop, is a thread that waits for and dispatches incoming events. The thread blocks waiting for requests to arrive and then dispatches the event to an event handler function. A message queue is typically used by the loop to hold incoming messages.
The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows . Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window.
The application needs a loop to retrieve the messages and dispatch them to the correct windows. For each thread that creates a window, the operating system creates a queue for window messages. This queue holds messages for all the windows that are created on that thread.
To dispatch the message, Win32 provides an API function, DispatchMessage (). The syntax of the function looks like below: Where lpMsg is a pointer to the valid MSG structure. Usually, we pass the MSG structure which is received from GetMessage () method, through this function. Step 3. Are we done with our Message Loop? Not really.
Unsourced material may be challenged and removed. The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows . Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window.
From About Messages and Message Queues:
Applications with multiple threads can include a message loop in each thread that creates a window.
Note that a message queue CAN support multiple windows... The second parameter of GetMessage
is the handle of the window you want to watch messages for. If NULL
then all the windows of the thread.
As a second note, it is possible to create a message queue without windows (at least from Windows 2000 onward). It is described in the documentation for PostThreadMessage
:
In the thread to which the message will be posted, call
PeekMessage
as shown here to force the system to create the message queue.
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
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