If you experiment with Spiral application, you’ll see that it has a major flaw: if you .swltch to another application and then return to the Spiral window, the curve is erased. This isn’t really a problem with the application per se, rather, it’s a thorny issue in drawing with Visual Basic. You can overcome it in several ways, but first you must understand how Visual Basic handles graphics. One of the most important properties affecting Visual Basic graphics IS AutoRedraw, which has two possible settings, True and False. When you draw on a control with its AutoRedraw property set to False, every shape appears instantly-on the screen. This is how the Spiral application works. However, this Setting has two problems. First, the drawing disappears when the window is covered by another one because any shapes placed on a control aren’t stored permanently.jhat’s why Windows can’t refresh the control. Which leads us to the-second problem: since Windows doesn’t maintain a permanent copy of the control’s contents, if can’t save the contents with the SavePicture method. Let’s see if the other setting eliminates these problems. Open the Spiral application in.design mode and set the PictureBox control’s property to True. Previously, with AutoRedraw=False, you could watch the curve slowly being drawn, but now you must wait until VISual Basic completes the drawing to see the curve on the control. The program now is half as fun to watch, notto mention that if you didn’t know better, you’d think that it didn’t work. So, what’s the problem with his setting of the AutoRedraw property?
When you draw with the AutoRedraw property set to True, Visual Basic draws in a special area of memory called a device context. A device context is a copy of the actual picture on the Picture Box or Form, and VISUalBasic uses it to update the image of the actual object from time to time. The contents of the device context are transferred to the conaol under two circumstances:
• Whenever VISual Basic gets a chance
• When you instruct Visual Basic to do so with the Refresh method
The Refresh method refreshes the contents of the control to which it’s applied, including any graphics; If you apply the Refresh method to the Form object, the entire, window is refreshed. To refresh the contents of a PictureBox control, apply the Refresh method to the control like this:
Picturel. Refresh
As mentiOned, Visual Basic also updates the controls when it gets a chance, and this happens whenever the application isn’t busy executing code. When the DrawRouletteO subroutine is executing, Visual Basic doesn’t get a chance to do anything else. When the subroutine ends and Visual Basic is waiting for an external event, it gets a chance to redraw the controls. That’s why you don’t see anything on the screen until the entire curve has been calculated .
When to Refresh
As you ‘can see, the setting of the AutoRedraw property can seriously affect your application. If you set it to False, users are able to watch the progress of the drawing, as they should. But you won’t have a permanent copy of the drawing, which means you can’t save it in a file. Also, if the user switches to another application, the control will be cleared, and you’ll have to repeat the drawing. It looks as if we have a nowin situation here, but as always, there are ways to overcome the problem. One solution is to draw with AutoRedraw set to True and to refresh the control frequently. How about refreshing the control after each poipt is plotted? You can try ‘this by Inserting the following statement right after the Line method in the DrawRoulette() subroutine:
Picture Refresh
This statement causes the program to refresh the PictureBox control so often that it behaves as if its AutoRedraw property is set to False. Run the program now and you’ll see that this approach is out of the question. The program is too slow. This ‘is the penalty of refreshing controls frequently. How about refreshing the control after each rotation of the outer circle around the inner one? Simply remove the Refresh statement you just inserted in the code, insert it between the two loops, and run the program again. The DoEvents statement between the two loops has the same effect. It interrupts the program and gives Wmdows the chance to refresh the screen. (This statement was placed there to give the program a chance to process external events, such as the click of a Command button.) The curve is drawn in pieces, a rather unnatural progression. This approach is quite useful in other situations but it’~ not as helpful here. For instance, when you process an image, you can refresh the image one row at a time. This is what we’ll do in the image processing application we’ll develop in the following chapter, but .the Spiral application requires a more complicated approach. You could refresh the PictureBox control more frequently, or you might ‘try another interesting approach: draw on two identical PictureBox controls. This is requires the following:
A visible control that has its AutoRedraw property set to False, so that all points appear as they are plotted.
An invisible control that has its AutoRedraw property set to True, so the bitmap of the curve is always available to your code If you want to save the curve as a bitmap to a disk file, you can use the Picture property of the invisible PictureBox.
The other problem you must cope with is updating the visible control when its contents are cleared because the user has switched to another window. This condition is signaled to your application by the Paint event. Therefore, every time the application receives a Paint event, the program must copy the contents of the invisible PictureBox control onto the visible one. This approach has all the benefits of the True setting of the AutoRedraw property and the curve is always visible, even while it’s being drawn.