Visual Basic Concepts
Each form and picture box has an AutoRedraw property. AutoRedraw is a Boolean property that, when set to True, causes graphics output to be saved in memory. You can use the AutoRedraw property to create persistent graphics.
Microsoft Windows manipulates the screen image to create an illusion of overlapping windows. When one window is moved over another, temporarily hiding it, and is then moved away again, the window and its contents need to be redisplayed. Windows takes care of redisplaying the window and controls. But your Visual Basic application must handle redisplaying graphics in a form or picture box.
If you create graphics on the form using graphics methods, you usually want them to reappear exactly as you placed them (persistent graphics). You can use the AutoRedraw property to create persistent graphics.
The default setting of AutoRedraw is False. When AutoRedraw is set to False, any graphics created by graphics methods that appear on the form are lost if another window temporarily hides them. Also, graphics that extend beyond the edges of the form are lost if you enlarge the form. The effects of setting AutoRedraw to False are shown in Figure 12.6.
Figure 12.6 The effects of setting AutoRedraw to False
When the AutoRedraw property of a form is set to True, Visual Basic applies graphics methods to a "canvas" in memory. The application copies the contents of this memory canvas to redisplay graphics temporarily hidden by another window. In most cases, the size of this canvas for forms is the size of the screen. If the form’s MaxButton property is False and the border of the form is not sizable, the size of the canvas is the size of the form.
This canvas also lets the application save graphics that extend beyond the edges of the form when the form is resizable. The effects of setting AutoRedraw to True are shown in Figure 12.7.
Figure 12.7 The effects of setting AutoRedraw to True
When the AutoRedraw property of a picture box is set to True, Visual Basic saves only the visible contents of the picture box in memory. This is because the memory canvas used to save the contents of the picture box is the same size as the picture box. Graphics that extend outside the picture box are cropped and never appear later, even if the size of the picture box changes.
You can leave AutoRedraw set to False for the form and all its picture boxes to conserve memory. But then the graphics are not automatically persistent: You have to manage redrawing all graphics in code as needed.
You can include code in the Paint event for a form or picture box that redraws all lines, circles, and points as appropriate. This approach usually works best when you have a limited amount of graphics that you can reconstruct easily.
A Paint event procedure is called whenever part of a form or picture box needs to be redrawn — for example, when a window that covered the object moves away, or when resizing causes graphics to come back into view. If AutoRedraw is set to True, the object’s Paint procedure is never called unless your application calls it explicitly. The visible contents of the object are stored in the memory canvas, so the Paint event isn’t needed.
Keep in mind that the decision to use nonpersistent graphics can affect the way graphics paint on the form or container. "Clipping Regions with ClipControls" and "Layering Graphics with AutoRedraw and ClipControls" discuss other factors that may determine whether or not you should use nonpersistent graphics.
You can change the setting of AutoRedraw at run time. If AutoRedraw is False, graphics and output from the Print method are written only to the screen, not to memory. If you clear the object with the Cls method, any output written when AutoRedraw was set to True does not get cleared. This output is retained in memory, and you must set AutoRedraw to True again and then use the Cls method to clear it.
For More Information To learn about the performance implications of AutoRedraw, see "Optimizing Display Speed" in "Designing for Performance and Compatibility."