Analysis of WINDOWS Message Mechanism

Source: Internet
Author: User
Windows is a message-driven OS. What is a message? It is hard to say clearly, and it is difficult to define a definition. I will explain it from several different aspects. I hope you will understand it later.

1. Composition of a message: a message consists of a message name (uint) and two parameters (wparam and lparam ). When the user enters the window or the window status changes, the system will send a message to a window. For example, after the menu is converted, A wm_command message is sent. hiword (wparam) in the wparam is the command ID, and the menu is the menu ID. Of course, you can also define your own message name, or use custom messages to send notifications and send data.

2. Who will receive the message: a message must be received in a window. In the window process (wndproc), you can analyze messages and process messages that you are interested in. For example, if you want to process the menu selection, you can define the code to process wm_command. If you want to output graphics in the window, you must process wm_paint.

3. Unprocessed messages are written there: M $ writes the default Window Process for the window, which will process those messages you do not process. With this default window process, we can use Windows for development without having to pay too much attention to the processing of various messages in windows. For example, when a window is dragged, there will be a lot of messages sent, and we can ignore it and let the system handle it.

4. Window handle: when it comes to messages, the window handle cannot be left blank. The system uses the window handle to uniquely identify a window in the system, when sending a message, you must specify a window handle to indicate that the message is received by that window. Each window has its own window process, so user input is processed correctly. For example, two Windows share the same window Process Code. When you press the mouse over the window, the message is sent to window 1 rather than window 2 through the handle of Window 1.

5. Example: The following pseudocode demonstrates how to process messages in the window.

Long yourwndproc (hwnd, uint umessagetype, wparam WP, lparam)
{
Switch (umessagetype)
{// Use the switch statement to separate messages
Case (wm_paint ):
Doyourwindow (...); // output when the window needs to be re-drawn
Break;
Case (wm_lbuttondown ):
Doyourwork (...); // process when the left mouse button is pressed
Break;
Default:
Calldefaultwndproc (...); // Let the system handle other situations by itself
Break;
}
}

Next, let's talk about the message mechanism: the system will maintain one or more message queues, and all generated messages will be put or inserted into the queue. The system extracts each message from the queue and sends the message to the program with the window according to the message receiving handle. Each running program has its own message loop. in the loop, it obtains its own message and calls the corresponding window process according to the handle of the receiving window. When there is no message, the control of the message loop is handed over to the system, so windows can perform multiple tasks at the same time. The following pseudocode demonstrates the usage of a message loop:

While (1)
{
Id = getmessage (...);
If (ID = quit)
Break;
Translatemessage (...);
}

If the program does not receive a message notification, getmessage will not be returned and will not occupy the CPU time of the system. Message Delivery Mode

In a 16-bit system, there is only one message queue in the system. Therefore, the system must wait for the current task to process the message before sending the next message to the corresponding program, if a program is stuck in an endless loop or time-consuming operation, the system will not be under control. This type of multi-task system is also called a collaborative multi-task system. Windows3.x is such a system.

In a 32-bit system, each running program has a message queue. Therefore, the system can convert multiple message queues without waiting for the current program to complete message processing. This multi-task system is called a preemptive multi-task system. Windows 95/NT is such a system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.