Windows messages (1): queue messages and non-queue messages

Source: Internet
Author: User

From: http://www.cppblog.com/mzty/archive/2006/11/24/15619.html

1. system message queue and application Message Queue

In Windows, there is a system message queue. For every running Windows application, the system creates a "message queue" for it, that is, the application

Ordered message queue, used to store messages in various Windows that may be created by the program. The application contains a piece of code called "message loop", which is used

Retrieve these messages and distribute them to corresponding window functions.


Message Loop

Windows maintains a message queue for each currently executed windows program 」. After an input event occurs, Windows converts the event to

And put the message into the message queue of the program. The program extracts messages from the message queue by executing a program code called "message loop:

While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}

The MSG variable is a MSG-type structure. The MSG type is defined in winuser. h as follows:

Typedef struct tagmsg
{
Hwnd;
Uint message;
Wparam;
Lparam;
DWORD time;
Point pt;
}
MSG, * PMSG;

The point data type is also a structure, which is defined in windef. h as follows:

Typedef struct tagpoint
{
Long X;
Long y;
}
Point, * Ppoint;

Translatemessage (& MSG); transmits the MSG structure to Windows for some keyboard conversion.

Dispatchmessage (& MSG); returns the MSG structure to Windows. Then, Windows sends the message to the appropriate window message handler to make it

. In other words, Windows calls the window message processing program. In hellowin, the window message handler is the wndproc function.

After the message is processed, wndproc is passed back to Windows. In this case, Windows is still in the dispatchmessage call. End

After the dispatchmessage call is processed, Windows returns to the hellowin program and starts the message loop from the next getmessage call.

Appendix: Annotations on the window process and message loop. Transferred from Sun Xin's "windows program internal operating mechanism"

1. wndproc function pointer prototype: typedef lresult (callback * wndproc) (hwnd, uint, wparam, lparam );

The callback function convention is the _ stdcall convention.

2. The translatemessage function is used to convert a virtual key message into a character message. The character message is delivered to the Message Queue of the calling thread, and is taken out when the current getmessage function is called. When we press a character key on the keyboard, the system will generate the wm_keydown and wm_keyup messages. The two additional parameters (wparam and lparam) contain virtual key code, scan code, and other messages. We usually need to get the assixes of a certain character in the program, the translatemessage function can convert the combination of wm_keydown and wm_keyup messages into a wm_char message (the wparam additional parameter of the message contains the ASCII code of the characters ), and deliver the converted new message to the Message Queue of the calling thread. Note: The translatemessage function does not modify the original message. It only generates new messages and delivers them to the message queue.

The dispatchmessage function assigns a message to the window, and the window process function processes the message. Dispatchmessage is actually a message sent back to the operating system. The operating system calls the window procedure function to process (respond) the message ).

The Message Processing Mechanism of Windows applications is shown in:

Figure: Message Processing Mechanism of Windows Applications

(1) The operating system receives a window message from the application and delivers the message to the Message Queue of the application.

(2) The application calls the getmessage function in the message loop to retrieve a message from the message queue. After a message is retrieved, the application can pre-process the message, for example, discard the response to a message or call translatemessage to generate a new message.

(3) The application calls dispatchmessage to send the message back to the operating system. The message is represented by the MSG struct object, which contains the handle of the window for receiving the message. Therefore, the dispatchmessage function can always be correctly transmitted.

(4) The system uses the pointer of the window process function stored by the lpfnwndproc Member of the wndclass struct to call the Window Process and process the message (that is, "The system sends a message to the application ").

The above is the message processing process of Windows applications.

Three-queue message and non-queue message

Messages can be divided into "queue-oriented" and "non-queue-oriented 」. Messages in the queue are put into the program message queue by windows. In the message loop of the program,

Return the message and assign it to the window message processing program. Non-queue messages are directly sent to the window message processing program during windows call window. That is to say, queue-based

Messages are "sent" to the message queue, while messages that are not queue-oriented are "sent" to the window message handler. In any case, the window message handler will obtain

All messages in the window, including queue-based and non-queue-based messages. The window message processing program is the "message center" of the window 」.

A queue message is basically the result of user input., Which is a string generated by a key (such as wm_keydown and wm_keyup messages) or a key.

(Wm_char), wm_mousemove, and wm_lbuttondown.When a queue message is included

Clock message (wm_timer), update message (wm_paint), and exit message (wm_quit).

Non-queue messages are other messages.In many cases, non-queued messages come from calling specific Windows functions. For example, when winmain calls

When createwindow is created, Windows creates a window and sends a wm_create message to the window message processing program during processing. When winmain calls

During showwindow, Windows will send wm_size and wm_showwindow messages to the window message processing program. When winmain calls

During updatewindow, Windows sends the wm_paint message to the window message processing program. The queuing message signal sent when the keyboard or mouse is input.

It appears in a non-queue message. For example, if you select a menu item with a keyboard or mouse, the keyboard or mouse message is queue-based, indicating that the menu item has been selected

Wm_command messages may be non-queue messages.

4. What is the difference between sendmessage () and postmessage?

Both are used to send messages to applications. Postmessage () directly adds the message to the Message Queue of the application program, and exits without returning the program.

Sendmessage () is the opposite. It returns only after the application processes the message. I want to better reflect the relationship between the two functions:

V.What is the difference between the peekmessage function and getmessage function?

The two functions have the following two differences:

1. getmessage will not be returned until there is a suitable message, and peekmessage is just a glimpse of the message queue.

2. getmessage will delete the message from the queue, while peekmessage can set the last parameter wremovemsg to determine whether to retain the message in the queue.

.

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.