Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

The Line and Shape Controls

The graphics you've worked with in this book have, until now, been graphic images stored in files. The Image and Picture Box controls display graphic images on the form. The toolbar buttons can display icon images. You have yet to see how to draw your own graphics. The rest of this lesson introduces VB's drawing tools.

The Line and Shape controls work together to draw lines, boxes, and all kinds of figures on the form. By placing the controls and setting appropriate properties, you'll be adding flair to applications. The properties of each control that you place on your form determine exactly what kind of image the control becomes.

Here are the primary graphics images that you can draw with the Line and Shape controls:

Figure 19.8 shows most of these images. By combining these fundamental geometric images and setting appropriate color and size properties, you can draw virtually anything you need to draw on the form.

19fig08.gif

Figure 19.8 The images that you can draw.

The Line Control

You use the Line control to draw lines of various widths, lengths, and patterns. The Shape control handles the drawing of all other fundamental shapes.

Mastering the Line Control

The Line control contains properties that specify the width and length of lines you draw. In addition, you can change the pattern of each line you draw.

Table 19.1 lists the fundamental property values for the Line control. Table 19.2 contains the values that you can specify for the BorderStyle property. The BorderStyle property determines the pattern that Visual Basic uses to draw the line. By specifying various BorderStyle values, you can vary the line pattern. If you assign a BorderStyle property at runtime, you can either specify a number that represents BorderStyle or use one of Visual Basic's named literals.

Table 19.1. The Line control's fundamental properties.

Property Description
BorderColor Sets the line color.
BorderStyle Contains one of seven values that specifies the pattern of the drawn line. See Table 19.2 for available BorderStyle values. The default value is 1-Solid. BorderStyle has no effect on lines with BorderWidth greater than 1 twip.
BorderWidth Specifies the size, in twips, that the line takes.
DrawMode An advanced style that determines how the bit patterns of the line interact with the surrounding form's bit appearance. The default value, 13-Copy Pen, works well for virtually all Visual Basic applications.
Visible Holds True or False, indicating whether the user can see the Line control. You might want to set the Visible property in code so the line appears as a highlighting tool.
X1 Contains the number of twips from the left of the Form window to the start of the line.
X2 Contains the number of twips from the left of the Form window to the end of the line.
Y1 Contains the number of twips from the top of the Form window to the left starting point of the line.
Y2 Contains the number of twips from the top of the Form window to the lower ending point of the line.

Table 19.2. The Line control's BorderStyle values.

Value Named Literal Description
0-Transparent vbTransparent Background comes through the line.
1-Solid vbBSSolid The line is a solid line.
2-Dash vbBSDash The line is composed of dashes.
3-Dot vbBSDot The line is composed of dots.
4-Dash-Dot vbBSDashDot The line is composed of a continuing dash-dot-dash-dot.
5-Dash-Dot-Dot vbBSDashDotDot The line is composed of a series of one dash followed by two dots.
6-Inside Solid vbBSInsideSolid Same as 1-Solid for lines.

Figure 19.9 shows how various BorderStyle settings affect the lines you draw. BorderStyle determines how a series of dashes and dots compose the line's pattern. (Is this Morse code we're speaking here?)

19fig09.gif

Figure 19.9 The BorderStyle property values.

To draw a line, double-click the Line control on the toolbox. A line appears in the center of the form with a handle on each end. To move the line to a different location, drag the center of the line with the mouse. To lengthen or shorten the line, drag either handle on the line. You can raise and lower either end of the line by dragging either end's handle with the mouse.

After you position the line with the mouse in the approximate location at which you need the line to appear, you can fine-tune the line's size and location by setting the various property values. If you're a patient programmer, you can even animate the lines by changing the X1, X2, Y1, and Y2 property settings repeatedly through code.

Figure 19.10 contains the Form window that might be used as a company's front-end form. The various lines help separate controls from the title. As you can see, lines help focus the user's attention.

19fig10.gif

Figure 19.10 Accenting forms with lines.

Mastering the Shape Control

The Shape control gives you the capability to draw six different kinds of figures on the form. The various shading and color properties help you distinguish one shape from another. Table 19.3 contains the basic properties you'll use for the Shape control. The most important property is the Shape property. The Shape property gives a shape from one of the six fundamental shapes.

Table 19.3. The Shape control's fundamental properties.

Property Description
BackColor Specifies a Windows color value that determines the background color of the shape.
BackStyle Contains either 0-Transparent (the default) or 1-Opaque, which determines whether the background of the form appears through the shape or if the shape hides whatever it covers.
BorderColor Specifies a Windows color value that determines the color of the shape's bordering edges.
BorderStyle Contains one of seven values that specifies the pattern of the shape's border. The Line control's BorderStyle values (refer to Table 19.2) provide the shape's BorderStyle possible values as well. The default value is 1-Solid. BorderStyle has no effect on shapes with a BorderWidth greater than 1 twip.
BorderWidth Specifies the size, in twips, that the shape's outline takes.
DrawMode An advanced style that determines how the bit patterns of the shape interact with the surrounding form's bit appearance. The default value, 13-Copy Pen, works well for virtually all Visual Basic applications.
FillColor Specifies a Windows color value that determines the color of the shape's interior lines.
FillStyle Contains one of eight values that specifies the pattern of lines with which Visual Basic paints the interior of the shape. Table 19.4 contains the possible values for the shape's FillStyle. The default FillStyle value is 0-Solid.
Height Specifies the number of twips high that the shape is (from the highest point to the lowest point in the shape).
Left Specifies the number of twips from the form's left edge to the shape's far left edge.
Shape Contains one of six values that specifies the type of shape that the Shape control takes on. Table 19.5 contains the possible values for the shape's Shape property. The default Shape property is 0-Rectangle.
Top Specifies the number of twips from the form's top edge to the shape's highest edge.
Width Specifies the number of twips wide that the shape takes (at the widest axis).

Table 19.4 contains the possible values for the Shape control's FillStyle property. Figure 19.11 shows the various fill patterns that a shape can contain.

19fig11.gif

Figure 19.11 The FillStyle property determines the shape's interior design.

Table 19.4. The Shape control's FillStyle values.

Value Named Literal Description
0-Solid vbFSSolid Solid color fill with no pattern.
1-Transparent vbFSTransparent The shape appears as an outline only.
2-Horizontal Line vbHorizontalLine Horizontal lines fill the shape.
3-Vertical Line vbVerticalLine Vertical lines fill the shape.
4-Upward Diagonal vbUpwardDiagonal Upward diagonal lines fill the shape.
5-Downward Diagonal vbDownwardDiagonal Downward diagonal lines fill the shape.
6-Cross vbCross Crosshairs fill the shape.
7-Diagonal Cross vbDiagonalCross Diagonal crosshairs fill the shape.

Table 19.5 contains the possible values for the Shape control's Shape property. Figure 19.8 shows the various shapes that the Shape control can take. Therefore, when you want to place a square on a form, you'll place the Shape control on the form and set the Shape property to 1-Square.

Table 19.5. The Shape control's Shape values.

Value Description
0-Rectangle A rectangle
1-Square A square
2-Oval An oval
3-Circle A circle
4-Rounded Rectangle A rectangle with rounded corners
5-Rounded Square A square with rounded corners

Share ThisShare This

Informit Network