Graphic Control (Methods)
Graphic Control (Methods)
Other than using the line and shape controls to draw graphics on the form, you can also use some graphics
designing method.
The Pset Method
The Pset method draw a dot on the screen, it takes the syntax
Pset (x , y ), color
(x,y) is the coordinates of the point and color is its color. To specify the color, you can use the color codes or
the standard VB color constant such as VbRed, VbBlue, VbGeen and etc. For example, Pset(100,200), VbRed
will display a red dot at the (100,200) coordinates.
The Pset method can also be used to draw a straight line on the form. The procedure is
For x= a to b
Pset(x,x)
Next x
This procedure will draw a line starting from the point (a,a) and to the point (b,b). For example, the following
procedure will draw a magenta line from the point (0,0) to the point (1000,1000).
For x= 0 to 100
Pset(x,x) , vbMagenta
Next x
The Line Method
Although the Pset method can be used to draw a straight line on the form, it is a little slow. It is better to use the
Line method if you want to draw a straight line faster. The format of the Line command is shown below. It
draws a line from the point (x1, y1) to the point (x2, y2) and the color constant will determine the color of the
line.
Line (x1, y1)-(x2, y2), color
For example, the following command will draw a red line from the point (0, 0) to the point (1000, 2000).
Line (0, 0)-(1000,2000), VbRed
The Line method can also be used to draw a rectangle. The syntax is
Line (x1-y1)-(x2, y2), color, B
The four corners of the rectangle are (x1-y1), (x2-y1), (x1-y2) and (x2, y2)
Another variation of the Line method is to fill the rectangle with a certain color. The syntax is
Line (x1, y1)-(x2, y2), color, BF
If you wish to draw the graphics in a picture box, you can use the following syntaxes
Picture1.Line (x1, y1)-(x2, y2), color
Picture1.Line (x1-y1)-(x2, y2), color, B
Picture1.Line (x1-y1)-(x2, y2), color, BF
Picture1.Circle (x1, y1), radius, color
The Bar Graph Plotter
We shall use the knowledge we gained from the Line method to create a bar graph. In this program, we shall
insert a picture box for drawing the bar graph. In addition, we insert six text boxes for accepted the user input.
We also insert two command buttons for drawing and reseting. Besides that, we need to define a new origin
using the Scale method, otherwise, the bar graph will be upside down. The code is
Picture1.Scale (0, 5000)-(5000, 0)
The Circle Method
The Circle Method Draws a circle, ellipse, or arc on an object such as a form or a picture box. The syntax is :
object.Circle (x, y), radius, color, start, end, aspect
Where
(x,y) indicate the coordinates of the center of the circle
color indicates the color of the circle's outline
start and end indicate the starting position and ending position in radian of an arc or a sector
aspect indicates the aspect ratio of the circle. The default value is 1.0, which yields a perfect (non-
elliptical) circle.
For example, the procedure
Form1.Circle (400, 400),500, VbRed
draws a circle centered at coordinates(400, 400) with a radius of 500 twips and a red outline on the form.
Actually you can use the keyword Me to replace Form1, as follows
Me.Circle (400, 400),500, VbRed
In addition, usually we need to re-define the coordinates system as the default coordinates of the origin (0,0) is
at the top left corner. We would like to move it to somwehere in the middle. We can define the coordinates
using the Scale method. The syntax is
Scale(x1,y1)-(x2,y2)
where (x1,y1) is the new coordinates of the upper left corner and (x 2,y2) is the new coordinates of the lower right
corner.
For example,
Scale (-2000,2000) - (2000,-2000)
changes the the coordinates of the upper left corner to (-2000,2000) and the coordinates of the lower right
corner to (2000,-2000). Besides, be aware that we the measurement we use here is twip, 1cm is about 567 twip.
In addition, we must also know that Circle has a few properties, namely FillColor, FillStyle, DrawWidth,
DrawMode and DrawStyles. The DrawStyles are vbDot, vbDash, vbDashDot, vbSolid and vb DashDotDot.
However, if the DrawWidth property is set to a value more than 1, the Drawstyles has no effect.
The PaintPicture method
PictureBox controls are equipped with a very powerful method that enables the programmer to perform a wide
variety of graphic effects, including zooming, scrolling, panning, tiling, flipping, and many fading effects: This
is the PaintPicture method. (This method is also exposed by form objects, but it's most often used with
PictureBox controls.) In a nutshell, this method performs a pixel-by-pixel copy from a source control to a
destination control. The complete syntax of this method is complex and rather confusing:
DestPictBox.PaintPicture SrcPictBox.Picture, destX, destY, [destWidth], [destHeight], [srcX], [srcY2],
[srcWidth], [srcHeight], [Opcode])
The only required arguments are the source PictureBox control's Picture property and the coordinates inside the
destination control where the image must be copied. The destX / destY arguments are expressed in the
ScaleMode of the destination control; by varying them, you can make the image appear exactly where you
want. For example, if the source PictureBox control contains a bitmap 3000 twips wide and 2000 twips tall, you
can center this image on the destination control with this command:
picDest.PaintPicture picSource.Picture, (picDest.ScaleWidth - 3000) / 2, (picDest.ScaleHeight - 2000) / 2