The new vbAccelerator Site - more VB and .NET Code and Controls

Force an area of a Window to Repaint


Steve McMahon(


API,GDI,Graphics,Windows Controls



Other Tips
All Tips
By Date
By Subject

API (33)
Manipulation (3)

Clipboard (3)
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Box (2)

Windows (11)
Controls (10)


This sample shows how to force an area of a window to repaint. Sometimes this is necessary, particularly when you're experimenting with owner draw control techniques, or when using the LockWindowUpdate API call to improve the speed at which a control fills with data.

Create a new project, add a module and paste in the code below:

Private Type RECT
&nbsp &nbsp Left As Long
&nbsp &nbsp Top As Long
&nbsp &nbsp Right As Long
&nbsp &nbsp Bottom As Long
End Type
Private Type POINTAPI
&nbsp &nbsp X As Long
&nbsp &nbsp 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( _
&nbsp &nbsp &nbsp &nbsp ByRef objThis As Object, _
&nbsp &nbsp &nbsp &nbsp Optional ByVal bClientAreaOnly As Boolean = True _
&nbsp &nbsp )
Dim tR As RECT
&nbsp &nbsp If (bClientAreaOnly) Then
&nbsp &nbsp &nbsp &nbsp GetClientRect objThis.hWnd, tR
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp GetWindowRect objThis.hWnd, tR
&nbsp &nbsp &nbsp &nbsp tP.X = tR.Left: tP.Y = tR.Top
&nbsp &nbsp &nbsp &nbsp ScreenToClient objThis.hWnd, tP
&nbsp &nbsp &nbsp &nbsp tR.Left = tP.X: tR.Top = tP.Y
&nbsp &nbsp &nbsp &nbsp tP.X = tR.Right: tP.Y = tR.Bottom
&nbsp &nbsp &nbsp &nbsp ScreenToClient objThis.hWnd, tP
&nbsp &nbsp &nbsp &nbsp tR.Right = tP.X: tR.Bottom = tP.Y
&nbsp &nbsp End If
&nbsp &nbsp InvalidateRect objThis.hWnd, tR, 1
&nbsp &nbsp
End Sub
&nbsp &nbsp &nbsp &nbsp

To try out the redrawing, add a ListBox and a Command button to your Project's form. Make the ListBox as big as possible so you will see the effect. Then add the following code:

Private Sub Command1_Click()
&nbsp &nbsp RepaintWindow List1
End Sub

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

When you click on the button, the client area of the List Box will be entirely redrawn. The effect is not particularly noticeable with the List Box as shown, but basically this tip is here to get you around any problems you have when things don't redraw themselves properly.


Related Tips and Articles:


AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 01/08/98