Table of Contents
1.1 Message Queue
1.2 Types of message queues
1.3 Queue messages and non-queue messages
2. Message loop and GetMessage/PeekMessage
2.1 Message loop
2.2 GetMessage and PeekMessage
" >2.3 GetMessage/PeekMessage获取消息的过程
endMessage和PostMessage" >三、SendMessage和PostMessage
Home Operation and Maintenance Windows Operation and Maintenance Win32 SDK Basics (11) Detailed explanation of message queue and GetMessage/PeekMessage, SendMessage/Postmesage

Win32 SDK Basics (11) Detailed explanation of message queue and GetMessage/PeekMessage, SendMessage/Postmesage

Jun 06, 2017 am 10:09 AM

1. MessageQueue

1.1 Message Queue

Message queue is a queue used to store messages. Messages are first in, first out in the queue. All windows All programs have a message queue, and the program can obtain messages from the queue.

1.2 Types of message queues

System message queue: A message queue maintained by the operating system, which stores messages generated by the system, such as mouse and keyboard messages, etc.

Program message queue: A message queue belonging to each application (thread), maintained by the application.

When a mouse, keyboard, etc. message is generated, the message is first stored in the system message queue, and then the operating system finds the message queue of the corresponding window based on the stored message, and delivers the message to the window's message queue.

1.3 Queue messages and non-queue messages

Queue messages: After the message is sent, it is first put into the queue and then obtained through the message loop. Common queue messages: keyboard, mouse, timer messages, etc.

Non-queue message: After the message is sent, directly find the message processing function of the window , and call the message processing function for processing without going through the message queue. Common non-queue messages: WM_PAINT, WM_SIZE, etc.

2. Message loop and GetMessage/PeekMessage

2.1 Message loop

The general message loop is as follows:

void Message(HWND hWnd)  
{  
	MSG nMsg = { 0 };
	while (GetMessage(&nMsg, hWnd, 0, 0))  
	{  
		TranslateMessage(&nMsg);  
		DispatchMessage(&nMsg);  

		if(nMsg.message == WM_PAINT)
		{
			char buff[30]={};
			sprintf(buff,"处理消息%d\n",nMsg.message);
			WriteConsole(hOutput,buff,sizeof(buff),NULL,NULL);
		}
	}  
}
Copy after login

GetMessage/PeekMessage: Message from the program Get messages from the queue.

TranslateMessage: Translate messages such as keystrokes on the keyboard into character messages.

DispatchMessage: Put the translated message into the program’s message queue again.

2.2 GetMessage and PeekMessage

GetMessage(
LPMSG lpMsg,
HWND hWnd,
UINT wMsgFilterMin,
UINT wMsgFilterMax
)
Copy after login

lpMsg: Pointer to the MSG structure, which receives message information from the thread's message queue.
hWnd: The handle of the window whose message is obtained. When its value is NULL, GetMessage retrieves messages for any window belonging to the calling thread, and the thread message is sent to the calling thread through PostThreadMessage.
wMsgFilterMin: An integer that specifies the minimum message value to be retrieved.
wMsgFilterMax: An integer specifying the maximum message value to be retrieved.
Return value: If the function obtains other messages other than WM_QUIT, it returns a non-zero value. If the function gets a WM_QUIT message, the return value is zero. If an error occurs, the return value is -1. For example, when hWnd is an invalid window handle or lpMsg is an invalid pointer. If you want to get more error information, please call the GetLastError function.

BOOL PeekMessage(
LPMSG IpMsg,
HWND hWnd,
UINT wMSGfilterMin,
UINT wMsgFilterMax,
UINT wRemoveMsg
);
Copy after login

lpMsg: MSG structure pointer to receive message information.
hWnd: The window handle whose message is checked.
wMsgFilterMin: Specifies the first message in the message range to be checked.
wMsgFilterMax: Specifies the last message in the message range to be checked.
wRemoveMsg: Determines how the message is processed. This parameter can take one of the following values:

Value

Meaning

PM_NOREMOVE

After PeekMessage is processed, the message is not removed from the queue.

PM_REMOVE

After the PeekMessage is processed, the message is removed from the queue.

PM_NOYIELD

This flag causes the system not to release threads waiting for the calling program to become idle. PM_NOYIELD can be freely combined into PM_NOREMOVE or PM_REMOVE.


GetMessage和PeekMessage的主要区别在于:GetMessage是阻塞函数,它会在消息循环中会一直阻塞直到消息队列中出现了消息可以被获取,而PeekMessage是非阻塞函数,不管有没有获取到消息队列中的消息,它都会返回。PeekMessage更多用来检测消息队里中是否有消息,它的最后一个参数可以用来指定获取到消息后要不要把消息从消息队列中移除,通常情况下通过PeekMessage检测到消息队列有消息之后,再调用GetMessage区获取。

2.3 GetMessage/PeekMessage获取消息的过程

1、先在程序的消息队列中查找消息,如果有队列消息,就取出消息。

