|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Filling ShapesYou can fill shape controls using the shapes FillStyle property with crosshatching, diagonal lines, and other fill patterns. Heres a list of the possible values for the FillStyle property:
You can see what each of these fill styles looks like in Figure 14.11. Note in particular the transparent fill stylewhich really just means that the shape control is not filled. Thats usually the style you use when you draw shapes in a form to group controls together.
Drawing A Shape Without The IDE GridWhen 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:
Thats it. Now youre free to draw controls as you want them and where you want them, without having your controls boundaries fall on a grid line.
Moving Shapes At RuntimeBecause 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 controls Move method: Shape.Move left, [top, [width, height]] Besides using Move, you can change a shapes control Top, Left, Width, and Height properties. Lets see an example. Here, well 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 thats 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 books accompanying CD-ROM.
Adding A Line Control To A ProgramThe shape control offers a number of predefined shapes for visual design, but sometimes thats not enough (what if the Aesthetic Design Department were to start demanding octagons?). For other cases, theres 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 controljust 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 controls 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 containers AutoRedraw property is set to False (unless its Visible property is set to False). As an example, weve drawn a few lines in the form in Figure 14.13 using the line control.
|
|
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. |