VB Graphics
VB Graphics
In this section, I will show you how to use the Print method to display text on the form or
the PictureBox control in various styles or colors.
In code example 1 and code example 2, the texts are printed in the (0, 0) position.
You can print text on the PictureBox control. The following code clarifies this.
Example 3:
Code:
Private Sub cmdPrint_Click()
Picture1.Print "Hello world"
Picture1.Print "Welcome to Visual Basic 6"
Picture1.Print "Visual Basic is awesome!"
End Sub
You can change the printing position from (0, 0) to other. Examine the following code.
Example 4:
Code:
Private Sub Command1_Click()
CurrentX = 500
CurrentY = 1000
Form1.Print "Hello world"
CurrentX = 500
CurrentY = 1300
Form1.Print "Welcome to Visual Basic 6"
Form1.Print "Visual Basic is awesome!"
End Sub
Output of code example 4:
You can display text using different styles, sizes and colors. Consider the following code
example.
Example 5:
Code:
Private Sub cmdPrint_Click()
Form1.FontSize = 18
'Form1 object name is omitted
ForeColor = vbBlue
Font = "MS Serif"
Print "This is a new text"
End Sub
This section shows you how to draw points using the PSet method and how to use the
Step keyword with the PSet method.
The Pset method allows you to draw a point. You need to specify the coordinate i.e. the
drawing position. You can also pass a color constant that is an optional argument in the
PSet method.
Example 6:
Code:
Private Sub cmdShow_Click()
DrawWidth = 10
PSet (100, 500)
End Sub
Example 6:
Code:
Private Sub Command1_Click()
Picture1.DrawWidth = 10
Picture1.PSet (100, 500)
End Sub
The Step keyword allows you to draw in a position relative to the current position. See
the example.
Example 7:
Code:
Private Sub cmdShow_Click()
DrawWidth = 10
CurrentX = 500
CurrentY = 500
PSet Step(0, 0)
End Sub
The above code draws a point in the (0, 0) position relative to the current position that is
(500, 500).
That means, the point is drawn in the (500, 500) position. But this is (0, 0) position
relative to the current position.
Drawing lines
The Line method lets you draw lines in Visual Basic 6. You need to specify the starting
point and the finishing point of the line in the argument. You may also specify the color
of the line. This is optional, though.
A simple line
The following code example shows how to draw a simple line using the Line method in
Visual Basic 6.
Example 8:
Code:
Private Sub cmdShow_Click()
DrawWidth = 5
'A hyphen is required between the points
Line (0, 0)-(2000, 2000), vbBlue
End Sub
Output of code example 8:
Form’s DrawStyle property lets you draw lines using a particular style. The constant
values of the DrawStyle property are 0 (vbSolid), 1-(vbDash), 2-(vbDot), 3-
(vbDashDot), 4-(vbDashDotDot), 5-(vbTransparent) and 6-(vbInsideSolid). The
default value is 0, vbSolid. You may use the numeric constant or the symbolic constant
such as vbSolid, vbDash etc to change drawing styles in your code.
NOTE: The DrawStyle property does not work if the value of DrawWidth is other than 1.
Example 9:
Code:
DrawWidth = 1
DrawStyle = 1
'A hyphen is required between the points
Line (0, 0)-(2000, 2000), vbBlue
DrawStyle = vbDashDot
Line (100, 900)-(2800, 2800), vbRed
You can draw a circle using the Circle method in Visual Basic 6. You may also use the
Circle method to draw different geometric shapes such as ellipses, arcs etc. You need
to specify the circle’s center and radius values to draw a circle using the Circle method.
A simple circle
The following code draws a simple circle using the Circle method in Visual Basic 6.
Example 10:
Code:
Private Sub cmdShow_Click()
DrawWidth = 3
Circle (1800, 1800), 1000, vbRed
End Sub
In the above code, (1800, 1800) is the circle’s center, and the radius value is 1000. The
color constant ‘vbRed’ is an optional argument.
Output of code example 10:
The following code example shows how to fill a circle with color in Visual Basic 6.
Example 11:
Code:
Private Sub cmdShow_Click()
FillStyle = vbSolid
FillColor = &H80C0FF
DrawWidth = 3
Circle (1800, 1800), 1000, vbRed
End Sub
Output of code example 11:
Code:
Private Sub Command2_Click()
' Draw a circle centered at 2000,1500 with radius equal 800
Circle (2000, 1500), 800, vbYellow
' Draw a vertical line from center
Line (2000, 1500)-Step(0, 800)
' Draw a horizontal line from center
Line (2000, 1500)-Step(800, 0)
End Sub
Draw Arc
Define Pi
const Pi = 3.1415927
To draw a red arc whose center is at (300, 200) whose radius is 150, whose starting angle is 0 and whose
ending angle is Pi:
or
picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,0 ,Pi
or
picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,Pi, 2 * Pi
picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,0 ,Pi/2
or
picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,Pi/2, Pi
You can tell Visual Basic to draw the radii of the arc, creating an arc segment, by negating the angles (put
a minus sign in front of the angle). Visual Basic doesn't read the negative as a negative radian, it reads is
only as a signal to draw the radius.
or
picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,-Pi/2, -Pi
If you need a "negative zero", you must use a "small negative", such as -0.001:
or
picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,-0.001 ,-Pi/2
Code:
Private Sub Command3_Click()
Const Pi = 3.1415927
FillStyle = 0
FillColor = vbRed
Circle (1000, 1000), 500, , -0.001, -Pi / 2
End Sub
Ellipses and Aspect Ratio
Aspect Ratio is the ratio of the vertical scaling to the horizontal scaling. In simpler terms for an ellipse, it
represents the "vertical stretch".
You can indicate the aspect ratio by adding a sixth parameter. If you just want an ellipse, leave the third
(color), fourth (starting angle), and fifth (ending angle) parameters blank.
To draw an ellipse whose center is at (300, 200), whose horizontal radius is 75, and whose aspect ratio is
2:
To draw an ellipse that is "flat" in appearance, we must use an aspect ratio less than one in order to make
the vertical radius shorter than the horizontal radius.
The following ellipse has an aspect ratio of 0.5, thus making it half as high as it is wide:
Rectangle
The Line method can be used to draw different geometric shapes such as rectangle, triangle
etc. The following example shows you how to draw a rectangle using the Line method in Visual
Basic 6.
Example 12:
Code:
Private Sub cmdShow_Click()
DrawWidth = 3
Line (300, 300)-Step(4000, 2000), vbBlue, B
End Sub
The B argument in the Line method lets you draw a rectangle.
Output of code example 12:
End Sub
Load Picture
The LoadPicture function sets a picture to the PictureBox control or the form object. It
requires the file path as an argument. The following example shows you how to use the
LoadPicture function.
Example 13:
Code:
Private Sub cmdShow_Click()
Picture1 = LoadPicture("D:\pic.JPG")
End Sub
Output of code example 13: