Day1: Windows message loop mechanism

First distinguish a few concepts

System : specifically refers to the Windows operating system

Application : refers to a program, such as QQ, WeChat, etc.

Window : Each application program can have windows, and there can be multiple windows, but generally there will be one main window.

Message : The window system defines many kinds of messages, for example, clicking the mouse, changing the size of the window, pressing the keyboard, these operations will cause Windows to send a message to the application. The message itself is passed to the application as a record that contains the message type and other information

Message loop : a message mechanism of the window system

Message queue : belongs to the thread, and is a queue created and maintained by the windows system for the thread to store various messages. The system itself maintains a system message queue, and then maintains a thread-specific message queue for each GUI thread.

Thread : Each thread does not have a message queue by default. The system creates a message queue for the thread only when it calls the user interface for the first time (such as creating a window or operating UI elements). An application can have multiple threads, but there can only be one UI thread, which is the main thread by default, and other sub-threads cannot operate the UI and create UI elements. This is specified by windows

Message Loop Diagram

The specific process of the cycle:

first step:

After we create the win32 application program, when the user operates the application program through the mouse and keyboard, since Windows has been monitoring the I/O device, the event will first be converted into a message, captured by the windows system, and stored in the system message queue.

Step two:

The Windows system knows which application program should handle the message, and then copies it to the corresponding application message queue. At the same time, the message is deleted from the system message queue.

third step:

The message loop of the application program is constantly being executed. At this time, call GetMessage() to find the message from the message queue, and if the message is found, GetMessage() will return a positive value and get the message Msg; PS: If the message queue is If empty, the program will stop executing and wait (program blocks).

the fourth step:

Then take out the message (Msg) and pass it to the TranslateMessage() function, which does some extra processing: convert the virtual key-value information into character information. This step is actually optional, but it is required in some places.

Step 5:
After the above steps are executed, pass the message MSG to the DispatchMessage() function. The DispatchMessage() function sends the message to the windows system, and the windows system finds the target window and distributes it to the window, calls the window procedure function corresponding to the message, that is, the WinPro function of the window, and lets the WinPro function process it. The WinPro function allows us to do specific processing for different messages. If we don’t process it, the DefWindowProc function will be called to do the default processing, so why the type of WinPro in the default code is CallBack (callback), because it is not called by us actively. is the system callback

Step six:

Once a message is processed, the window procedure WinPro function returns, and the DispatchMessage() function returns, and the message loop of the application program continues the while loop, and the Window system continues to monitor various messages, repeating the above steps

Guess you like

Origin blog.csdn.net/qq_61553520/article/details/130892857