Preliminaries
Graphical effects can be achieved with only a few simple commands. These commands draw on the screen at specified pixel coordinates. A pixel is a tiny dot on the screen, which can be comparatively larger or smaller depending on your screen resolution. Two popular resolutions are 640x480 (e.g. 640 pixels wide and 480 pixels tall) and 800x600 (800 pixels wide and 600 pixels tall).
Unfortunately, VB doesn't automatically draw things using the pixel coordinate system. Each form has a ScaleMode property that deteremines how the drawing commands are used. Change it from
"1 - Twip"
to "3 - Pixel"
.
For simplicity's sake, you can specify color with the RGB()
command, which specifies Red, Green, and Blue values for a color; these values can be from 0 to 255. Thus, RGB(255, 0, 0)
would be all Red, no Green, and no Blue. RGB(255, 255, 255)
Would be all Red, all Green, and all Blue (e.g. white).
Simpler Stuff
You can put a simple dot on the screen with the command Pset (x, y), RGB(red, green, blue)
, where x
and y
are coordinates for a pixel.
For a variety of reasons, (0,0) is the upper-left hand corner of the window. Thus, Pset (50, 20), RGB(0, 0, 255)
would place a bright blue dot 50 pixels to the right and 20 pixels down from the upper-left hand corner of the program window.
Lines and Boxes
The next most obvious graphical command is Line (x1, y1)-(x2, y2), RGB(red, green, blue)
, which (obviously) draws a line.
You can draw a box (e.g. rectangle) by adding , B
to the end of a line command, like this: Line (30, 30)-(90, 50), B
. You can also draw a filled box (e.g. a solid rectangle) by adding F
to the end of that: Line (30, 30)-(90, 50), BF
Circular Reasoning
Another helpful thing to draw is a circle: Circle (x, y), radius, RGB(red, green, blue)
, which draws a circle with (x, y) at the origin and a radius of radius
.
If you want something that's not a perfect circle, you can specify what I call the "ovalness", which is a decimal number. If the "ovalness" is between 0 and 1 you get an oval stretched horizontally; if the "ovalness" is greater than 1 you get a circle stretched vertically (basically, 1/2 is the reverse of 2, 1/3 is the reverse of 3, etc.). This should be added to the Circle
command after three commas, like this:
Circle (75, 75), 20, RGB(0, 181, 0), , , 1 / 3
The Intro to Visual Basic homepage is hosted by GeoCities