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

Prevent a window from repainting


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 tip show show to prevent an area of a window from repainting. When you have a lot of items to add to a control, such as a ListBox or ListView, this can considerably speed up the process. On my system, it speeds up adding 10,000 items to a List Box by over 30%.

Create a new project, and add a ListBox, a Command Button and a CheckBox to the form. Change the caption of the CheckBox to '&Lock Update' and change the caption of the Command Button to '&Load...'. Then paste the following code into the form:

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

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

When you click on the command button, the code will add 10,000 items to the List Box. If the Lock Update check box is checked, windows will prevent any redrawing of the List Box whilst the items are added.


Related Tips and Articles:


AboutContributeSend FeedbackPrivacy

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