Optimizing Display Speed

See Also

Because of the graphical nature of Microsoft Windows, the speed of graphics and other display operations is crucial to the perceived speed of the application. The faster forms appear and paint, the faster your application will seem to the user. There are several techniques you can use to speed up the apparent speed of your application, including:

Set the ClipControls Property of Containers to False

Unless you are using graphics methods (Line, PSet, Circle, and Print), you should set ClipControls to False for the form and for all frame and picture box controls (it may cause unpredictable results if your code includes graphics methods that draw behind other controls). When ClipControls is False, Visual Basic doesn’t overpaint controls with the background before repainting the controls themselves. On forms that contain a lot of controls, the resulting speed improvements are significant.

For More Information   See "Layering Graphics with AutoRedraw and ClipControls" in "Working with Text and Graphics."

Use AutoRedraw Appropriately

When AutoRedraw is set to True for a form or control, Visual Basic maintains a bitmap to repaint that form or control. Although this improves the speed of simple repaints (for example, when the form or control is revealed after a window that covers it is removed), it slows graphics methods. Visual Basic has to perform the graphics methods on the AutoRedraw bitmap and then copy the entire bitmap to the screen. This process also consumes a considerable amount of memory.

If your application generates complex graphics but doesn’t change them frequently, setting AutoRedraw to True is appropriate. But if your application draws graphics that must change frequently, you will get better performance if you set AutoRedraw to False and perform the graphics methods for the form or control in the Paint event.

For More Information   See "Layering Graphics with AutoRedraw and ClipControls" in "Working with Text and Graphics."

Use Image Controls Instead of Picture Box Controls

This optimization improves the speed and minimizes the size of your application; use it whenever possible. When you are simply displaying pictures and reacting to click events and mouse actions on them, use the image control instead of the picture box. Don’t use a picture box unless you need the capabilities only the picture box provides, such as graphics methods, the ability to contain other controls, or dynamic data exchange (DDE).

Hide Controls When Setting Properties to Avoid Multiple Repaints

Every repaint is expensive. The fewer repaints Visual Basic must perform, the faster your application will appear. One way to reduce the number of repaints is to make controls invisible while you are manipulating them. For example, suppose you want to resize several list boxes in the Resize event for the form:

Sub Form_Resize ()
Dim i As Integer, sHeight As Integer
   sHeight = ScaleHeight / 4
   For i = 0 To 3
      lstDisplay(i).Move 0, i * sHeight, _
      ScaleWidth, sHeight
   Next
End Sub

This creates four separate repaints, one for each list box. You can reduce the number of repaints by placing all the list boxes within a picture box, and hiding the picture box before you move and size the list boxes. Then, when you make the picture box visible again, all of the list boxes are painted in a single pass:

Sub Form_Resize ()
Dim i As Integer, sHeight As Integer
   picContainer.Visible = False
   picContainer.Move 0, 0, ScaleWidth, ScaleHeight
   sHeight = ScaleHeight / 4
   For i = 0 To 3
      lstDisplay(i).Move 0, i * sHeight, _
      ScaleWidth, sHeight
   Next
   picContainer.Visible = True
End Sub

Note that this example uses the Move method instead of setting the Top and Left properties. The Move method sets both properties in a single operation, saving additional repaints.

Use Line Instead of PSet

The Line method is faster than a series of PSet methods. Avoid using the PSet method and batch up the points into a single Line method. Shape and line controls are appropriate for simple graphical elements that rarely change; complex graphics, or graphics that change rapidly, are generally best handled with graphics methods.