Working with graphics control and using grphic methods

Working with graphics is easy in Visual Basic 6. VB6 gives you the flexibility and power to make graphical applications in easy steps. It is rich in graphics related features. The built-in methods, properties and events allow you to use these features most effectively. 

In this tutorial, you will learn about various graphic methods, properties and techniques. The graphic methods and properties let you perform some graphical operations. More specifically, they allow you to draw points, lines, circles, rectangles, ellipses and other shapes. You will also learn how to display text and images. Finally, this tutorial introduces you to the Paint event. 

This tutorial is for the beginner learners. So the concepts are presented in the simplest way possible. Code examples have been used wherever they became necessary.

Graphic methods



The graphic methods allow you to draw on the form and the PictureBox control. In Visual Basic 6, graphic methods are only supported by the form object and the PictureBox control. However, the later versions of Visual Basic allow you to use them with other objects and controls. 

First of all, the graphic methods in this section are discussed only to give you a basic idea about them. Later in this tutorial, I will show you how to use them in your code to perform certain operations like printing text, drawing shapes etc.

The common graphic methods are explained below.
  • Print: Print is the simplest graphic method in Visual Basic 6. This method has been used throughout the earlier versions of the language. It prints some text on the form or on the PictureBox control. It displays texts.
  • Cls: The Cls method is another simple graphic method that is used to clear the surface of the form or the PictureBox control. If some texts are present, you can use the Cls method to remove the texts. It clears any drawing created by the graphic methods.
  • Point: The Point method returns the color value from an image for a pixel at a particular point. This method is generally used to retrieve color values from bitmaps.
  • Refresh: The refresh method redraws a control or object. In other words, it refreshes the control. Generally, controls are refreshed automatically most of the times. But in some cases, you need to refresh a control’s appearance manually by explicitly invoking the Refresh method.
  • PSet: The PSet method sets the color of a single pixel on the form. This method is used to draw points.
  • Line: The Line method draws a line. Using the Line method, you can also draw other geometric shapes such as rectangle, triangle etc.
  • Circle: The Circle method draws a circle. Using the Circle method, you can also draw other geometric shapes such ellipses, arcs etc.
  • PaintPicture: The PaintPicture method displays an image on the form at run-time.
  • TextHeight: The TextHeight method returns the height of a string on the form at run-time.
  • TextWidth: The TextWidth method returns the width of a string on the form at run-time.

The LoadPicture function



The LoadPicture function loads a picture to the form or to the PictureBox control. It sets the picture to the control in order to display it. The function takes the file path as an argument. The LoadPicture function allows you to set pictures at run-time.

Example:
Code:
Picture1 = LoadPicture(“C:\MyPic.JPG”)
Here, Picture1 is the PictureBox control. When this code is executed the picture is loaded and set to the PictureBox control. If Visual Basic cannot find the picture in the specified location, it throws a run-time error ‘53’.

The RGB function



The RGB function returns an integer, a color code which is used to set colors in Visual Basic code. The RGB color code is a combination of red, green and blue colors. Consider the following example to understand RGB function in VB6.

Example:
Code:
Form1.BackColor = RGB (120, 87, 55)
The RGB color is set as the background color of the form object. The first, second and the third arguments represent red, green and blue colors respectively. The color value is an integer. These values are in a range of 0 to 255. So you can use any value between 0 and 255 to obtain a color. 

Graphic properties



The graphic properties are useful while working with the graphic methods. Some of the form's properties and some of the PictureBox's properties are the graphics properties. 
The common graphic properties are discussed in this section. You’ll learn more about them using code examples later in this tutorial.

Consider the following graphic properties.
  • DrawMode: The DrawMode property sets the mode of drawing for the appearance of output from the graphic methods. In the DrawMode property, you can choose from a variety of values.
  • DrawStyle: The DrawStyle property sets the line style of any drawing from any graphic methods. It allows you to draw shapes of different line styles such as solid, dotted, dashed shapes etc.
  • DrawWidth: The DrawWidth property sets the line width of any drawing from any graphic methods. While drawing shapes, you can control the thickness of the lines using this property.
  • FillColor: The FillColor property is used to fill any shapes with a color. You may use the symbolic color constants to fill your shapes. You may also use the color codes as well as the RGB function.
  • FillStyle: The FillStyle property lets you fill shapes in a particular filling style.
  • ForeColor: The ForeColor property is used to set or return the foreground color.
  • AutoRedraw: Set the AutoRedraw property to True to get a persistent graphics when you’re calling the graphic methods from any event, but not from the Paint event.
  • ClipControls: Set the ClipControls property to True to make the graphic methods repaint an object.
  • Picture: The Picture property is used to set a picture. Pictures can be set both at design time and run-time.

Run-time graphic properties



CurrentX and CurrentY are the run-time properties which are used to set and return the position of a shape or point at run-time.
  • CurrentX: The CurrentX property sets or returns the horizontal coordinate or X-coordinate of the current graphic position at run-time.
  • CurrentY: The CurrentY property sets or returns the vertical coordinate or Y-coordinate of the current graphic position at run-time.

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
In 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()
    picMyPictureBox.Print "Hello world"
    picMyPictureBox.Print "Welcome to Visual Basic 6"
    picMyPictureBox.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 cmdPrint_Click()
    CurrentX = 500
    CurrentY = 1000
    Print "This is a new text"
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:



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:



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:



Displaying an image



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:




The Paint event



The Paint event fires automatically when the form is refreshed. For instance, the Paint event fires when you uncover areas in a form or when you resize the form. If the AutoRedraw property is set to True, this event will not be invoked. And while resizing, if you shrink the form, this event does not fire.

You may use the necessary graphic methods inside the Paint event procedure so that whenever the form is refreshed, the graphic methods are automatically called.

Example 14:

Code:
Private Sub Form_Paint()
    DrawWidth = 5
    Circle (Rnd * 3000, Rnd * 7000), Rnd * 800, vbYellow
End Sub
New circles are drawn automatically when you resize the form. New circles are drawn in random positions and with random sizes.