Spirals Revised

The Spirals 1 application implements the technique just described. The Picture2 control is invisible and has its AutoRedraw property set to True, It also has the exact position and dimensions as the visible Picture! control. The revised Draw-Roulettel) routine, which draws on both controls, is shown next The Refresh method seems unnecessary, but the program won’t work without it.

Run the Spiralsl application, watch its behavior, and compare it with the Spirals application. You gain all the convenience of the immediate refreshing of the Picture control, and at the same time, the contents of the control are persistent. Of course, the Spiralsl application requires a few more additional lines of code. For example, when the user changes the pen’s width, you must change the Draw- Width property for both Picturebox controls. Likewise, when the Clear Graph button is clicked, the program must clear both PictureBox controls. Other than that, the code of the two applications is identical.

Transparent Drawing

The AutoRedraw property isn’t only a source of problems. For instance, since everything you draw while the AutoRedraw Property is set to False appears on the control but doesn’t update the internal copy, you can manipulate the AutoRedraw property at runtime to draw on layers that can be instantly removed from the control. Figure 6.22 shows the TRGrit. application, which turns on and off a grid over the-contents of a PictureBox control. The grid is drawn while the AutoRedraw property. is False and can be easily removed with a single call to the Refresh method. The Refresh method copies the contents of the device context to the control. Because the grid is not saved in the device context, it disappears when the control is refreshed. Click the two buttons at the bottom of the Form to superimpose a linear or polar grid on top of the image.

The following code draws the linear grid on top of the image already loaded onto the Picture! control

To remove the grid, you must either reload the graphic to overwrite the contents -of the Picture Box or refresh the contents of the control with the Refresh method. The RemoveGrid subroutine that removes the grid from the Picture Box couldn’t be simpler:

Picture1.Refresh

The trick is in the second line of the ShowGridO subroutine:

Picturel.AutoRedraw False

Everything drawn after this (in other words, ‘everything drawn while the AutoRed.raw property is False) is ignored when VISual Basic updates the con- .tents of the control, because it’s not part of the device context. This simple technique . allows you to display all kinds o{grarhics on top of an image and. then remove them with a Refresh command, as if they were drawn on a transparency over the actual image. You can even assign different colors to different users. and let them annotate the same m:awing (e.g., the image of a received fax or a technical drawing).

Better Rubber Shapes

In the Draw application, with the help of the XOR drawing mode, you learned how to draw rubber shapes and how to temporarily place elements on a back- . ground without disturbing it. The XOR drawing mode has the Unique property of being reversible, but it also has a serious side effect: the rubber shapes assume strange colors. A better approach is to draw the rubber shape in copy mode (Draw- Mode=13), but switch to A~toRedraw=Fa1se mode. Then, instead of erasing the previous rubber shape as we did before, you can refresh the control. With each refresh, the rubber shape is erased, revealing the background, which is drawn with Auto- Redraw set to True.
The QDr~w application works just like the Draw application presented earlier, but it uses the technique just described. to draw rubber shapes (see Figure 6.23).The rubber shapes don’t change color, so they are much more convincing. The QDraw application is identical to the Draw application, but instead of switching to the XOR mode to draw rubber shapes and erasing the previous rubber shape from within the .MouseMove event, it turns off the AutoRedraw property and then uses the Refresh method to erase the previous shape. If you open the QDraw application and examineits code, you’ll see that only a few lines are changed.

The Case switch that 4,raws rubber shapes in the MouseMove event in the revised application is where most of the changes can be found.

Notice that the line that used to erase·the previous line in XOR mode is commented out. Its function is now accomplished with a call to the Form’s Refresh method. This example concludes our discussion of Visual Basic’s drawing commands. However, we’re not done with the graphics methods. In the following chapter, we are going to discuss the methods for manipulating pixels and ‘we’ll demonstrate them with an image processing application.

Scroll to Top