Partager via


Visual Basic Concepts

Using Graphics Methods

In addition to the graphical controls, Visual Basic provides several methods for creating graphics. The graphics methods, summarized in the following table, apply to forms and picture boxes.

Method Description
Cls Clears all graphics and Print output.
PSet Sets the color of an individual pixel.
Point Returns the color value of a specified point.
Line Draws a line, rectangle, or filled-in box.
Circle Draws a circle, ellipse, or arc.
PaintPicture Paints graphics at arbitrary locations.

Note   The Print method can also be considered a graphics method, because its output is written to the object and is saved in the memory image (if AutoRedraw is on) just like the PSet, Line, and Circle methods. For more information about the Print method, see "Displaying Text on Forms and Picture Boxes" earlier in this chapter.

Advantages of Graphics Methods

The graphics methods work well in situations where using graphical controls require too much work. For example, creating gridlines on a graph would need an array of line controls but only a small amount of code using the Line method. Tracking the position of line controls in an array as the form changes size is more work than simply redrawing lines with the Line method.

When you want a visual effect to appear briefly on a form, such as a streak of color when you display an About dialog, you can write a couple of lines of code for this temporary effect instead of using another control.

Graphics methods offer some visual effects that are not available in the graphical controls. For example, you can only create arcs or paint individual pixels using the graphics methods. Graphics you create with these graphics methods appear on the form in a layer of their own. This layer is below all other controls on a form, so using the graphics methods can work well when you want to create graphics that appear behind everything else in your application.

For More Information   See "Layering Graphics with AutoRedraw and ClipControls" earlier in this chapter.

Limitations of Graphics Methods

Creating graphics with the graphics methods takes place in code, which means you have to run the application to see the effect of a graphics method. Graphics methods therefore don't work as well as graphical controls for creating simple design elements of an interface. Changing the appearance of graphical controls at design time is easier than modifying and testing the code for a graphics method.

For More Information   For information about creating graphical applications with the mouse events and the Line or Move methods, see "The MouseDown Event," "The MouseMove Event" and "Using Button to Enhance Graphical Mouse Applications" in "Responding to Mouse and Keyboard Events."