The difference between Invalidate, RedrawWindow and UpdateWindow

Source: Internet
Author: User

A: When will the message redraw the window occur?

The application sends a WM_PAINT message when it is necessary to update or redraw the appearance of the window. Redraws the window.

Two: Invalidate ()---RedrawWindow ()--UpdateWindow () What are the similarities and differences between the three functions?

Invalidate () is forcing the system to repaint, but not necessarily redrawing it right away. Because invalidate () only notifies the system, the window has become invalid at this time. Force the system to call WM_PAINT, and this message is just a post that puts the message in the message queue. The exposure is redrawn when the WM_PAINT message is executed.

UpdateWindow only sends a WM_PAINT message to the form, and GetUpdateRect (Hwnd,null,true) sees if there is no customer area to draw before sending it, and if not, WM_PAINT is not sent.

RedrawWindow () is a dual feature with Invalidate () and UpdateWindow (). The State of the declaration window is invalid and immediately updates the window, calling WM_PAINT message processing immediately.

Three: Refresh combination

InvalidateRect (hctrl,null,true);

UpdateWindow (Hctrl);
What does the combination of these two functions mean?

InvalidateRect is triggered WM_PAINT event, but is not immediately triggered, general will wait until the end of the current operation is triggered, if necessary immediately trigger, then with the UpdateWindow () to use it. Execute InvalidateRect First, then execute UpdateWindow ().

/************************************************************************************/

Invalidate adds an WM_PAINT message to the message queue with an invalid zone for the entire client area.
UpdateWindow sends a WM_PAINT message directly, and its invalid extents are the invalid extents of WM_PAINT messages (up to one) in the message queue.
The effect is obvious, when invalidate is called, the screen is not necessarily updated immediately, because the WM_PAINT message is not necessarily in the queue header, and the call UpdateWindow causes the WM_PAINT message to execute immediately, bypassing the message queue.
If you want to update the screen immediately after calling invalidate, add the UpdateWindow () statement.
Explanation of MSDN
UpdateWindow
The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT

Message to the window if the window's update region was not empty. The function sends a WM_PAINT

Message directly to the window procedure of the specified window, bypassing the application queue.

If the update is empty, no message is sent.

InvalidateRect
The system sends a WM_PAINT message to a window whenever it update region was not empty and

There is no other messages in the application queue for that window.

The approximate explanation of translation into Chinese is as follows:
UpdateWindow: If there is an invalid zone, then sending a WM_PAINT message to the window processing, not into the message queue queued, immediately refresh the window, otherwise, do nothing.
InvalidateRect: Sets the invalid zone, if the null parameter, sets the entire window to an invalid area. When the message queue for that window of the application is empty, sending a WM_PAINT message (even if the update area is empty). The update area of all invalidaterect in sending a WM_PAINT message is incremented.

1: Set Invalid Zone
InvalidateRect

2: Refresh Now
UpdateWindow ();

If you call UpdateWindow without calling InvalidateRect, then UpdateWindow does nothing.??????
If UpdateWindow is not called after calling InvalidateRect, the system automatically sends a WM_PAINT message when the window message queue is empty.

A WM_PAINT message is sent when UpdateWindow () is called, and the application will automatically call invalidate () after receiving the WM_PAINT message, so invalidate () is not necessarily present in the program code!

UpdateWindow () is to send the WM_PAINT message immediately, only for areas where the declaration is invalid.
Invalidate () is one of the ways that the declaration is invalid.

Invalidate () indicates that the customer area is invalid and is redrawn the next time the WM_PAINT occurs. While WM_PAINT is maintained by the system, Windows sends a WM_PAINT message whenever the update area of CWnd is not empty and there is no other message in the application's window message queue.
The invalidate has a bool type parameter that identifies whether the redraw is filled with a background color. Does it use the SetBkColor function? Go ahead and study.

UpdateWindow requires the system to redraw the area immediately.

Seeing someone ask questions on the Internet, he wrote the drawing function behind invalidate but did not do it because invalidate went to the paint command after the execution. So the back is not shown.

And finally figured out that my picture has been flashing, because I used the invalidate () function in the paint, so he kept self-nesting, but the picture of the painting kept flashing.

Invalidate let the customer area in a redraw state, and UpdateWindow began to redraw, but it first to determine whether the client area is empty, not empty UpdateWindow do not execute, is empty to perform the repaint.

Invalidat is finally called Invalidatrect, only invalidatrect in the Windows API

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.