Visual Basic 6 Black Book phần 5 docx

112 329 0
Visual Basic 6 Black Book phần 5 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

" VbShapeOval2; oval " VbShapeCircle3; circle " VbShapeRoundedRectangle4; rounded rectangle " VbShapeRoundedSquare5; rounded square One important use of shape controls is to group other controls together. (Note, however, that shape controls cant act as true control containers in the way picture boxes or frames can. For example, you cant group option buttons together with shapes.) In Figure 14.10, were using shape controls to group the buttons visually into two groups. Figure 14.10 Using the shape control to group other controls. You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. For more on this control, see the other topics in this chapter. Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\446-449.html (3 of 3) [3/14/2001 1:48:23 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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: PreviousTable of ContentsNext Drawing Rectangles How do you draw rectangles with the shape control? You start by clicking the Shape Control tool in the Visual Basic toolbox and drawing that control to match the size and location you want your new figure to have. To draw a rectangle, you simply set the controls Shape property to VbShapeRectangle (thats the default anyway). Thats all you have to dothe shape control is very easy to work with. Using the shape control, you can draw in both forms and picture boxes. You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. Drawing Squares How do you draw squares with the shape control? You start by clicking the Shape Control tool in the Visual Basic toolbox and drawing that control to match the size and location you want your new figure to have. To draw a square, you simply set the controls Shape property to VbShapeSquare. Thats all you have to dothe shape control is simple. Using the shape control, you can draw in both forms and picture boxes. You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\449-452.html (1 of 3) [3/14/2001 1:48:31 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Go! using dots and dashes. For more on this control, see the other topics in this chapter. Drawing Ovals To draw ovals with the shape control, you start by clicking the Shape Control tool in the Visual Basic toolbox and drawing that control to match the size and location you want your new figure to have. To draw an oval, you simply set the controls Shape property to VbShapeOval. Thats all you have to dothe shape control is very easy. Using the shape control, you can draw in both forms and picture boxes. You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. For more on this control, see the other topics in this chapter. Drawing Circles To draw circles, you start by clicking the Shape Control tool in the Visual Basic toolbox and drawing that control to match the size and location you want your new figure to have. To draw a circle, you simply set the controls Shape property to VbShapeCircle. Thats all you have to dothe shape control is very easy to work with. Using the shape control, you can draw in both forms and picture boxes. You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. For more on this control, see the other topics in this chapter. Drawing Rounded Rectangles How do you draw rounded rectangles with the shape control? You start by clicking the Shape Control tool in the Visual Basic toolbox and drawing that control to match the size and location you want your new figure to have. To draw a rounded rectangle, you simply set the controls Shape property to VbShapeRoundedRectangle. Thats all you have to dothis control is very easy. Using the shape control, you can draw in both forms and picture boxes. You can also set the width of the shapes drawing line with the BorderWidth property, and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. For more on this control, see the other topics in this chapter. Drawing Rounded Squares Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\449-452.html (2 of 3) [3/14/2001 1:48:31 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com To draw rounded squares with the shape control, you start by clicking the Shape Control tool in the Visual Basic toolbox and drawing that control to match the size and location you want your new figure to have. To draw a rounded square, you simply set the controls Shape property to VbShapeRoundedSquare. Thats all you have to do. The shape control is easy. Using the shape control, you can draw in both forms and picture boxes. You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. For more on this control, see the other topics in this chapter. Setting Shape Borders: Drawing Width, Dashes, And Dots The Aesthetic Design Department is on the line. Cant you do something about the shapes in your program? Maybe make themdotted? You think, dotted? Visual Basic can help here. Just set the shape controls BorderStyle property. Here are the possible values for the BorderStyle property: " vbTransparent0; transparent " vbBSSolid1 (the default); solid (the border is centered on the edge of the shape) " vbBSDash2; dash " vbBSDot3; dot " vbBSDashDot4; dash-dot " vbBSDashDotDot5; dash-dot-dot " vbBSInsideSolid6; inside solid (the outer edge of the border is the outer edge of the shape) Using this property, you can adjust the border of your shape control as you want it. Heres another way to customize a shape control: you can set the shape controls border width (in other words, the drawing line width) using the shape controls BorderWidth property. Just set that property to the new value you want for the border thickness (the default value is 1). Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\449-452.html (3 of 3) [3/14/2001 1:48:31 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\452-456.html (1 of 3) [3/14/2001 1:48:54 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\452-456.html (2 of 3) [3/14/2001 1:48:54 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com (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. Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\452-456.html (3 of 3) [3/14/2001 1:48:54 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Drawing Thicker, Dotted, And Dashed Lines Using the line control, you can select a line style with the BorderStyle property. Here are the possible values for the line control’s BorderStyle property: • vbTransparent—0; transparent • vbBSSolid—1 (the default); solid • vbBSDash—2; dash • vbBSDot—3; dot • vbBSDashDot—4; dash-dot • vbBSDashDotDot—5; dash-dot-dot • vbBSInsideSolid—6; inside solid To set a line’s width, you use the BorderWidth property (the default value is 1). It seems a little odd to call the line’s style BorderStyle and its width BorderWidth—after all, what is the line a border to? However, those properties are named that way to be consistent with the shape control. TIP: We might also note that the effect of setting the BorderStyle property depends on the setting of the BorderWidth property; if BorderWidth isn’t 1 and BorderStyle isn’t 0 or 6, Visual Basic sets BorderStyle to 1. Drawing A Line Without The IDE Grid When you draw lines in the Visual Basic Integrated Development Environment (IDE), those lines 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). Changing A Line Control At Runtime You can move Visual Basic controls at runtime—why not line controls? You can’t use the Move method to move a line control at runtime, but you can move or resize it by altering its X1, X2, Y1, and Y2 properties. Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\456-460.html (1 of 4) [3/14/2001 1:49:12 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Let’s see an example. In this case, we’ve added four random line controls to a form in a control array, LineControl(0) to LineControl(3). When the user clicks a command button, Command1, we loop over all four lines and arrange them horizontally. Here’s what the code looks like (the measurements are in the Visual Basic default, twips, or 1/1440s of an inch): Private Sub Command1_Click() Dim intLoopIndex As Integer For intLoopIndex = 0 To 3 LineControl(intLoopIndex).X1 = 1000 LineControl(intLoopIndex).X2 = 3500 LineControl(intLoopIndex).Y1 = 1000 + 100 * intLoopIndex LineControl(intLoopIndex).Y2 = LineControl(intLoopIndex).Y1 Next intLoopIndex End Sub The result of this code appears in Figure 14.14. Now we’re moving lines around at runtime. Figure 14.14 Changing line controls at runtime. Using Form Methods To Draw Lines We’ve seen how to draw lines with the line control—but you can use a form method, the Line method, to draw lines directly. The Line method can be an important part of graphic design (especially if you want to draw lines in a loop and don’t want to create a dozen or more line controls), and because we’re covering that topic in this chapter, we’ll look at the line control here. Here’s how you use the Line method: [Form.]Line [(x1, y1)]&45;(x2, y2)[, color] Let’s see an example. Here, we’ll just draw four lines with the Line method when a form first loads. As with other graphic methods, to use this method in the Form_Load() handler, you must set the form’s AutoRedraw property to True. Here’s the code we add to the Load event, making use of the Line method: Private Sub Form_Load() Dim intLoopIndex As Integer For intLoopIndex = 0 To 3 Line (1000, 1000 + 400 * intLoopIndex)-(3500, 1000 + 400 _ Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\456-460.html (2 of 4) [3/14/2001 1:49:12 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com * intLoopIndex) Next intLoopIndex End Sub The result of the preceding code appears in Figure 14.15—you can see the four lines we’ve drawn there. Figure 14.15 Drawing lines with the Line method. TIP: The Line method is often a better choice than line controls if you have a large number of evenly spaced lines to draw, such as when you need to draw a grid or rules. Note, however, that if the user resizes the containing form, you might have to redraw those lines. Using Form Methods To Draw Circles We’ve seen that you can use the shape control to draw circles, but there is also a form method to do the same thing: the Circle method. Here’s how you use the Circle method: [Form.]Circle (x, y), radius[, color] For example, here’s how we draw a few circles in a form using the Circle method (note that as with all graphics methods used in the Form_Load() event handler, you must set the form’s AutoRedraw property to True here): Private Sub Form_Load() Dim intLoopIndex As Integer For intLoopIndex = 1 To 4 Circle (2300, 500 + 400 * intLoopIndex), 400 * intLoopIndex Next intLoopIndex End Sub Running this code yields the result you see in Figure 14.16. Now we’re drawing circles using the form’s Circle method. Figure 14.16 Drawing circles with the Circle method in a form. Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls http://24.19.55.56:8080/temp/ch14\456-460.html (3 of 4) [3/14/2001 1:49:12 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... shown in Figure 15. 25 http://24.19 .55 . 56 : 8080/temp/ch 15\ 489-493.html (3 of 4) [3/14/2001 1 :51 :11 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 15. 25 Using a progress bar http://24.19 .55 . 56 : 8080/temp/ch 15\ 489-493.html (4 of 4) [3/14/2001 1 :51 :11 AM] Visual Basic 6 Black Book: Toolbars, Status... you add a toolbar to a form? http://24.19 .55 . 56 : 8080/temp/ch 15\ 463 - 469 .html (3 of 4) [3/14/2001 1:49:33 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com http://24.19 .55 . 56 : 8080/temp/ch 15\ 463 - 469 .html (4 of 4) [3/14/2001 1:49:33 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars,.. .Visual Basic 6 Black Book: The Frame, Label, Shape, And Line Controls Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com http://24.19 .55 . 56 : 8080/temp/ch14\4 56 - 460 .html (4 of 4) [3/14/2001 1:49:12 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars Chapter 15 and Split Unregistered Version - http://www.simpopdf.com... Click the Panels tab in the property pages, as shown in Figure 15. 20 Figure 15. 20 Adding a panel to a status bar 3 Click the Insert Panel button as many times as you want panels in your status bar 4 Close the property pages by clicking on OK http://24.19 .55 . 56 : 8080/temp/ch 15\ 480-4 85. html (4 of 4) [3/14/2001 1 :50 :55 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars It’s also... in Figure 15. 19 Users can customize the toolbar as they like using that dialog box Figure 15. 19 Using the Customize Toolbar dialog box TIP: If you allow your end user to reconfigure the toolbar control, you can save and restore the toolbar by using the SaveToolbar and RestoreToolbar methods http://24.19 .55 . 56 : 8080/temp/ch 15\ 480-4 85. html (1 of 4) [3/14/2001 1 :50 :55 AM] Visual Basic 6 Black Book: Toolbars,... and select the Properties item in the menu that appears 2 Click the Bands tab in the coolbar’s property pages, as shown in Figure 15. 26 Figure 15. 26 The coolbar property pages http://24.19 .55 . 56 : 8080/temp/ch 15\ 493-497.html (1 of 3) [3/14/2001 1 :51 : 16 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars 3 Add new bands to the coolbar using the Insert Band button Simpo PDF Merge... this property with an image file on disk 5 Close the property pages by clicking on OK That’s it—now when you run the program, the image you’ve selected appears in the panel you’ve chosen, as shown in Figure 15. 23 Figure 15. 23 Displaying images in a status bar http://24.19 .55 . 56 : 8080/temp/ch 15\ 4 85- 489.html (3 of 3) [3/14/2001 1 :51 :03 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars,... to the Visual Basic toolbox, as shown in Figure 15. 3 http://24.19 .55 . 56 : 8080/temp/ch 15\ 489-493.html (2 of 4) [3/14/2001 1 :51 :11 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars 4 To place a progress bar in your form, just add it as you would any control, using the Progress Bar ControlMerge and Split Unregistered Version - http://www.simpopdf.com Simpo PDF tool 5 Set the... the menu that appears 2 Click the Buttons tab in the property pages that open, as shown in Figure 15. 15 Figure 15. 15 The toolbar property pages 3 Insert a new button where you want the combo box to go http://24.19 .55 . 56 : 8080/temp/ch 15\ 474-480.html (3 of 4) [3/14/2001 1 :50 :18 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars 4 Set the new button’s Style property to tbrPlaceholder... MsgBox "You selected " & Combo1.Text End Sub http://24.19 .55 . 56 : 8080/temp/ch 15\ 474-480.html (4 of 4) [3/14/2001 1 :50 :18 AM] Visual Basic 6 Black Book: Toolbars, Status Bars, Progress Bars, And Coolbars That’s all we need—now run the program, as shown in Figure 15. 16 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 15. 16 Adding a combo box to a toolbar When users make a selection . for visual design, but sometimes that’s not enough Visual Basic 6 Black Book: The Frame, Label, Shape, And Line Controls http://24.19 .55 . 56 : 8080/temp/ch14 452 -4 56 . html (2 of 3) [3/14/2001 1:48 :54 . twips). Moving Shapes At Runtime Visual Basic 6 Black Book: The Frame, Label, Shape, And Line Controls http://24.19 .55 . 56 : 8080/temp/ch14 452 -4 56 . html (1 of 3) [3/14/2001 1:48 :54 AM] Simpo PDF Merge and. drawn with the line control. Visual Basic 6 Black Book: The Frame, Label, Shape, And Line Controls http://24.19 .55 . 56 : 8080/temp/ch14 452 -4 56 . html (3 of 3) [3/14/2001 1:48 :54 AM] Simpo PDF Merge and

Ngày đăng: 14/08/2014, 01:20

Mục lục

  • Visual Basic 6 Black Book

    • Table of Contents

    • Introduction

    • Whats on the CD-Rom

    • About the Author

    • Chapter 1 - Visual Basic Overview

      • Chapter 1 - Continued

      • Chapter 1 - Continued

      • Chapter 1 - Continued

      • Chapter 1 - Continued

      • Chapter 1 - Continued

      • Chapter 1 - Continued

      • Chapter 1 - Continued

      • Chapter 2 - The Visual Basic Development Environment

        • Chapter 2 - Continued

        • Chapter 2 - Continued

        • Chapter 2 - Continued

        • Chapter 2 - Continued

        • Chapter 2 - Continued

        • Chapter 2 - Continued

        • Chapter 2 - Continued

        • Chapter 3 - The Visual Basic Language

          • Chapter 3 - Continued

          • Chapter 3 - Continued

Tài liệu cùng người dùng

Tài liệu liên quan