Understanding Windows Message Mechanism

Source: Internet
Author: User

 

Windows is a message-driven OS. What is a message? Next we will explain it from several different aspects.

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 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 message loop gives control to the system.Windows supports multiple tasks at the same time. The following pseudocode demonstrates the usage of a message loop:

While (1)
  
{
  
Related Article

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.