0% found this document useful (0 votes)
15 views

VB Graphics

Uploaded by

gyangmargitar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

VB Graphics

Uploaded by

gyangmargitar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Printing text

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.

Printing on the form

The following code prints some text on the form.


Example 1:
Code:
Private Sub cmdPrint_Click()
Form1.Print "Hello world"
Form1.Print "Welcome to Visual Basic 6"
Form1.Print "Visual Basic is awesome!"
End Sub

The above code can be written in the following way too.


Example 2:
Code:
Private Sub cmdPrint_Click()
Print "Hello world"
Print "Welcome to Visual Basic 6"
Print "Visual Basic is awesome!"
End Sub
the above code, the Print method is called without the object name. Here ‘Form1’ is the object
name. When you’re writing code inside the form module, you may omit the form’s name while
invoking its methods.

In code example 1 and code example 2, the texts are printed in the (0, 0) position.

Output of code example 1 and code example 2:


Printing on the PictureBox control

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

Output of code example 3:

Specifying printing positions

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:

Printing a customized text

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

Output of code example 5:


Drawing points

This section shows you how to draw points using the PSet method and how to use the
Step keyword with the PSet method.

Drawing points using 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

Output of code example 6:

Example 6:
Code:
Private Sub Command1_Click()
Picture1.DrawWidth = 10
Picture1.PSet (100, 500)
End Sub

Relative positioning with the Step keyword

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.

Output of code example 7:

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:

A line with drawing styles

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

Output of code example 9:


Drawing circles

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:

A circle filled with color

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:

picA.Circle (300, 200), 150, RGB(255, 0, 0), 0, Pi

or

picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,0 ,Pi

To draw the bottom half of a circle:

picA.Circle (300, 200), 150, RGB(255, 0, 0), Pi, 2 * Pi

or

picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,Pi, 2 * Pi

To draw the first quarter of a circle:

picA.Circle (300, 200), 150, RGB(255, 0, 0), 0, Pi/2


or

picA.Forecolor = RGB(255, 0, 0)
picA.Circle (300, 200), 150, ,0 ,Pi/2

To draw the second quarter of a circle:

picA.Circle (300, 200), 150, RGB(255, 0, 0), Pi/2, Pi

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.

picA.Circle (300, 200), 150, RGB(255, 0, 0), -Pi/2, -Pi

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:

picA.Circle (300, 200), 150, RGB(255, 0, 0), -0.001, -Pi/2

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:

picA.Circle (300, 200), 75, , , , 2

The first parameter (or position), (300, 200), is the center.


The second parameter, 75, is the radius.
The third parameter, the color, is blank.
The fourth parameter, the starting angle of the arc, is blank.
The fifth parameter, the ending angle of the arc, is blank.
The sixth parameter, 2, is the aspect ratio.

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:

picA.Circle (300, 200), 150, , , , 0.5

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:

Example 12: Triangle


Code:
Private Sub Command1_Click()
FillStyle = vbSolid
FillColor = &H80C0FF
DrawWidth = 1
Line (2000, 300)-(1000, 2000), vbBlue
Line (1000, 2000)-(3000, 2000), vbBlue
Line (3000, 2000)-(2000, 300), vbBlue
End Sub
Displaying an image

Example 12: Triangle


Code:
Private Sub Command1_Click()
FillStyle = vbSolid
FillColor = &H80C0FF
DrawWidth = 5
Line (2000, 300)-(1000, 2000), vbBlue
Line -(3000, 2000), vbBlue
Line -(2000, 300), vbBlue
End Sub

Example 12: House


Code:
Private Sub Command1_Click()
Dim X1 As Integer
Dim Y1 As Integer
Dim X2 As Integer
Dim Y2 As Integer
Dim X3 As Integer
Dim Y3 As Integer
' Initialise Coordinates of rectangle
X1 = 4200: Y1 = 2000
X2 = 6200: Y2 = 3000
X3 = 5200: Y3 = 1000
Line (X1, Y1)-(X2, Y1)
Line -(X2, Y2)
Line -(X1, Y2)
Line -(X1, Y1)
DrawStyle = vbDashDot
Line -(X3, Y3)
Line -(X2, Y1)
CurrentX = X1 + 50
CurrentY = Y1 + 50
' Define Font Size
Font.Size = 18
' Print the text at cursor location
Print "Hello there!"

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:

You might also like