What's the difference between invalidate,update and refresh?

Source: Internet
Author: User
Tags bool

When you do Windows Forms development, you will inevitably need to manually refresh the window to redraw the controls that you want to change, or something else. When this type of demand arises, you have three choices, using the Invalidate,update or Refresh method.

Invalidate has six overloaded methods:

public void Invalidate (Region Region)
public void Invalidate (Region Region, bool Invalidatechildren)
public void Invalidate ()
public void Invalidate (bool invalidatechildren)
public void Invalidate (Rectangle RC)
public void Invalidate (Rectangle RC, bool Invalidatechildren)

Generally speaking, the public void Invalidate () method is more widely used. The public void Invalidate (bool Invalidatechildren) method is actually tuned inside this method, and the argument is set to false.

1 public void Invalidate ()
2 {
3 this. Invalidate (FALSE);
4}

True and false of the parameter values in the public void Invalidate (bool Invalidatechildren) method represent the need to redraw all of the subforms at the same time when redrawing.

public void Invalidate (bool invalidatechildren)  
{  
    if (this. ishandlecreated)  
    {  
    if (Invalidatechildren)  
    {  
        Safenativemethods.redrawwindow (new HandleRef ( This.window, this. Handle), NULL, NATIVEMETHODS.NULLHANDLEREF, N.  
    else
    {  
        using (new Control.multithreadsafecallscope ())  
        {  
        Safenativemethods.invalidaterect (new HandleRef (This.window, this. Handle), NULL, (This.controlstyle & Controlstyles.opaque)!= controlstyles.opaque);  
        }  
    This. Notifyinvalidate (this. ClientRectangle);  
    }  

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

If you need to redraw all the subforms, call the RedrawWindow method with the parameter flags set to 133.

Flags are some of the macro definitions defined in the WinUser.h file.

  
* * * REDRAWWINDOW () flags */
#define Rdw_invalidate          0x0001  
#define Rdw_internalpaint       0x0002  
#define Rdw_erase               0x0004  
       
#define Rdw_validate            0x0008  
#define Rdw_nointernalpaint     0x0010  
#define Rdw_noerase             0x0020  
       
#define Rdw_nochildren          0x0040  
#define Rdw_ Allchildren         0x0080  
       
#define Rdw_updatenow           0x0100  
#define Rdw_erasenow            0x0200  
       
# Define Rdw_frame               0x0400  
#define RDW_NOFRAME             0x0800

133 means rdw_invalidate|rdw_erase|rdw_allchildren, the specific meaning of each value can view MSDN.

And if you don't need to redraw the subform, call the InvalidateRect method accordingly.

As for the Update method, the UpdateWindow method is called directly.

1 public void Update ()
2 {
3 Safenativemethods.updatewindow (The new HandleRef (This.window, this.) Internalhandle));
4}

UpdateWindow can bypass application Message Queuing (there may be a lot of messages in the application that need to be processed) to send a WM_PAINT message directly to the form process (window procedure), but no message will be sent if the update area is blank. The Update method actually increases the priority of the Invalidate method to the highest level.

Finally, the Refresh method. It may be confusing with update, but once you see the source code, all relationships become clear.

1 public virtual void Refresh ()
2 {
3 this. Invalidate (TRUE);
4 this. Update ();
5}

Yes, refresh calls the Invalidate method first, and then calls the Update method. So refresh = Invalidate + Update.

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.