强制和防止窗口重画 (转)

强制和防止窗口重画 (转)[@more@]强制窗口重画  WXJ_Lake 编译

  这个例子告诉你如何强制窗口的一部分重画。有时这是必须的,特别是当你试验自己重画 控件的技术,或者已经使用了LockWindowUpdate这个 api 函数以阻止控件重画时(参见“防止一个窗口重画”)。

  新建一个项目,添加一个module,然后粘贴下列代码:

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Private Type POINTAPI
  X As Long
  Y As Long
End Type

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function InvalidateRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT, ByVal bErase As Long) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long

Public Sub RepaintWindow(ByRef objThis As object, Optional ByVal bClientAreaOnly As Boolean = True)
 Dim tR As RECT
 Dim tP As POINTAPI
 If (bClientAreaOnly) Then
   GetClientRect objThis.hWnd, tR
 Else
   GetWindowRect objThis.hWnd, tR
   tP.X = tR.Left: tP.Y = tR.Top
   ScreenToClient objThis.hWnd, tP
   tR.Left = tP.X: tR.Top = tP.Y
   tP.X = tR.Right: tP.Y = tR.Bottom
   ScreenToClient objThis.hWnd, tP
   tR.Right = tP.X: tR.Bottom = tP.Y
 End If
 InvalidateRect objThis.hWnd, tR, 1 
End Sub
    
  为了试试重画,在窗体上添加一个ListBox和一个Command。把ListBox拉得大一些,这样效果比较明显。再加入下列代码:

Private Sub Command1_Click()
 RepaintWindow List1
End Sub

Private Sub Form_Load()
 Dim i As Long
 For i = 1 To 200
   List1.AddItem "TestItem " & i
 Next i
End Sub

  当你单击Command按钮,ListBox的客户区将全部重画。对于ListBox,这种效果并不十分明显地显示,但这段代码放在这里主要目的,是让你在遇上有东西不能恰当地重画它自己时可以有办法解决。

————————————————————————————————

防止窗口重画  WXJ_Lake 编译

  这则代码演示了如何防止窗口的一部分重画。当你要往ListBox或ListView这样的控件里添加许多项时,暂缓重画可以相当地提高处理速度。在我的 系统上,往一个ListBox中加10000项比原来提速30%。

  新建一个项目,添加一个ListBox、一个Command和一个CheckBox。把CheckBox的Caption设为"&Lock Update",Command的Caption设为"&Load"。然后,把下列代码粘贴到窗体中:

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Private Sub Command1_Click()
 Dim i As Long
 Dim lTIme As Long

 lTIme = timeGetTime()
  
 If (Check1.Value = Checked) Then
   LockWindowUpdate List1.hWnd
 End If
  
 List1.Clear
 For i = 1 To 10000
   List1.AddItem "Test " & i
 Next i
  
 If (Check1.Value = Checked) Then
   LockWindowUpdate 0
   List1.Refresh
 End If
  
 MsgBox "Time: " & timeGetTime - lTIme
  
End Sub

  当你单击Command按钮,代码将往ListBox中添加10000项。如果"Lock Update"的复选框被选中, windows将在往ListBox中添加项时防止它的重画。操作结束后,会弹出一个对话框报告运行时间。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10790690/viewspace-951525/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10790690/viewspace-951525/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值