Control.Invalidate, Control.Update and Control.Refresh 有什么区别


关键字: Control.Invalidate, Control.Update, Control.Refresh

摘自:Whats the difference between Control.Invalidate, Control.Update and Control.Refresh?


Control.Invalidate() v/s Control.Update() v/s Control.Refresh()

 

Before discussing each one of the above functions, let’s look at how winforms controls paint.

 

Windows controls paint is response to WM_PAINT messages. This message is sent when UpdateWindow orRedrawWindow is called, or by the DispatchMessage function when the application gets a WM_PAINT through the message queue. On getting the WM_PAINT message, the control paints its background and then the foreground if necessary. Double-buffering and transparency is honored while painting and then the OnPaint event is fired to give the user a chance to perform his custom painting.

 

With this background, let’s look at the above mentioned three functions in more detail,

 

Control.Invalidate( ) / Control.Invalidate(bool) / Control.Invalidate(Rectangle) / Control.Invalidate(Rectangle, bool) / Control.Invalidate(Region) / Control.Invalidate(Region, bool)

 

            The bool parameter denotes whether the user wants to invalidate the child controls of the control on which he is calling Invalidate. The Rectangle parameter are the bounds to invalidate and the region parameter is the region to invalidate. All the overloads essentially end up calling one of the RedrawWindowInvaliateRect orInvalidateRgn functions. If RedrawWindow is called then this may result in a WM_PAINT message being posted to the application message queue (to invalidate the child controls).

            The important thing to note here is that these functions only “invalidate” or “dirty” the client area by adding it to the current update region of the window of the control. This invalidated region, along with all other areas in the update region, is marked for painting when the next WM_PAINT message is received. As a result you may not see your control refreshing (and showing the invalidation) immediately (or synchronously).

 

Control.Update()

 

Update function calls the UpdateWindow function which updates the client area of the control by sending WM_PAINT message to the window (of the control) if the window's update region is not empty. This function sends a WM_PAINT directly to WNDPROC() bypassing the application message queue.

Thus, if the window update region is previously “invalidated” then calling “update” would immediately "update" (and cause repaint) the invalidation.

 

Control.Refresh()

 

            By now, you might have guessed what Refresh( ) would be doing. Yes, it calls Invalidate(true) to invalidate the control and its children and then calls Update( ) to force paint the control so that the invalidation is synchronous.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值