Table of Contents
1. What is a message
2. Message acquisition
2.1 Message queue
Loop" >2.2 MessageLoop
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)

Jun 06, 2017 am 10:02 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Windows 11 22H2 brings mica/acrylic design to more Win32 desktop apps Windows 11 22H2 brings mica/acrylic design to more Win32 desktop apps Apr 14, 2023 pm 04:58 PM

Windows 11’s native apps (like File Explorer) and other shell apps use rounded corners and Fluent Design elements (like Acrylic) by default. In addition to rounded corners, another important design feature of Windows 11 is materials like mica, which aligns the background color of apps with the desktop. Mica is similar to acrylic but works slightly differently. As Microsoft describes it, Windows Mica Materials creates "color hierarchy by aligning backgrounds with apps

Microsoft is developing new blur effects for Windows 11 Microsoft is developing new blur effects for Windows 11 May 13, 2023 am 09:04 AM

The new Windows 11 SDK for build 22523 revealed that Microsoft is developing a new blur effect for Windows 11. This effect is called Tabbed, and is in addition to Acrylic and Mica. The new DWMWA_SYSTEMBACKDROP_TYPE in the 22523 SDK, Mica, Acrylic’s public Win32 API and their weird new “tab” mix: pic.twitter.com/dbsu7ZFiIi — It’s All Back (@StartIsBack) December 15, 2021 Available in the following SDK’s Sample application

Trojan/win32.casdet Rfn in Windows 11 Trojan/win32.casdet Rfn in Windows 11 Apr 14, 2023 pm 02:49 PM

Antivirus software may sometimes display a warning stating Trojan/win11.casdet rfn on Windows 32 laptops. It indicates that the PC is infected with Trojan malware, causing it to malfunction. Fortunately, there are some possible ways to fix this problem, as explained below. Additionally, you may be interested in our detailed guide on whether cdn.districtm.io is a pop-up/virus/malware or not. What is Trojan/win32.casdet rfn? Trojan/win32.casdet rfn is a severe Trojan virus infection that can overrun the system and kill its processes, making it easy

What is the difference between win32 and win64 What is the difference between win32 and win64 May 29, 2023 pm 05:22 PM

The difference between win32 and win64 is: 1. win32 refers to the 32-bit environment of the Microsoft Windows operating system, and win64 refers to the 64-bit version of the Microsoft Windows operating system, which is more stable and faster than the 32-bit version; 2. win32 supports up to 2G of memory. win64 must have more than 4G of memory; 3. win64 supports 64-bit processors, but win32 cannot fully support it; 4. win32 pursues simplicity, while win64 pursues performance.

Microsoft begins testing new OneNote design for Windows 11 Microsoft begins testing new OneNote design for Windows 11 Apr 19, 2023 pm 08:01 PM

Back in August 2021, Microsoft promised that OneNote on Windows 10 and Windows 11 would receive a series of major feature updates in the coming months, as the tech giant unifies multiple versions of the note-taking app into a single one. part of the efforts of customers. OneNote has undergone many changes over the past few years. In 2018, when Microsoft really wanted people to use its UWP version of OneNote, the company stopped bundling the original and feature-rich version of OneNote with preinstalled Office apps. Instead, Microsoft has only provided a UWP client and added new features for modern versions. Microsoft later changed

Master the essential skills for secondary development of Java Hikvision SDK Master the essential skills for secondary development of Java Hikvision SDK Sep 06, 2023 am 08:10 AM

Master the essential skills for secondary development of Java Hikvision SDK Introduction: With the rapid development of information technology, video surveillance systems have been widely used in various fields. As the leading domestic video surveillance solution provider, Hikvision’s products and technologies have always occupied an important position in the market. In order to meet the needs of different projects, Hikvision provides SDK for developers to carry out secondary development. This article will introduce some essential skills for mastering the secondary development of Java Hikvision SDK, and attach corresponding code examples. 1. Understand Hikvision

Windows App SDK 1.2 is now online, here's what's new Windows App SDK 1.2 is now online, here's what's new May 12, 2023 pm 06:07 PM

The WindowsAppSDK is a set of tools and APIs that developers can use in their Windows applications to provide "consistent" functionality across a variety of devices using Windows 10 (version 1809 and later) and Windows 11. It's really important to understand that it doesn't replace existing application types like .NET or Windows SDK, it just provides a unified API toolset that can be used to complement your existing applications. Today, Microsoft released version 1.2 of Windows App SDK with many new features. The highlight of this release may be third-party developers

what is sdk what is sdk Jan 06, 2023 pm 03:26 PM

The full name of sdk is "Software Development Kit", which means "software development kit" in Chinese. It is a set of tools provided by manufacturers of hardware platforms, operating systems (OS) or programming languages. SDKs assist software developers in creating applications for specific platforms, systems or programming languages. A basic SDK usually consists of a compiler, debugger, and application programming interface (API), but may also include other content, such as: documentation, libraries, runtime/development environment, testing/analysis tools, network protocols, etc.

See all articles