Home > Operation and Maintenance > Windows Operation and Maintenance > Win32 SDK Basics (8) Detailed explanation of Windows message mechanism (picture)

Win32 SDK Basics (8) Detailed explanation of Windows message mechanism (picture)

黄舟
Release: 2017-06-06 10:02:15
Original
4058 people have browsed it

1. What is a message

Before explaining what a message is, let’s first discuss the execution mechanism of the program. Generally speaking, programs can be divided into two categories according to their execution mechanisms:
The first category is process driver. For example, the C program we wrote when we first came into contact with programming, or the microcontroller program. This type of program often has the execution process set in advance, and when we execute it, we just execute it step by step;
The second type is event driven. I believe everyone can understand this incident. The occurrence of each event is random, and each event will have its own moment, similar to events in life. Events in the program will also have their own trigger points. The event-driven program has written the processing flow for each event in advance. In the Windows operating system, messages are events in Windows. Almost every operation in Windows will trigger a message. As we said before, creating a window will trigger a WM_CREATE message, and drawing a window will trigger a WM_PAINT message. When we click the mouse or keyboard, the corresponding message will be triggered.
    Windows messages are encapsulated into a structure called MSG, whose prototype is as follows:

typedef struct tagMSG { // msg 
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG;
Copy after login

Hwnd - the handle of the window that triggered the message.
Message - Message ID. The Windows operating system assigns a message ID to each message, and this ID is unique. The essence of WM_CREATE we mentioned above is an integer, which is the message ID.
wParam - Parameters that can be attached to the message.
lParam - Parameters that can be attached to the message.
Time - The moment when the message occurred.
Pt - The position of the mouse when the message occurred.
The above parameters are indispensable for the message.
In Windows, messages are encapsulated into MSG objects. When sending messages, these objects are placed in the messagequeue; when getting messages, These MSG objects are also obtained.

2. Message acquisition

2.1 Message queue

We have said that almost every operation in Windows will trigger a message, and these messages will be sent to in the message queue. What is a message queue? We can understand it as using a first-in-first-out Deque to store Msg objects - Deque. There are two types of message queues, one is the system message queue, and the other is the process message queue. After we trigger the message, the message first enters the system message queue. After processing, the operating system will allocate the message to our program's own message queue based on the window handle hwnd value of the message, and then process the message within our program.

2.2 MessageLoop

In the previous article, we once wrote a message loop. The so-called message loop is to continuously read the messages in the message queue in our process and then process them.

void Message()  
{  
    MSG nMsg = { 0 };  
    while (GetMessage(&nMsg, NULL, 0, 0))  
    {  
        TranslateMessage(&nMsg);  
        DispatchMessage(&nMsg);  
    }  
}
Copy after login


## Here, GetMessage() continues To capture messages in the message queue, its

function prototype is as follows: GetMessage (LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
lpMsg - MSG type used to store messages pointer.
hWnd - Specifies the handle of the window from which the message is obtained. When its value is NULL, GetMessage retrieves messages for any window belonging to the calling thread.
wMsgFilterMin - An integer specifying the minimum message value to be retrieved.
wMsgFilterMax - An integer specifying the maximum message value to be retrieved.

After GetMessage() obtains the message, TranslateMessage will translate the message, mainly converting the virtual key message into a character message. The character message is sent to the calling thread's message queue and is read out the next time the thread calls the function GetMessage or PeekMessage. Each keyboard key in Windows corresponds to a macro, and the message sent by this keyboard key is a virtual key message. The function of TranslateMessage is to convert virtual key messages into character messages WM_CHAR, WM_SYSCHAR, etc.

3. Message processing

The function of DispatchMessage is to dispatch the message to the window processing function defined by our implementation for processing. The following is what we defined in the previous article Window processing function:


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)  
{  
	switch (uMsg)  
	{  
	case WM_DESTROY:  
		PostQuitMessage(0);//可¨¦以°?使º1GetMessage返¤¦Ì回?0  
		break;  
	default:  
		break;  
	}  
	return DefWindowProc(hWnd, uMsg, wParam, lParam);  
}
Copy after login


hWnd就是产生消息的窗口句柄,uMsg是传递的消息,wParam和lParam分别是消息携带的两个参数。在上面的窗口处理函数中,我们定只处理了一个消息WM_DESTROY,这是我们在点击窗口的关闭按钮后产生的一个消息。我们说过,我们在创建窗口是,也会产生一个WM_CREATE消息。下面我们在窗口处理函数中处理这个消息:

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)  
{  
	switch (uMsg)  
	{  
	case WM_DESTROY:  
		PostQuitMessage(0);//可¨¦以°?使º1GetMessage返¤¦Ì回?0  
		break; 
	case  WM_CREATE:
		MessageBox(NULL,"WM_CREATE消息被处理了","消息处理",MB_OK);
	default:  
		break;  
	}  
	return DefWindowProc(hWnd, uMsg, wParam, lParam);  
}
Copy after login


        我们在接受到WM_CREATE后,会弹出一个对话框。预期的效果是点击这个对话框的确定按钮后才会显示窗口。如下面所示:

        运行程序,先弹出对话框:


        点击确定按钮后,弹出窗口:


The above is the detailed content of Win32 SDK Basics (8) Detailed explanation of Windows message mechanism (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template