Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Filling Shapes

You can fill shape controls using the shape’s FillStyle property with crosshatching, diagonal lines, and other fill patterns. Here’s a list of the possible values for the FillStyle property:

  VbFSSolid—0; solid
  VbFSTransparent—1 (the default); transparent
  VbHorizontalLine—2; horizontal line
  VbVerticalLine—3; vertical line
  VbUpwardDiagonal—4; upward diagonal
  VbDownwardDiagonal—5; downward diagonal
  VbCross—6; cross
  VbDiagonalCross—7; diagonal cross

You can see what each of these fill styles looks like in Figure 14.11. Note in particular the transparent fill style—which really just means that the shape control is not filled. That’s usually the style you use when you draw shapes in a form to group controls together.


Figure 14.11  The Visual Basic fill styles.


TIP:  To set the fill color in a shape control, you can use the FillColor property at both design time and runtime. To place a value in the FillColor property at runtime, use the Visual Basic RGB function like this, where we fill a shape with red: Shape1.FillColor = RGB(255, 0, 0).

Drawing A Shape Without The IDE Grid

When you draw shapes in the Visual Basic Integrated Development Environment (IDE), the boundaries of that control fall along the dotted grid you can see in forms. That grid can help in aligning controls and lines, but there are times when you want finer control.

To turn off the automatic alignment of controls to the grid as you draw them, follow these steps:

1.  Select the Tools|Options menu item.
2.  Click the General tab in the Options dialog box.
3.  Deselect the box marked Align Controls To Grid.
4.  Click on OK to close the Options dialog box.

That’s it. Now you’re free to draw controls as you want them and where you want them, without having your controls’ boundaries fall on a grid line.


TIP:  You can hide the grid by deselecting the Show Grid box in the Options dialog box, as well as reset its dimensions (the default size of each cell in the grid is 120x120 twips).

Moving Shapes At Runtime

Because shape controls are design elements, there are times you might want to move them around as a program runs, and you can do that with the control’s Move method:

Shape.Move left, [top, [width, height]]

Besides using Move, you can change a shape’s control Top, Left, Width, and Height properties. Let’s see an example. Here, we’ll just move four shape controls showing circles around at random in a form. To use random numbers in Visual Basic, we start with the Randomize statement when the form loads; this initializes the random number generator:

Private Sub Form_Load()
    Randomize
End Sub

Next, add four shape controls, Shape1 to Shape4, showing circles, and a timer, Timer1, to the program, setting the timer Interval property to 1000 (in other words, 1 second), and adding a Timer event handler:

Private Sub Timer1_Timer()

End Sub

Now in Timer1_Timer(), we move the four circles around at random with the Move method:

Private Sub Timer1_Timer()
    Shape1.Move Shape1.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape1.Top _
        + ScaleHeight * (Rnd - 0.5) / 50
    Shape2.Move Shape2.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape2.Top _
        + ScaleHeight * (Rnd - 0.5) / 50
    Shape3.Move Shape3.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape3.Top _
        + ScaleHeight * (Rnd - 0.5) / 50
    Shape4.Move Shape4.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape4.Top _
        + ScaleHeight * (Rnd - 0.5) / 50
End Sub

And that’s all it takes. The result of this code appears in Figure 14.12. When you run the program, the circles move around at random. The code for this example is located in the circles folder on this book’s accompanying CD-ROM.


Figure 14.12  Moving shape controls around at random.


TIP:  Besides moving shapes, you can hide and show them by setting their Visible property to False and True, respectively.

Adding A Line Control To A Program

The shape control offers a number of predefined shapes for visual design, but sometimes that’s not enough (what if the Aesthetic Design Department were to start demanding octagons?). For other cases, there’s the line control.

The line control does just as its name implies: it draws a line. You can draw lines at design time simply as you would any other control—just click the Line Control tool in the toolbox, press the mouse button at one end of the line you want, and drag the mouse to the other end.

The line control’s primary properties are X1, X2, Y1, and Y2, and those values form the coordinates of the line segment: (X1, Y1) and (X2, Y2). You can even change those values at runtime to move or resize the line (line controls do not have a Move method).

You can also draw lines with this control in forms, picture boxes, and in frames. In fact, lines drawn with the line control stay visible even if its container’s AutoRedraw property is set to False (unless its Visible property is set to False).

As an example, we’ve drawn a few lines in the form in Figure 14.13 using the line control.


Figure 14.13  Lines drawn with the line control.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.