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


Visual Basic can help out here because picture boxes give you some rudimentary graphics-drawing capabilities that you can make use of in code. In particular, you can draw lines and circles, and set points to particular colors in picture boxes using the following methods (note, by the way, that you can also use all the following methods with forms as well as picture boxes).

Some of the following methods make use of CurrentX and CurrentY; these are properties that you can set in a picture box. For example, if you omit the first set of coordinates when using the Line() method, Visual Basic draws the line from the location (CurrentX, CurrentY).

You may want to specify measurements to the graphics methods using pixels, not the default twips, and you can change the measurements in a picture box to pixels by setting its ScaleMode property this way:

Private Sub Form_Load()
    Picture1.ScaleMode = vbPixels
End Sub

We’ll start working with the drawing methods of picture boxes now, starting with the Circle() method.

Drawing Circles

You use the Circle() method to draw circles:

PictureBox.Circle [Step] (  x, y), radius, [color, start, end, aspect]

Here are the arguments you pass to Circle():

  Step—Keyword specifying that the center of the circle, ellipse, or arc is relative to the current coordinates given by the CurrentX and CurrentY properties of object.
  x, ySingle values indicating the coordinates for the center point of the circle, ellipse, or arc. The ScaleMode property of object determines the units of measure used.
  radiusSingle value indicating the radius of the circle, ellipse, or arc. The ScaleMode property of object determines the unit of measure used.
  colorLong integer value indicating the RGB color of the circle’s outline. If omitted, the value of the ForeColor property is used. You can use the RGB function or QBColor function to specify the color.
  start, end—Single-precision values. When an arc or a partial circle or ellipse is drawn, start and end specify (in radians) the beginning and end positions of the arc. The range for both is –2 pi radians to 2 pi radians. The default value for start is 0 radians; the default for end is 2 * pi radians.
  aspect—Single-precision value indicating the aspect ratio of the circle. The default value is 1.0, which yields a perfect (nonelliptical) circle on any screen.

As an example, we draw a circle in a picture box with this code:

Private Sub Command1_Click()
    Picture1.Circle (80, 70), 50
End Sub

The result of this code appears in Figure 10.9. If there were an image already in the picture box, the circle would appear drawn on top of it.


Figure 10.9  Drawing a circle in a picture box.

Drawing Lines

You use the Line() method to draw lines:

PictureBox.Line [Step] (  x1, y 1) [Step] ( x2, y2), [ color], [B][F]

Here are the arguments you pass to Line():

  Step—Keyword specifying that the starting point coordinates are relative to the current graphics position given by the CurrentX and CurrentY properties.
  x1, y1Single values indicating the coordinates of the starting point for the line or rectangle. The ScaleMode property determines the unit of measure used. If omitted, the line begins at the position indicated by CurrentX and CurrentY.
  Step—Keyword specifying that the end-point coordinates are relative to the line starting point.
  x2, y2Single values indicating the coordinates of the end point for the line being drawn.
  colorLong integer value indicating the RGB color used to draw the line. If omitted, the ForeColor property setting is used. You can use the RGB function or QBColor function to specify the color.
  B—If included, causes a box to be drawn using the coordinates to specify opposite corners of the box.
  F—If the B option is used, the F option specifies that the box is filled with the same color used to draw the box. You cannot use F without B. If B is used without F, the box is filled with the current FillColor and FillStyle. The default value for FillStyle is transparent.

Setting Points

You use PSet() to set points in a picture box:

PictureBox.PSet [Step] (  x, y), [ color]

Here are the arguments you pass to PSet():

  Step—Keyword specifying that the coordinates are relative to the current graphics position given by the CurrentX and CurrentY properties.
  x, ySingle values indicating the horizontal (x-axis) and vertical (y-axis) coordinates of the point to set.
  colorLong integer value indicating the RGB color specified for point. If omitted, the current ForeColor property setting is used. You can use the RGB function or QBColor function to specify the color.


TIP:  In a picture box, you set the color of figures with the ForeColor property and the fill color with the FillColor property.


TIP:  If you want your images to persist (in other words, be redrawn automatically when needed), set the picture box’s AutoRedraw property to True.

Using Image Lists With Picture Boxes

When handling images, it’s often useful to use image lists. An image list is an invisible control whose only purpose is to hold images. A common thing to do is to load images into an image list and then when they’re all loaded (and stored in memory, not on the disk), place them rapidly into picture boxes as needed.


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.