WM_PAINT message in detail, using InvalidateRect or InvalidateRgn function deliberately generated WM_PAINT messages (WIN7 changes, "Adjust visual effects", "Enable desktop combination" removed)

Source: Internet
Author: User

When will the WM_PAINT message be triggered?

The following is from the famous "Windows Programming (Fifth Edition)"

Most Windows programs call function UpdateWindow during initialization of WinMain before they enter the message loop. Windows takes advantage of this opportunity to send the first WM_PAINT message to the window message handler. This message notifies the window message handler that the display area must be drawn. Thereafter, the window message handler should be ready to process other WM_PAINT messages at any time, if necessary, or even redraw the entire display area of the window. When one of the following events occurs, the window message handler receives a WM_PAINT message:

    • Areas that were previously hidden in the window are re-visible when the user moves the window or displays the window.
       
    • The user changes the size of the window (if the window category style has Cs_hredraw and Cs_vredraw flags set).
       
    • The program uses the ScrollWindow or SCROLLDC function to scroll through a portion of the area.
       
    • The program uses the InvalidateRect or INVALIDATERGN function to deliberately generate WM_PAINT messages.
       

In some cases, a portion of the display area is temporarily overwritten, and Windows tries to save a display area and restore it later, but this does not necessarily succeed. Windows may send WM_PAINT messages in the following situations:

    • Windows erase a dialog box or message box that overrides a portion of a window.
       
    • The menu is pulled out and then released.
       
    • Displays the ToolTip message.
       

In some cases, Windows always saves the display area it covers, and then restores it. These conditions are:

    • The mouse cursor crosses the display area.
       
    • icon to drag over the display area.

In practice, however, a program written under Win7 may encounter the following scenario:

When the program window is maximized, the WM_PAINT message is triggered, and sometimes the WM_PAINT message is triggered when the window is moved, and sometimes it is not triggered, and when the other window obscures the program's window, the WM_PAINT message will not be emitted.

In fact, this is related to your windows7 settings, in the "Performance Information and Tools" section of the Control Panel, select "Adjust visual effects" and "Enable desktop combination" to remove it.

When the WM_PAINT message is triggered, the entire region is prepared for processing. In fact, the system simply redraws the invalid zone. For example, when you move window a away from window B, instead of redrawing window B, the rest of B is not moving, just redrawing the missing part. If you want the entire redraw, you need to use InvalidateRect (hwnd, NULL, TRUE) to indicate that the invalid zone is the entire client area.

So how do you use the WM_PAINT message?

Windows drawing is done through GDI (graphics device interface). Simply put, GDI is made up of: DC (device Description table), GDI functions, GDI basic graphics and some other things. To use GDI drawing, you must get to the DC. Because the DC is associated with a specific display (printing device). Only if you get the DC, you can output the graphics correctly. To give a simple example, a raster device (in a nutshell, a pixel in pixels) and a vector device (when displayed, telling him where to start and end, which will give you automatic connections) have different imaging principles, so you only have to know their imaging principles (in the DC) to draw.

So how to get the DC, the method is many, but under the WM_PAINT message, can only use BeginPaint to get the HDC, use the EndPaint to release it. Write GDI functions between the two. Even if you do nothing under the WM_PAINT news, you have to write these two sentences. The reason is because WM_PAINT causes the background of the invalid zone to be erased.

http://blog.csdn.net/thefutureisour/article/details/7533497

WM_PAINT message in detail, using InvalidateRect or InvalidateRgn function deliberately generated WM_PAINT messages (WIN7 changes, "Adjust visual effects", "Enable desktop combination" removed)

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.