Redrawwindow, updatewindow, invalidaterect usage

Source: Internet
Author: User

1. When will the system send the wm_paint message?

The system sends the wm_paint message at multiple times: When a window is created for the first time, when the window size is changed

When the window is removed from the back, 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.

Function. In 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 done through the invalidaterect and invalidatergn functions.

Invalidaterect and invalidatergn Add the specified region to the update Region Window. When the application's message queue has no other

When the update region in 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 wait until the application message queue is empty?

What about the wm_paint message? 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 invalidaterect and invaliatergn between two wm_paint messages make them invalid.

Will be accumulated, and then updated once in a wm_paint message, not only can avoid repeated updates to the same region, but also optimized

Update the application.

In this way, invalidaterect and invalidatergn are used to invalidate the window area, depending on the system to send wm_paint at the appropriate time.

The messaging mechanism is actually asynchronous, that is, there is a delay between the Invalid Window area and the message sent wm_paint; sometimes

This latency is not what we want. In this case, we can use sendmessage to send a wm_paint message in the Invalid Window area.

But it is better to use Windows GDI to provide 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.

More control: whether to re-draw non-customer areas and backgrounds, whether to always send the wm_paint message, 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?ProgramWill be like

When you enter an infinite loop, the CPU usage is astonishing. You will find that the program is always processing the wm_paint Message one by one. This is because

When the application receives the wm_paint message, the update region in the window is non-empty (if it is empty, you do not need to send the wm_paint message

), Beginpaint is used to set the update region to null. If you do not call beginpaint

Update region is never blank. As mentioned 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, and there is a flag in the returned message 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 is

The background 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 area on the screen to an invalid area. After calling the interface after updatewindow, Windows will automatically find whether there is an invalid area and redraw it again;

 

To put it simply:
We know that the content of the drawing window is usually in wm_paint
The message is being processed. This message is triggered by various situations, such as closing a window that overwrites itself. However, for efficiency, wm_paint
Message generation does not mean that the entire window needs to be re-drawn. Considering the example just mentioned, if window B overwrites the upper left corner of window a, window B Closes and window
You will receive a wm_paint
Message, but you only need to draw the part in the upper left corner (for the sake of simplicity, we do not consider the changes in the content of the window itself, such as animation windows ). In the upper left corner, it is exactly "none ".
Valid region ". Therefore, we can explain invalidaterect.
The function adds a rectangle to the invalid area list of the window and tells the window to process the next wm_paint.
This region needs to be re-drawn when the message is sent. By the way, invalidaterect has a sibling function that processes not only a rectangle, but a region, that is
The invalidatergn function is the same.

We also know that in most cases, wm_paint
The message is not sent by our program, but by the window manager (or windows ).
System itself. However, the system still sends this message to ourselves, that is, the updatewindow function. We often see that the classic SDK-style
In the hello World Program, after the main window is created, the showwindow function will be called to display it, followed by an updatewindow
Is to be immediately refreshed after the display of the main window. However, this was the case before 32-bit windows, and now the window manager is smarter than the original, so
The updatewindow function has been weakened and is no longer frequently used. Of course, updatewindow
You can also make some judgments to make sure that the wm_piant message is sent only when the invalid region exists.

For redrawwindow
Function, the implementation is very complicated, but we can ignore the irrelevant part. Generally, if you understand postmessage and sendmessage
The difference, it will be better understood. Generally, wm_paint messages are delivered to the message queue and are processed after getmessage is taken out. While
Redrawwindow
It will forcibly call the callback function of the window for local processing. To put it bluntly, it is the difference between asynchronous and synchronous. This function allows you to specify the part (rectangle or area) to be repainted. Therefore, it is often said that this function has both
The efficacy of the two functions.

Invalidaterect marks an area as invalid, and the system will post wm_paint messages to the window until the area is marked as valid.
Updatewindow is an immediate response to the wm_paint message, which is equivalent to directly calling the window function.
Redrawwindow is equivalent to combining the first two, and there are many parameters to choose from, you can more flexible re-painting.

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.