Invalidate () InvalidateRect () and UpdateWindow ()

Source: Internet
Author: User

By citation: 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, after calling invalidate, 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.

UpdateData ():

When you use ClassWizard to establish a link between a control and a variable: When you modify the value of a variable and you want the dialog box control to update the display, you should call UpdateData (FALSE) after modifying the variable, or if you want to know what the user entered in the dialog box, You should call UpdateData (TRUE) before accessing the variable to map the control's input to the variable.

Invalidate ():
the function is to invalidate the entire window client area . Invalid client area of window means redraw required . For example, if a window obscured by another window becomes a foreground window, the part that was obscured is invalid and needs to be redrawn. Windows then places the WM_PAINT message in the application's message queue. MFC provides a WM_PAINT message handler function for the window class Onpaint,onpaint is responsible for redrawing the window. There are some exceptions to the view class, and the OnDraw function is called in the OnPaint function of the view class, and the actual redraw work is done by OnDraw. When the parameter berase is true, the background in the redraw area will be erased, otherwise the background will remain unchanged.

InvalidateRect ():
The function is functionally the same as invalidate, but it makes the specified area invalid, requires entering an area, and if the argument is null, sets the entire window to an invalid area.

UpdateWindow ():
      UpdateWindow () is the function of making the window redraw immediately . The window does not redraw immediately after calling functions such as invalidate, because the WM_PAINT message has a low priority, and it needs to be processed after other messages in the message queue have been sent out. Calling the UpdateWindow function causes the WM_PAINT to be sent directly to the target window, causing the window to be redrawn immediately.

updatewindow: If there is an invalid zone, then immediately sending a WM_PAINT message to the window processing process, do not enter the message queue to wait, immediately refresh the window, otherwise, do nothing.  
invalidaterect: Set Invalid zone, If it is a null parameter, the entire window is set 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 automatically calls invalidate () after it receives the WM_PAINT message. therefore, in the program code, do not necessarily appear invalidate ()!

UpdateWindow () is to send WM_PAINT messages immediately,UpdateWindow requires the system to redraw the area immediately, which only works on areas declared as invalid, and invalidate () is one way to declare an invalid zone.

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 .

Invalidat finally also calls Invalidatrect.

RedrawWindow forced flush will call WM_PAINT, but if you force the refreshed part to not exist, the WM_PAINT will not be called. If you do not have any parameters, this window will be refreshed.

*************************************************************************************************************** **************************

See someone on the Internet to ask questions, he wrote the function of the drawing after the invalidate but did not execute, this is because invalidate executed after the change to the paint command, so the back is not displayed.

Also finally figured out my picture has been flashing, this is because I used the invalidate () function in the paint, so he kept self-nesting, resulting in the picture is constantly flashing.

In short: Invalidate let customer area can redraw the state, and UpdateWindow began to redraw, but it first need to determine whether the client area is empty, not empty then UpdateWindow do not execute, empty to perform redraw.

*************************************************************************************************************** ******************************

The redraw function is often called when the window is refreshed MFC provides three functions for window redrawing
InvalidateRect (&rect)
Invalidate ()
UpdateWindow ()
When a window needs to be updated or redrawn, the general system emits two messages WM_PAINT (notifies the client area of changes) and Wm_ncpaint (notifies the non-client area to change) Wm_nvpaint the system will take care of itself. WM_PAINT message corresponding function is OnPaint () , which is the system default to accept WM_PAINT messages, but we usually do redraw in the program in the OnDraw function, because the OnDraw function is called in the View class OnPaint function.
CView default standard redraw function
void Cview::onpaint ()
             {  
CPAINTDC DC (this);
ONPREPARDC (&DC);
OnDraw (&DC);//Call the OnDraw

            } 
the above mentioned InvalidateRect (&rect) and Invalidate (). The two function forms and functions are similar, but invalidate is the result that the entire window forms an invalid rectangle, while InvalidateRect (&rect) invalidates the specified area. The Invalidate () declaration is invalid, waits for the WM_PAINT message to redraw, and the system automatically sends no other messages in the queue. UpdateWindow () sends the WM_PAINT immediately, but before it sends it, it calls GetUpdateRect (hwnd,null,true) to see if there is no drawing area, and if not, the message is not sent. RedrawWindow () 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.
Why is the system not sending WM_PAINT messages when calling invalidate? Why do I have to wait until the app message queue is empty to send WM_PAINT messages? This is because the system treats the drawing operations in the window as a low-priority operation, so as to be pushed backwards as much as possible. However, this also helps to improve the efficiency of drawing: Two WM_PAINT messages between the InvalidateRect and Invaliatergn to invalidate the area will be added up, and then in a WM_PAINT message is updated once, It not only avoids updating the same area multiple times, but also optimizes the app's update operations. such as this through InvalidateRect and invalidatergn to invalidate the window area, depending on the system at the right time to send the WM_PAINT message mechanism is actually an asynchronous way of working, that is, in the invalid window area and send Wm_ There is a delay between the paint messages, and sometimes this delay is not what we want, and we can certainly use SendMessage to send a WM_PAINT message after the invalid window area to force immediate redrawing, but instead of using Windows GDI provides us with more convenient and powerful functions:
UpdateWindow and RedrawWindow. UpdateWindow checks the window's update region to send the WM_PAINT message when it is not empty; RedrawWindow gives us more control: whether to redraw non-client areas and backgrounds, whether to always send WM_PAINT messages regardless of the update Whether the region is empty or not. BeginPaint and WM_PAINT messages are closely related. What if you try not to write beginpaint in the WM_PAINT processing function? The program will achieve a staggering CPU usage as it enters a dead loop, and you will find that the program is always processing one WM_PAINT message after another. This is because, under normal circumstances, when an app receives a WM_PAINT message, the window's update region is non-empty (if it's empty, you don't need to send a WM_PAINT message), and BeginPaint's role is to set the update to empty. This way, if you do not call BeginPaint, the window's update region is not empty, as mentioned earlier, the system will always send WM_PAINT messages. BeginPaint and WM_ERASEBKGND messages are also related. When the window's update region is marked as needing to erase the background, BeginPaint sends a WM_ERASEBKGND message to redraw the background, and in its return message there is a flag indicating whether the window background has been redrawn. When we use InvalidateRect and invalidatergn to add the specified area to the update region, you can set whether the region needs to be erased, so that the next beginpaint knows whether to send the WM_ Erasebkgnd the news. It is also important to note that BeginPaint can only be used in WM_PAINT processing functions

OnDraw, is generally receivedWm_paintMessage is called, so the application generally passes theInvalidateProduceWm_paintMessage to call indirectlyOnDraw。 When the form is invalid, and so on,Windowwill also produceWm_paintMessage, this timeOnDrawis also called indirectly.
OnUpdateIsCView Provides a method that is typically called when a document is modified, and the application framework is cview::oninitialupdate  cdocument::updateallviews  
onupdateonupdateinvalidatewm_paintondraw 

ondraw< Span lang= "ZH-CN" > besides you and the application framework indirect call, window 
onupdate Typically only your program and application framework will be called. Of course its default implementation you can change the

**************************************************** ******************************************************************

Oninitupdate is the initialization of the VIEW
OnUpdate is a document multi-view, responding to changes in other views
OnDraw and OnPaint are all drawings. OnPaint calls OnDraw, and calls OnPrepareDC
--------------------------------------------------------------- 

Invalidate () InvalidateRect () and UpdateWindow ()

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.