Wm_paint Message Details

Source: Internet
Author: User

Wm_paint is an important message in windows,

The application passes throughProcessingThe message is drawn in the window.

1. When will the system send the wm_paint message?

  The system will send the wm_paint message at multiple different times: When a window is created for the first time, when the window size is changed and the window is removed from the back of another window, when the window is maximized or minimized, and so on, these actions are managed by the system. The application only passively receives the message and processes the message.FunctionIn most cases, the application also needs to be able to actively trigger the Drawing operation in the window. For example, when the data displayed in the window changes, this is usually done through invalidaterect and
The invalidatergn function.

   Invalidaterect and invalidatergn Add the specified region to the update Region Window. If the application's message queue has no other messages, if the update region of the window is not empty, the system automatically generates the wm_paint message.

   Why does the system not send the wm_paint message when invalidate is called? Why do I have to send the wm_paint message when the application message queue is empty? This is because the system regards the painting operation in the window as a low-priority operation, so we try to push it back.

   However, this will also improve the rendering efficiency: the areas where invalidaterect and invaliatergn invalidate the two wm_paint messages will be accumulated and updated once in a wm_paint message, this not only avoids repeated updates to the same region, but also optimizes the application update operations.

   In this way, invalidaterect and invalidatergn are used to invalidate the window area. The mechanism that relies on the system to send the wm_paint message at the right time is actually an asynchronous method, that is, there is a delay between the Invalid Window area and the wm_paint message. Sometimes this delay is not what we want, in this case, we can use sendmessage to send a wm_paint message after the Invalid Window area to force re-painting immediately, but it is better to use Windows
GDI provides us with more convenient and powerful functions: updatewindow and redrawwindow.

  Updatewindow checks the update region of the window and sends the wm_paint message only when it is not empty. redrawwindow gives us more control: whether to redraw non-customer areas and backgrounds, whether the wm_paint message is always sent, regardless of whether the update region is empty or not.

2. Beginpaint

  Beginpaint and wm_paint messages are closely related. What if you don't write beginpaint in the wm_paint processing function? The program will achieve amazing CPU usage as it enters an endless loop, and you will find that the program is always processing one by one wm_paint message. This is because in general, when the application receives the wm_paint message, the update region of the window is not empty (if it is empty, the wm_paint message does not need to be sent ), one function of beginpaint is to update
If you do not call beginpaint, update region of the window is never empty. As described above, the system will always send the wm_paint message.

   Beginpaint and wm_erasebkgnd messages are also related. When the update region of the window is marked as the background to be erased, beginpaint will send the wm_erasebkgnd message to re-draw the background, at the same time, there is a flag in the returned information indicating whether the background of the window has been re-painted.

   When we use invalidaterect and invalidatergn to add a specified region to update region, we can set whether the region needs to be erased, so that the next beginpaint will know whether to send the wm_erasebkgnd message.

   Note that beginpaint can only be used in the wm_paint processing function.

 

Additional points:

1. wm_paint is a passive message and cannot simply use sendmessage wm_paint in a common method.
This is not acceptable, but it is not impossible to trigger messages by programmers. It can be done through several special constants, but it should be found in Delphi.

2. sendmessage can send messages to the Message Queue. However, Windows automatically determines whether an invalid drawing area exists;
If an invalid drawing area exists, it may be re-painted. Otherwise, the message is discarded.

3. You can use several APIs such as invalidaterect to set any Rectangular areas on the screen as invalid areas. After calling updatewindow, Windows will automatically find whether there is an invalid area and redraw the area.

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.