Intrinsic Ctrls
ActiveX Ctrls
Cmd Reference
File Handling

Drag and Drop
Error Handling
Database Controls

Internet File Transfers
Perl Commands
Web Page Forms

GBIC >> VB >> Tutorials >> Graphics Commands
Graphics Commands
In VB there are a limited number of graphics features, but mixed with the judicious use of the Windows API a programmer can create some pretty fair graphics applications. When VB falls short, there is also no shortage of third party OCXs which can step in and add to the capabilites of VB.

Visual Basic Graphics

In VB, graphics capabilities are usually associated with drawing lines, boxes, or otherwise manipulating the display. Graphics are usually created on something, which in the case of VB is either the printer, the screen, or the picture control. All three represent a surface to which graphics can be applied. No other control supplied with VB supports the graphics features.

I'll first walk through the graphical methods and properties which VB supports and then discuss them one at a time to help you understand how they play together.

Here are graphics methods which VB supports.

  • Circle - as you expect, draws a circle or an ellipse
  • Cls - clears all graphics
  • Line - draws a straight line
  • PaintPicture - draws the contents of a graphics file
  • Point - returns the color of a point
  • Print - used for printing text
  • Pset - sets the color of a specific pixel

Likewise, here are the graphics properties which VB supports.

  • AutoRedraw - Determines if graphics are automatically redrawn if something moves in front of them
  • ClipControls - Determines how repainting of graphics is handled
  • DrawMode - Sets the appearance of a graphics method
  • DrawStyle - Sets the line style for output from graphics methods
  • DrawWidth - Sets the line width for output from graphics methods
  • FillColor - Sets the color used to fill circles, lines and boxes
  • FillStyle - Sets the pattern used to fill circle, lines and boxes
  • FontTransparent - Determines if the font is printed with a transparent background
  • Palette - Sets the image containing the palette to use for a control
  • PaletteMode - Determines which palette to use for the controls on a object
  • RightToLeft - Indicates the text display direction.
  • ScaleHeight - Sets the height of the client area
  • ScaleLeft - Sets the starting value of the left of the client area
  • ScaleTop - Sets the starting value of the top of the client area
  • ScaleWidth - Sets the width of the client area
  • ScaleMode - Sets the units of the scale


One of the first things about objects you need to undestand is the concept of scales. The obvious question to ask about an object or graphic on a screen is "How big is it?". The answer to the questions can depend on the units chosen for the measurement. Inches, centimeters, millimeters, and pixels are all common units of measurement so you won't be surprised to know that VB supports all of these. Their definition should be self- explanatory.

VB also supports other units of measuring the size of something on the screen. Specifically, it supports:

  • Points
    Points are the same as you're used to seeing associated with a font. 72 points make an inch. Typically point sizes for typed text range from 10-12 for most documents.
  • Twips
    Microsoft has also introduced a unit known as a twip, where there are 1440 twips to the inch. The twip gives more resolution to measurements than the point (1 twip = 1/20 of a point), making possible more accurate graphics but still with units tied back to the printing industry's standard unit of measurement, the point. There are two properties available which help in converting units, the twipsperpixelX and twipsperpixelY. Both can be used to convert from pixels to twips. Both forms and the printer object support these two properties.
  • Characters
    Uses fixed twips (120 twips per horizontal character and 240 twips per vertical character) definitions. This doesn't seem to get much use, but if you have some great places where it provides an advantage, then I'd like to hear about it.
  • User
    In this case, the name "user" means user-defined. This is one of the most powerful units you can use as a scale. Here, you define the number of units within the client area (top to bottom is height, left to right is width). The advantage of this is that you can use any size window with the exact same code! Sizing the window simply sizes the graphics that you create.
The ScaleMode property simply identifies which units are to be used. You can set it to any of the units I've already discussed above. Once you pick a scalemode, VB adjusts the size properties of the client area to match the selection.

A point to remember is that when you create graphics, you'll be creating them in the client area of the window. In general, you'll be using an X/Y position within the client area to direct VB to place graphics. The bounds of the client area are defined by the properties ScaleWidth and ScaleHeight. VB also lets you pick the top/left starting coordinates of the client area, using the properties ScaleTop and ScaleLeft. This can be useful if you know that your graphics equation only apply to an area some distance away from the coordinate origin (X=0 and Y=0).

Using Graphics Methods
As you saw up above, there are only 7 graphics methods. It's really just 6 if you consider that the PRINT method is in a class of its own. Since I already covered printing in an earlier tutorial, let's just walk through each of the remaining 6 graphics methods and see how to use them. For this exercise, I assume we have a picturebox named Picture1 on a form.

  • Circle
    In this code, I simply draw 10 circles, progressively moving the center to the right starting from the coordinate of X=500.
    For i = 1 to 1000 Step 100
       picture1.circle (i + 500, 1000), 400, vbGreen
    Next i
    The circle syntax can be pretty simple:
    picture1.circle (XCenter, YCenter), Radius, Color
    There are other things you can do as well, so check out HELP for a listing of all the arguments that are available.
  • Cls
    It doesn't get any simpler:
    The .cls methods will erase all graphics which have been drawn on the object, in this case the picturebox.
  • Line
    Similar to the use of the circle method, this example simply creates 10 vertical lines, positioned from left to right.
    For i = 1 to 1000 Step 100
       picture1.line (i+500,500)-(i+500,4000), vbBlue
    Next i
    The line method can also draw boxes by simply adding an extra argument to the code as follows:
    For i = 1 to 4000 Step 400
       picture1.line (i+200,500)-(i+400,3000), vbBlue, BF
    Next i
    The BF added at the end of the line of code simply tells VB to draw a filled box instead of a line.
  • PaintPicture
  • Point
    i = picture1.point (100,500)
    Here, the color of the point located at X=100 and Y=500 is assigned to the variable i. In my own experience I haven't had any reason to use this method.
  • Pset
    This is the most basic drawing tool VB has to offer. With PSET you can set the color of any point within the client area of the drawing surface. For example, the next code randomly picks a color and randomly picks a coordinate at which to set the color. For no good reason I limit the number of points plotted to 1000
    For i = 1 to 1000
       'pick color value from 0 to 15 (the QBasic standard color set)
       iColor = (Int(15*Rnd)+0)
       'pick X from 0 to ScaleWidth
       X = (Int((Picture1.ScaleWidth * Rnd) + 0))
       'pick Y from 0 to ScaleHeight
       Y = (Int((Picture1.ScaleHeight * Rnd) + 0))
       'now plot the data
       picture1.pset (X,Y), QBColor(iColor)
    Next i
    Note: To get a value from n to 15, replace the "0" with the value of n
    With PSET you can do almost any graphic you can imagine, limited only by the difficulty in controlling one point at a time. Also, the PSET method is not nearly so fast as some of the higher order graphics methods.

The Bottom Line

In case it hasn't struck you yet, VB is not a graphics powerhouse. It's tools are very simple and there's just not very many of them. However, all is not lost!

You'll find that to create graphics of any sophistication will involve the use of the Windows API (Application Programmer's Interface), which are very powerful and very fast. However, even though the API are fairly powerful, they are not designed to be a graphics programmer's toolkit.

For that purpose, Microsoft has created another set of DLLs, known as DirectX. DirectX was written for the more professional programmer who is looking to create the more complicated, higher performance applications. You can get DirectX for free from Microsoft.

DirectX works essentially the same way as do the Windows API. However my own experience is very limited so all I can do is to point you off to the DirectX API and let you experiment on your own.