The Windows system is a message-driven OS, what is the message? I am hard to say clearly, it is also difficult to define (who is I), I will explain from different aspects, I hope everyone will understand.
1, the composition of the message: A message consists of a message name (UINT), and two parameters (WPARAM, LPARAM). The system will send a message to a window when the user has changed or the state of the window changes. For example, there will be a WM_COMMAND message sent after the menu is transferred, and the HiWord (WPARAM) is the ID number of the command, and the menu is the menu ID. Of course, users can define their own message names, or use custom messages to send notifications and transfer data.
2, who will receive a message: A message must be received by a window. The message can be analyzed in the process (WndProc) to process the messages of yourself. For example, you want to handle the menu selection Then you can define the code that processes the WM_COMMAND. If you want to perform graphic output in the window, you must process WM_PAINT.
3, the unopened message is there: M $ writes the default window process for the window, which will be responsible for handling those you do not address messages. Because of this default window process, we can use Windows window to develop without having to pay too much attention to the processing of various messages. For example, there will be a lot of messages sent when the window is dragged, and we can use it to handle the system yourself.
4, window handle: When you say messages, you cannot say a window handle. The system is uniquely identified in the entire system through the window handle. When you send a message, you must specify a window handle indicating that the message is received by that window. Each window will have its own window process, so the user's input will be processed correctly. For example, there are two windows sharing a window process code, and when you press the mouse on the window, the message will be sent to the window until the window is not the window.
5. Example: There is a pseudo code to demonstrate how to process messages during the window.
Long YourWndProc (HWND HWND, UINT UmessageType, WPARAM WP, LPARAM)
{
Switch (UmessageType)
{// use the switch statement to separate various messages
Case (wm_paint):
DoyourWindow (...); // Over the window needs to re-draw
Break;
Case (wm_lbuttondown):
DoyourWork (...); // Treatment when the left mouse button is pressed
Break;
DEFAULT:
CallDefaultWndProc (...); // Let the system handles yourself for other situations
Break;
}
}
Let's talk about what is a message mechanism: The system will maintain one or more message queues, all generated messages are placed back or inserted into the queue. Each message will be taken in the queue, and the message is sent to the message loop of the program with the window according to the message receiving handle. Each running program has its own message loop, gets its own message in the loop and calls the corresponding window process based on the handle of the received window. When there is no message, the message loop will give the control to the system so Windows can simultaneously perform multiple tasks. The following pseudo code demonstrates the usage of the message loop:
While (1)
{
ID = getMessage (...);
IF (id == quit)
Break;
TranslateMessage (...);
}
When the program does not have a message notification, GetMessage will not return, and it will not take up the system's CPU time. Illustration message delivery mode
There is only one message queue in the system in the 16-bit system, so the system must wait for the current task to process the message to send the next message to the corresponding program. If a program is trapped, the system will not be To control. This multitasking system is also called a multi-tasking system. Windows3.x is this system. Each running program in the 32-bit system will have a message queue, so the system can be converted in multiple message queues without waiting for the current program to complete message processing. This multi-tasking system is called a predetermined multitasking system. Windows95 / NT is this system.