Graphics Techniques With Visual Basic
Graphics Techniques With Visual Basic
Graphics Methods
Graphics methods apply to forms and picture boxes (remember a picture box is
like a form within a form). With these methods, we can draw lines, boxes, and
circles. Before discussing the commands that actually perform the graphics
drawing, though, we need to look at two other topics: screen management and
screen coordinates.
In single program environments (DOS, for example), when something is drawn on
the screen, it stays there. Windows is a multi-tasking environment. If you switch
from a Visual Basic application to some other application, your Visual Basic form
may become partially obscured. When you return to your Visual Basic
application, you would like the form to appear like it did before being covered. All
controls are automatically restored to the screen. Graphics methods drawings
may or may not be restored - we need them to be, though. To accomplish this, we
must use proper screen management.
The simplest way to maintain graphics is to set the form or picture box's
AutoRedraw property to True. In this case, Visual Basic always maintains a
copy of graphics output in memory (creates persistent graphics). Another way
to maintain drawn graphics is (with AutoRedraw set to False) to put all graphics
commands in the form or picture box's Paint event. This event is called whenever
an obscured object becomes unobscured. There are advantages and
disadvantages to both approaches (beyond the scope of discussion here). For
now, we will assume our forms won't get obscured and, hence, beg off the
question of persistent graphics and using the AutoRedraw property and/or Paint
event.
All graphics methods described here will use the default coordinate system:
Scale Width
0,0
Scale Height
Note the x (horizontal) coordinate runs from left to right, starting at 0 and extending
to ScaleWidth - 1. The y (vertical) coordinate goes from top to bottom, starting at
0 and ending at ScaleHeight - 1. Points in this coordinate system will always be
referred to by a Cartesian pair, (x, y). Later, we will see how we can use any
coordinate system we want.
PSet Method:
To set a single point in a graphic object (form or picture box) to a particular color,
use the PSet method. We usually do this to designate a starting point for other
graphics methods. The syntax is:
where ObjectName is the object name, (x, y) is the selected point, and Color is
the point color (discussed in the next section). If the ObjectName is omitted, the
current form is assumed to be the object. If Color is omitted, the object's
ForeColor property establishes the color. PSet is usually used to initialize some
further drawing process.
This form has a ScaleWidth of 3975 (Width 4095) and a ScaleHeight of 2400
(Height 2805). The command:
3975