Win32 SDK Basics (12) Processing of WM_PAINT message (picture)

黄舟
Release: 2017-06-06 10:11:46
Original
7732 people have browsed it

1. Introduction

In a computer, almost everything displayed on the screen is drawn, including windows, dialog boxes,pictures, and all text, and the WM_PAINT message is Message triggered by the system when drawing theseobjects. Almost every operation we perform on the computer will trigger this message, and it is also one of the most important messages in Windows. This article focuses on experimenting with this message for a comprehensive study.

2. Basics of WM_PAINT

2.1 Macro definition

#define WM_PAINT 0x000F
Copy after login

2.2 Carrying parameters

We know that when using sendmessage/ When postmessagesends a message, it often carries two parameters, WPARAM and LPARAM, and when using GetMessage or PeekMessageto receive a message, these two parameters will also be received. Some of these messages will carry some necessary information in parameters, such as mouse position, window length and width, etc. These two parameters of WM_PAINT are empty and carry no message.

2.3 Trigger timing

In order to obtain the trigger timing of this message, we first create a Win32 window project as the test object. The window processingfunctionis defined as follows:

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 break; case WM_PAINT: { WriteConsole(hOutput,"WM_PAIN\n",10,NULL,NULL); } break; default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
Copy after login

In our WM_PAINT processing statement, astring of "WM_PAINT\n" is written to the standard output of the console., to verify that the WM_PAINT message is triggered. Let's verify the triggering timing of WM_PAINT respectively:

1. When the program starts, it triggers when the window is drawn.

When we start the program, because we need to draw the window, the WM_PAINT message will be triggered, and the above string will be printed:


2. When the window is resized with the mouse, it will be triggered continuously:

Since the window needs to be redrawn continuously when the window is resized, what is shown at this time is continuous triggering. WM_PAINT message:


3. The WM_PAINT message will not be triggered when minimizing, but will be triggered when restoring from minimization

The picture below is from the process of minimizing to restoring the window twice. You can see that two more strings are printed


4. The WM_PAINT message will be triggered when maximizing

When the picture is maximized and restored, the WM_PAINT message will be triggered once, as shown in the following figure:


5. When the window is dragged outside the screen, the WM_PAINT message will not be triggered, but when it is pulled back into the screen, the WM_PAINT message will be triggered continuously

The screenshot below shows that when the window is pulled back to the screen, the window is constantly redrawn, triggering the WM_PAINT message.


#6. Use the InvalidateRect function to trigger the WM_PAINT message

The function prototype of InvalidateRect As follows, each call will trigger a WM_PAINT message:

BOOL InvalidateRect( HWND hWnd, // handle of window with changed update region CONST RECT *lpRect, // address of rectangle coordinates BOOL bErase // erase-background flag );
Copy after login

hWnd: The handle of the form where the client area whereis to be updated is located. If it is NULL, the system will redraw all windows before the function returns, and then send WM_ERASEBKGND and WM_PAINT to the window procedure processing function.lpRect: The rectangular representation of the invalid area. It is a
structure pointerthat stores the size of the rectangle. If NULL, the entire window client area will be added to the update area.bErase: Indicates whether to redraw the area after the invalid rectangle is marked as valid. Use a predefined brush when redrawing. Redraw is required when TRUE is specified.
Return value:
The function returns a non-zero value if successful, otherwise it returns a zero value.

In order to verify the function of the InvalidateRect function, we need to add a processing of the WM_LBUTTONDOWN message in the window processing function, and call InvalidateRect every time the left mouse button is clicked:

//窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 break; case WM_PAINT: { WriteConsole(hOutput,"WM_PAIN\n",10,NULL,NULL); } case WM_LBUTTONDOWN: { InvalidateRect(hWnd,NULL,true); } break; default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
Copy after login

下图为执行结果,点击了3次鼠标左键,触发了3次WM_PAINT消息。

总结:

触发WM_PAINT消息的本质是改变窗口对应的显存的大小就触发一次,我们进行的每一次窗口最大化、最小化并恢复都是因为改变了窗口的显存而触发了该消息。在我们向屏幕外面拖动窗口时,这点比较特殊,窗口的显存是在一点点被擦除的,此时不会触发WM_PAINT,但是拉回窗口后,显存需要将擦除的部分重新绘制,这就又会触发一次该消息。而InvalidateRect函数,就是通过强制的清除并重绘显存来实现触发WM_PAINT消息。

三、WM_PAINT消息的处理

我们尝试处理WM_PAINT消息,并在窗口上绘制一个矩形,绘图步骤如下:

1、开始绘图处理

HDC BeginPaint( HWND hwnd,//绘图窗口 LPPAINTSTRUCT lpPaint );
Copy after login

我们利用BeginPaint获取绘图设备的句柄---一个HDC对象,然后在改绘图设备上进行绘制。

2、利用HDC对象进行绘图

3、结束绘图处理

Bool EndPoint( HWND hWnd, CONST PAINTSTRUCT *lpPaint );
Copy after login

绘制过程参考下面的代码:

//窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 break; case WM_PAINT: { PAINTSTRUCT pt; HDC hdc; hdc=BeginPaint(hWnd,&pt); Rectangle(hdc,0,0,100,100); EndPaint(hWnd,&pt); } case WM_LBUTTONDOWN: { //InvalidateRect(hWnd,NULL,true); } break; default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
Copy after login

执行结果如下,我们成功绘制了一个矩形:


The above is the detailed content of Win32 SDK Basics (12) Processing of WM_PAINT message (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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!