Chapter One internal running mechanism of Windows Program (3) message and Message Queuing

Source: Internet
Author: User

In Windows, the user program can call the system's API functions, the system will also invoke the user program, this time through the message.

Windows programming is an event-driven approach to programming patterns, primarily based on messages.

"Example" when the user paints in the window, pressing the left mouse button, the operating system will perceive the event, so the event is wrapped into a message, posted to the application's message queue, and then the application from the message queue to take out the message to respond. During processing, the operating system "sends messages" to the application. "Send Message" actually refers to a function in the operating system calling program that is responsible for processing the message, which is called a window procedure .

1. Message

In Windows, messages are represented by the msg struct. The MSG struct is defined as follows:

struct tagmsg {     //  msg      hwnd hwnd;//indicates the window to which the message belongs.   UINT message;// Specifies the identifier of the message   WPARAM WPARAM;    LPARAM LPARAM;    DWORD time;    Point pt; } MSG;

In the structure:

The first member variable HWND represents the window to which a message belongs. The applications we typically develop are window applications, and a message is typically associated with a window.

The second member variable message specifies the identifier of the message. In Windows, messages are represented by a numeric value, and different messages correspond to different values. For ease of memory, Windows defines the value of the message as a WM_XXX macro. For example, the left mouse button pressed message is Wm_lbuttondown. The message pressed by the keyboard is wm_keydown. Character message wm_char and so on.

The third, fourth member variables, wparam, and lparam, are used to specify additional information for a message. The additional information used to specify the message. For example, when we receive a conceited message, the value of the message member variable is WM_CHAR, but what character is entered by the user, as described by wparam and lparam. The information represented by WParam and lparam varies depending on the message.

The last two variables represent the events and the current position of the mouse that the message was posted to in the message queue.

2. Message Queuing

After each Windows application starts executing, the system creates a message queue for the program that holds the window messages created by the program. For example, when the left mouse button is pressed, a WM_LBUTTONDOWN message will be generated, and the message will be placed in the message queue of the application to which the window belongs, waiting for the application to be processed. Windows places the resulting message in the message queue, and the application continuously extracts the message from the message queue and responds with a message loop.

3. Incoming news and no incoming team news

Incoming messages are placed by the system in the application's message queue and then taken out and sent by the application. Do not enter the queue message is sent directly to the window during system Call window procedure. Both incoming and not incoming messages are eventually processed by the Call window procedure function team message.

Chapter One internal running mechanism of Windows Program (3) message and Message Queuing

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.