Windows program design (2): wm_paint Message Details

Source: Internet
Author: User

When will the wm_paint message be triggered?

The following content is from the famous "Windows Programming (fifth edition)"

Most Windows programs call the updatewindow function during initialization before entering the message loop in winmain. Windows uses this opportunity to send the first wm_paint message to the window message handler. In this message Notification window, the message handler must draw a display area. After that, the window message processing program should be ready to process other wm_paint messages at any time, and, if necessary, re-draw the entire display area of the window. When the following events occur, the window message handler receives a wm_paint message:

  • When a user moves a window or displays a window, the area hidden before the window is visible again.
     
  • The user changes the window size (if the window type style has cs_hredraw and cs_vredraw flag settings ).
     
  • The program uses the scrollwindow or scrolldc function to scroll part of the display area.
     
  • The program uses the invalidaterect or invalidatergn function to deliberately generate the wm_paint message.
     

In some cases, part of the display area is temporarily overwritten. Windows tries to save a display area and restore it later, but this may not succeed. Windows may send the wm_paint message in the following cases:

  • Windows erasure overwrites the dialog box or message box of some windows.
     
  • Drop-down menu and release it.
     
  • Displays a tooltip message.
     

In some cases, windows always saves the covered display area and restores it. These situations are:

  • Move the cursor over the display area.
     
  • The icon is dragged across the display area.

 

 

But in fact, the program written in win7 may encounter the following situation:

The wm_paint message is triggered when the program window is maximized. When the window is moved, the wm_paint message is triggered sometimes, but not when other Windows block the program window, the wm_paint message will not be sent. Why?

In fact, this is related to Windows 7 settings. In the "performance information and tools" column of the "control panel", select "Adjust visual effects" and remove "enable desktop Combinations, you can.

 

When the wm_paint message is triggered, the system prepares to process the entire region. In fact, the system only redraws the invalid partition. For example, when you remove window a from window B, it is not the whole re-painting window B, but the other part of B is not moving, just re-drawing the leaked part. If you want to redraw the entire image, you need to use invalidaterect (hwnd, null, true) to indicate that the entire customer zone is invalid.

 

How can we use the wm_paint message?

Windows plotting is completed through GDI (Graphic device interface. To put it simply, GDI is composed of: DC (device description table), GDI functions, basic GDI graphics, and some other things. To use a GDI drawing, you must obtain the DC. Because DC is related to a specific display device ). Only when DC is obtained can the image be correctly output. For example, a grating device (in simple terms, it is displayed in pixels) and a vector device (when displayed, it tells him the start and end points, it will automatically connect to you) the imaging principles are different, so only when you know their imaging principles (inside DC) Can you draw a picture.

There are many ways to get the DC, but in the wm_paint message, you can only use beginpaint to get the HDC, and then use endpaint to release it. Write the GDI function between the two. Even if you do not do anything in the wm_paint message, you have to write these two sentences. The reason is that wm_paint causes the background of the invalid region to be erased.

 

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.