


Win32 SDK Basics (10) Detailed explanation of several common Windows message processing sample codes
Introduction
This article mainly introduces the processing of several common messages in Windows. In "Win32 SDK Basics (8) - Windows Message Mechanism", we introduced the processing of WM_CREATE messages. In the window creation Previously, we used the message processing function to pop up a MessageBox. Based on this, this article introduces WM_DESTROY, WM_SYSCOMMAND, WM_QUIT, WM_SIZE and other other common Windows messages. First, we introduce the SDK Basics (8) - Windows Message Mechanism" is the code in the article, and subsequent experiments are based on this code.
#include "stdafx.h" #include "MessageTs.h" HINSTANCE g_hInstance = 0; //窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 break; case WM_CREATE: MessageBox(NULL,"WM_CREATE消息被处理了","消息处理",MB_OK); default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } //注册窗口类 BOOL Register(LPSTR lpClassName, WNDPROC wndProc) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof(wce); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInstance; wce.lpfnWndProc = wndProc; wce.lpszClassName = lpClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW | CS_VREDRAW; ATOM nAtom = RegisterClassEx(&wce); if (nAtom == 0) return FALSE; return true; } //创建主窗口 HWND CreateMain(LPSTR lpClassName, LPSTR lpWndName) { HWND hWnd = CreateWindowEx(0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL); return hWnd; } //显示窗口 void Display(HWND hWnd) { ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); } //消息循环 void Message() { MSG nMsg = { 0 }; while (GetMessage(&nMsg, NULL, 0, 0)) { TranslateMessage(&nMsg); DispatchMessage(&nMsg); } } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { // TODO: Place code here. g_hInstance = hInstance; BOOL nRet = Register("Main", WndProc); if (!nRet) { MessageBox(NULL, "注册失败", "Infor", MB_OK); return 0; } HWND hWnd = CreateMain("Main", "window"); Display(hWnd); Message(); return 0; }
2. WM_CREATE message
We still want to introduce the WM_CREATE message. Because in the previous article we only verified the timing of the WM_CREATE message by popping up the MessageBox, but did not introduce its other very important component-WPARAM and LPARAM parameters. When we send a message, we can often carry some information through these two parameters. The WM_CREATE message is a message automatically sent by the system when we create a window, and these two parameters are also used to carry information. LPARAM carries the 12 parameter information of CreateWindowEx in which we created the window. WPARAM is not used. Next, when we process the WM_CREATE message, we will display the window class and window name on the pop-up dialog box.
//窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 break; case WM_CREATE: { CREATESTRUCT crt = *((CREATESTRUCT*)lParam); char buf[256] = {0}; sprintf(buf,"创建的窗口类名称是%s,窗口名称是%s",crt.lpszClass,crt.lpszName); MessageBox(NULL, buf, "消息处理", MB_OK); } default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
Running the program, we found that we successfully captured the registered window class name and window name of the window before the window was created, and displayed it on the dialog box:
3. WM_DESTROY message
We have already solved this message in the previous article. It is a message sent when the Windows window is closed. To put it simply, when you click the close button of the Windows window, this message will be sent. In our previous code, the operation to process this message was PostQuitMessage(0). This actually means that we sent a WM_QUIT message to be introduced next to end the program process. Neither LPARAM nor WPARAM of the WM_DESTORY message is used. It is generally used to do some resource recycling and memory release before the window is closed. The PostQuitMessage(0) we call is a very common usage.
4. WM_QUIT message
As we have already introduced before, this is a message we send using PostQuitMessage(0) to end the program process. Its WMPARAM is the parameter passed in PostQuitMessage , the LPARAM parameter is not used. Normally, after we send WM_QUIT, it will cause the GetMessage function in the message loop to return, causing the process to exit . The WM_QUIT message cannot be passed to our own window processing function. But we can verify that there is such a message from the side: We first add processing of the WM_QUIT message in the code.
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 break; case WM_CREATE: { CREATESTRUCT crt = *((CREATESTRUCT*)lParam); char buf[256] = {0}; sprintf(buf,"创建的窗口类名称是%s,窗口名称是%s",crt.lpszClass,crt.lpszName); MessageBox(NULL, buf, "消息处理", MB_OK); } case WM_QUIT: { int param = (int)wParam; char buf[256]; sprintf(buf, "进程退出,退出码:%d", param); MessageBox(NULL, buf, "消息处理", MB_OK); } default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
Running the program, after closing the dialog box for processing the WM_CREATE message, we found that the dialog box for us to verify the WM_QUIT message popped up.
This is because the pop -up processing wm_create message MESSAGEBOX is also a window, but it belongs to our process. Each window has a message loop, but there is only one message queue per process. When the MessageBox window is closed, its window processing function also calls PostQuitMessage(0) to send a WM_QUIT message. In its message loop, GetMessage returns and exits after receiving the WM_QUIT message. This message will be captured by our message loop, but since it is not a message sent by our window, it will not cause our GetMessage to return to end the loop. Instead, it will be passed to our window processing function for summary processing, so it appears. The dialog box shown in the screenshot above appears.
5. WM_SYSCOMMAND message
System command message, this message will be triggered when we click the maximize, minimize and close commands (so when we click the close button, WM_DESTROY and WM_SYSCOMMAND will be triggered at the same time) Two pieces of news). Its wParam parameter carries the specific window operation:
Close: SC_CLOSE
Maximize: SC_MAXIMIZE
Minimize: SC_MINIMIZE
这里只列举了三种常见的WM_SYSCOMMAND携带的参数宏,其它的可以参照MSDN。WM_SYSCOMMAND的lParam携带的是产生该消息的鼠标的位置,位置的X和Y坐标分别被存放在lParam的低位和高位字中,我们用下面的代码来验证在窗口最大化时,我们鼠标的位置:
case WM_SYSCOMMAND: { if (wParam == SC_MAXIMIZE) { short x = LOWORD(lParam); short y = HIWORD(lParam); char buf[256]; sprintf(buf, "窗口最大化,x坐标:%d,y坐标:%d", x,y); MessageBox(NULL, buf, "消息处理", MB_OK); } }
当我们点击窗口的最大化按钮时,出现下面的提示:
六 WM_SIZE消息
每当我们调整窗口的大小时,都会触发WM_SIZE消息。它的wParam参数携带的是该消息产生的原因:
SIZE_RESTORED —— 重新调整窗口大小
SIZE_MAXIMIZED —— 最大化显示
SIZE_ MINIMIZED —— 最小化显示
其他参数宏详见MSDN
它的lParam参数携带的是重新调整大小后的窗口的高和宽,其中低字节代表宽,高字节代表高,这里我们通过代码验证,当窗口最大化后窗口的高和宽:
case WM_SIZE: { if (wParam == SIZE_MAXIMIZED) { short width = LOWORD(lParam); short hight = HIWORD(lParam); char buf[256]; sprintf(buf, "窗口最大化,高度:%d,宽度:%d", hight, width); MessageBox(NULL, buf, "消息处理", MB_OK); } }
运行程序,最大化时可以显示最大化后的窗口高度和宽度:
The above is the detailed content of Win32 SDK Basics (10) Detailed explanation of several common Windows message processing sample codes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The MySQL connection may be due to the following reasons: MySQL service is not started, the firewall intercepts the connection, the port number is incorrect, the user name or password is incorrect, the listening address in my.cnf is improperly configured, etc. The troubleshooting steps include: 1. Check whether the MySQL service is running; 2. Adjust the firewall settings to allow MySQL to listen to port 3306; 3. Confirm that the port number is consistent with the actual port number; 4. Check whether the user name and password are correct; 5. Make sure the bind-address settings in my.cnf are correct.

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

Unable to access MySQL from the terminal may be due to: MySQL service not running; connection command error; insufficient permissions; firewall blocks connection; MySQL configuration file error.

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Pull vertical guides in Photoshop: Enable ruler view (View > ruler). Hover the mouse over the vertical edge of the ruler, and then the cursor becomes a vertical line with double arrows and hold and drag the mouse to pull out the reference line. Click Delete by dragging the guide, or hovering it into a cross.

MySQL download prompts a disk write error. The solution is as follows: 1. Check whether the disk space is insufficient, clean up the space or replace a larger disk; 2. Use disk detection tools (such as chkdsk or fsck) to check and fix disk errors, and replace the hard disk if necessary; 3. Check the target directory permissions to ensure that the user account has write permissions; 4. Change the download tool or network environment, and use the download manager to restore interrupted download; 5. Temporarily close the anti-virus software or firewall, and re-enable it after the download is completed. By systematically troubleshooting these aspects, the problem can be solved.
