Window Refresh problem (description of WM_PAINT, BeginPaint, EndPaint)

Source: Internet
Author: User
Tags bool empty message queue

In Windows API programming, WM_PAINT is an important message in the Windows window, and the application completes the drawing of the window by responding to the message.

The WM_PAINT message was generated by the system and should not being sent by a application. The system sends this message when there are No. messages in the application ' s message queue

Note: The WM_PAINT message is generated by the system and is not sent WM_PAINT messages until the application's message queue is empty.

In fact, the system will be in many different mechanisms to send WM_PAINT messages, such as the call UpdateWindow function, the first time to create a window, change the size of the window, maximize, minimize and so on. The production of these actions is controlled by the system, and the application simply receives the message and processes the message.

When Windows detects that a window is covered and needs to be restored, it sends a WM_PAINT message to the user program that includes the area that needs to be recovered, and then the user program decides how to recover the overwritten content. When a window process receives a WM_PAINT message, it does not mean that the entire client area needs to be refreshed, and there is a possibility that the area covered by the client area is only a small piece, which is called an "invalid zone", and the program only needs to update the area. Like the Wm_timer message, the WM_PAINT message is also a low-level message, and although it is not discarded like a wm_timer message, Windows always puts WM_PAINT in when the message loop is empty, and in fact, Windows maintains a "drawing information structure" for each window, and the coordinates of the invalid zone are in it, and every time the message loop is empty, if Windows finds an invalid zone, it puts a WM_PAINT message in.

The coordinates of the invalid zone are not attached to the parameters of the WM_PAINT message, there are other methods available in the program, WM_PAINT message just notifies the program that a zone needs to be updated, so Windows does not put two WM_PAINT messages into the message loop at the same time. When Windows wants to put in a WM_PAINT message, if it finds that an invalid zone already exists, it only needs to combine the two invalid areas of the new and old to compute an invalid zone, or just a WM_PAINT message in the message loop.

If the program does not end after refreshing the client area in the WM_PAINT message, and if the invalid zone is not rendered valid, Windows will continue to place a WM_PAINT message in the next message loop, instead of the program performing the refresh process, so the program can not brush The new client area, but simply using a validaterect function to make the client area effective, "spoofing" Windows has no invalid zones, and when Windows checks the "drawing information structure", it finds no invalid areas and will not continue to send wm_ Paint the news.

So how do you get the "drawing information structure"? The second parameter of the BeginPaint function is the buffer address of a drawing information structure, where Windows returns the structure of the drawing information, where the structure contains the location and size of the invalid area, and the structure of the drawing information is defined as follows:

typedef struct tagPAINTSTRUCT { // ps
     HDC   hdc;
    BOOL fErase;
     RECT rcPaint;
     BOOL fRestore;
    BOOL fIncUpdate;
    BYTE  rgbReserved[32];
} PAINTSTRUCT;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.