Generating Simple Graphics

Generating simple graphics is a relatively easy task. Visual Basic includes the Circle, Line, and PSet methods for drawing circles, lines, and points, respectively.

The code in Listing 9.10 shows how to create a circle with a radius of 0.5 inch (720 twips) centered at a location 1 inch down and 1 inch over (1440 by 1440 twips). Then a square is drawn, with each side 1 inch long (hint: 2160 twips equal 1.5 inches). Finally, I make a point in the exact center of the circle and the square. Figure 9.9 shows the results.

Listing 9.10: Command7_Click Event in Print

Private Sub Command7_Click()
Picture1.Circle (1440, 1440), 720
Picture1.Line (720, 720)-(2160, 720)
Picture1.Line (2160, 720)-(2160, 2160)
Picture1.Line (2160, 2160)-(720, 2160)
Picture1.Line (720, 2160)-(720, 720)
Picture1.PSet (1440, 1440)

End Sub

Figure 9.9: You can use Visual Basic’s drawing methods to create a square, a circle, and a point.

Inches, Centimeters, Pixels, or Twips

You can use any of the four measuring systems when you specify coordinates for these graphic methods, as follows:

At one time or another, I’ve tried each of these scales and had problems. I forgot to set ScaleMode when I created a new control, or somehow ScaleMode would get reset back to twips. Or worse, the scales were user-defined—so who knew what values were legal coordinates? Finally, I gave up and decided to memorize 1,440 twips = 1 inch. Now I write code like 1440*2 instead of 2.0 inches. Performance-wise, there shouldn’t be a difference, since the compiler should optimize that 1440*2 to 2880.

© 1998 SYBEX Inc. All rights reserved.