2、如果程序的消息队列中没有消息,向系统的消息队列获取属于本程序的消息。如果系统的消息队列中有属于本程序的消息,系统的消息队列会将消息分发到本程序的消息队列中。

3、如果系统的消息队列也没有消息,检查窗口需要绘制的区域是否需要重绘,如果发现有需要重绘的区域,产生WM_PAINT消息。

4、如果没有重新绘制区域,检查是否具有到时的定时器,如果有产生WM_TIMER定时器消息。

5、如果没有到时的定时器,整理程序的资源、内存等等。

三、SendMessage和PostMessage

LRESULT SendMessage(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM IParam
)
Copy after login

hWnd:其窗口程序将接收消息的窗口的句柄。如果此参数为HWND_BROADCAST,则消息将被发送到系统中所有顶层窗口,包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口。
Msg:指定被发送的消息。
wParam:指定附加的消息特定信息。
IParam:指定附加的消息特定信息。

返回值:返回值指定消息处理的结果,依赖于所发送的消息。

BOOL WINAPI PostMessage(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Copy after login

hWnd:其窗口程序接收消息的窗口的句柄。可取有特定含义的两个值:
HWND_BROADCAST:消息被寄送到系统的所有顶层窗口,包括无效或不可见的非自身拥有的窗口、 被覆盖的窗口和弹出式窗口。消息不被寄送到子窗口
NULL:此函数的操作和调用参数dwThread设置为当前线程的标识符PostThreadMessage函数一样
Msg:指定被寄送的消息。
wParam:指定附加的消息特定的信息。
LParam:指定附加的消息特定的信息。
返回值:如果函数调用成功,返回非零,否则函数调用返回值为零

1、SendMessage

       发送消息到指定的窗口,并等候对方将消息处理,为阻塞函数,获取消息的执行结果后返回。主要需要发送非队列消息,发送的消息不经过消息队列。

2、PostMessage

        发送消息到程序的消息队列,不管消息有没有被处理都会立即返回,用于队列消息的发送

The above is the detailed content of Win32 SDK Basics (11) Detailed explanation of message queue and GetMessage/PeekMessage, SendMessage/Postmesage. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Microsoft releases fix for Behavior:Win32/Hive.ZY error in Windows Defender Microsoft releases fix for Behavior:Win32/Hive.ZY error in Windows Defender Apr 28, 2023 pm 04:01 PM

A Microsoft official confirmed widespread reports that Google Chrome, ChromiumEdge, Discord and several other applications were flagged as "Behavior:Win32/Hive.ZY" by Microsoft's built-in antivirus software "WindowsDefender". The tech giant confirmed in a statement that it is working on a fix that will be rolled out to everyone in the next few hours. So what exactly is "Behavior:Win32/Hive.ZY"? According to a document posted on Microsoft's security portal, any file marked "Behavior:Win32/Hive.ZY" is

What's new in Microsoft Store on Windows 11? What's new in Microsoft Store on Windows 11? Apr 28, 2023 pm 06:13 PM

Microsoft is putting a lot of new features into the Microsoft Store. These will benefit both end users and developers as the company continues its efforts to make the Store better for everyone who uses Windows. One of the biggest highlights of these changes will be the new "Restore Apps" feature in Microsoft Store on Windows 11. It will be connected to the user's Microsoft account, so after logging into a new Windows 11 PC, they can automatically restore the application with just a few clicks. "To make it easier for customers to quickly and seamlessly transition to their new PCs, we will soon be introducing

How to fix Windows Defender behavior: Win32/Hive.ZY alert How to fix Windows Defender behavior: Win32/Hive.ZY alert May 06, 2023 am 08:04 AM

Many Windows 11 and 10 users are troubled by seeing warning notifications from Windows Defender stating that the threat "Behavior: Win32/Hive.ZY" has been detected. According to reports, this Windows Defender warning or alert is triggered when users try to open some commonly used applications such as Google Chrome or Chromium Edge, Whatsapp, Discord, and Spotify. Even if you have blocked this threat on your PC, it will pop up with a message MicrosoftDefenderAntivi the next time you open this affected application

Win32 graphic design software Canva launches free AI tool to challenge giant Adobe Win32 graphic design software Canva launches free AI tool to challenge giant Adobe Mar 05, 2024 pm 04:34 PM

According to recent news, Canva has launched a series of AI image tool functions in an attempt to compete with Adobe's Firefly. It aims to simplify the content creation process in the workplace, making it easier and more efficient for non-design professionals to create content, and allowing graphic designers to Focus on mission-critical tasks and challenge Adobe's dominance in the market. According to reports, Canva’s Visual Worksuite has integrated the new BrandHub, which aims to provide users with a set of tools to help them ensure consistency with their organization’s visual identity. Users can use this tool to create a BrandKit (brand IP toolkit) that contains company cultural information (such as LOGO, fonts, colors and unified design style).

See all articles