Working with Graphics in Visual Basic 6
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.
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.
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.
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.
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.
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.
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.
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.
Specifying printing positions
You can change the printing position from (0, 0) to other. Examine the following code.
Printing a customized text
You can display text using different styles, sizes and colors. Consider the following code example.
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.
Relative positioning with the Step keyword
The Step keyword allows you to draw in a position relative to the current position. See the example.
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:
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.
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.
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.
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.
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.
Output of code example 12:
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.
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.
Re: Working with Graphics in Visual Basic 6
It was much better at your post. I learned many things in this post. The more you post, so that the user can learn to play a lot more like me.
Once you get more tips a:snore:bout online and you can see at this
Re: Working with Graphics in Visual Basic 6
|All times are GMT +5.5. The time now is 08:10.|