Windows Messaging mechanism detailed _c language

Source: Internet
Author: User

For Windows programming, here are a few key words to note: message, message loop, window process.

The so-called Windows Messaging mechanism is similar to the life of the logistics companies. When the sender (such as mouse, keyboard) to the logistics Company (Windows System), the logistics Company (Windows System) will be collated and distributed (collation and distribution mainly by the message loop completed), to the corresponding courier (window process) to deal with. Courier (Window process) to get the package (message) after a variety of ways to deal with, such as immediately to the recipient, and so on a day to the recipient, or transfer to other express delivery, which needs to be used in the window process to distinguish between the swich/case.

message loop:

When the initialization is complete, the WinMain main function enters the message loop:

while (GetMessage (&msg,...))
{
translatemessage (&msg);//convert keyboard message
dispatchmessage (&msg);//Dispatch message
}

Window procedure:

Lresult CALLBACK WndProc (...)
{
...
Switch (msg)
{case
1:
...
Case 2: ...
Default: ...
}
return 0;
}

The following is a detailed summary of the two questions.

Question 1: What are the classification methods for the message?
(1) queue messages and non-queue messages
the queue message is sent to the system message queue and then to the thread message queue. Windows maintains a system message queue, and each GUI thread also has a thread message queue. These include keyboard and mouse messages, WM_PAINT, Wm_timer, Wm_quit.
The vast majority of messages outside these queue messages are not queue messages.

Note here that messages placed in message queues are in the form of a MSG structure, MSG includes: window handles, message IDs, two parameters (wparam and lparam) of messages, the time of message delivery (POST), and the location of the mouse. In addition, you can use:: GetMessage functions and::P eekmessage functions to save the message information obtained.
non-queue messages are sent directly to the destination window process.

(2) system messages and application messages
System Message ID range 0X8000~0XBFFF
Application Elimination ID range 0XC000~0XFFFF
For the ID uniqueness of an application message, you can use:: RegisterWindowMessage to a specific ID.

Question 2: How are messages delivered, received, and processed?
the input of a Windows application is a window sent by Windows system to the application as a message that receives and processes messages through a window procedure, and then returns control to Windows.

Note here: non-queue messages are sent directly to the window process of the destination window, and the queue message is sent by::D ispatchmessage The window process to the destination window. The window procedure is invoked to receive four parameters: A window handle, a message ID, and two 32-bit message parameters. In the window process, the Switch/case branch processing statements are used to identify and process messages.

After each GDI application is created in the main window, it goes into the message loop, accepting user input, interpreting, and processing messages. The message loop gets the message from the message queue, and if it is not a shortcut message or a dialog box message, the message is converted and distributed to allow the window process of the destination window to be processed. Exits the message loop when a message wm_quit, or: getmessage error occurs.

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.