Background knowledge of Windows program development---Windows Messaging mechanism

Source: Internet
Author: User

One, the news

Message refers to a notification that the Windows operating system sends to an application that tells the application that a particular event has occurred. For example, a user clicking a mouse or pressing a button causes the Windows system to send the appropriate message. The final processing of the message is the window function of the application, if the program is not processed, the operating system has a default function will be processed.
From a data structure perspective, a message is a struct that contains the type identifier of the message and some additional information. A system-defined struct-body msg is used to represent a message, and MSG has the following defined form:
typedef struct MSG
{
HWND hwnd; An HWND is a handle to a window that determines which window procedure function handles the message
UINT message; A message is a constant that represents the type of message
WPARAM WPARAM; 32-bit additional information, specific to what content, depending on the type of message
LPARAM LPARAM; 32-bit additional information, specific to what content, depending on the type of message
DWORD time; Time is when the message was sent
Point pt; Where the mouse is located when the message is sent
}  two, Windows programming principles      windows is a message-driven system that provides Windows Messaging between applications and applications, applications and Windows Means of communication between systems. The functionality that the application implements is triggered by the message and is done by responding to and processing the message. There are two types of Message Queuing in Windows systems, one is system Message Queuing and the other is application Message Queuing. All of the computer's input devices are monitored by Windows, and when an event occurs, Windows places the incoming message in the system message queue and then copies the incoming message to the appropriate application queue. A message loop in an application retrieves each message from its message queue and sends it to the corresponding window function.              Arrows 1 shows that the operating system can manipulate input, such as letting printers print;      Arrows 2 Note that the operating system can perceive input state changes, such as mouse clicks, keystrokes, and so on, which is the interaction between the operating system and the computer hardware       Arrow 3 is an application that notifies the operating system to perform a specific operation, which is done by invoking the operating system's API To implement the;      Arrows 4  operating system can sense the state of the hardware changes, but does not decide what to do, but instead of wrapping this change into a message to the application, the application decides what to do, and the arrow 4 illustrates the transfer situation.   Three, Windows message loop       message loop is the root of the existence of Windows applications, the application through the message loop to get a variety of messages, and through the corresponding window procedure function, the message is processed It is this message loop that enables an application to respond to various external events.          The  windows operating system maintains a message queue for each thread, and when an event occurs, the operating system senses the occurrence of the event and wraps it into a message queue, The application obtains the message through the GetMessage () function in a message structure, and then interprets and distributes the message through a translatemessage () and DispatchMessage (), which describes the Windows message loop.

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
TranslateMessage (&msg) does not work for most messages, but some messages, such as keyboard presses and bounces (for KeyDown and KeyUp messages respectively), need to be interpreted to produce a WM_CHAR message. DispatchMessage (&MSG) is responsible for distributing the message to the corresponding window in the message structure, which is processed by the window procedure function. The return value of GetMessage () before getting wm_quit is true, meaning that only the wm_quit message is retrieved to return false in order to jump out of the message loop.

For a Windows Forms program, the following code is in the main function in the Program.cs file: Application.Run (New Form1 ()); This line of code means to start running a standard application message loop on the current thread and make the specified form visible. is actually starting an infinite loop of getmessage. While the thread that responds to Windows messages is called the Windows Forms program UI thread Four, the main function difference between SendMessage () and PostMessage () is used to send messages to the application. SendMessage () joins the message directly into the application's message queue and returns when the application finishes processing the message. Postmessagex () joins the message directly into the application's message queue and exits without waiting for the program to return. PeekMessage and GetMessage are used to extract data from the message queue, getmessage to get a message from the message queue and place it in the specified structure, and the thread will delete the message from the message queue after the message is successfully obtained. The GetMessage function waits until a message arrives to return a value. The PeekMessage function checks the message queue and places the message (if it exists) in the specified structure and then returns, without waiting.

Background knowledge of Windows program development---Windows Messaging mechanism

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.