*** Welcome to piglix ***

Message loop in Microsoft Windows


The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows.

Windows GUI programs are event-driven. After starting a process, Windows maintains an individual message queue for it. Windows places messages into that queue whenever mouse activity occurs on that process's window, whenever keyboard activity occurs while that process's window has focus, and at other times. A process can also add messages to its own queue. To accept user input, and for other reasons, each process must continuously retrieve messages from its queue, and act on them. A programmer makes the process do that by writing a loop that calls GetMessage (which blocks for a message and retrieves it), and then calls DispatchMessage (which dispatches the message), and repeats indefinitely. This is the message loop. It is usually part of the main program, which runs on the main thread. Messages for every window of the process pass through its message queue, and are handled by its message loop. A message loop is one kind of event loop.

A basic message loop appears as follows:

It is conventional for the event loop to call TranslateMessage on each message which can translate virtual keystrokes into strings. Calling TranslateMessage is not technically required, but problems can result if it is not called. The message loop must call DispatchMessage.

The message loop does not directly act on the messages that it handles. It dispatches them by calling DispatchMessage, which transfers the message to the "window procedure" for the window that the message was addressed to. (The "window procedure" is a callback procedure, which got associated with the window class when it was registered.) (More than one window can use the same window procedure.)


...
Wikipedia

...