|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 18 Working With Graphics
- If you need an immediate solution to:
- Redrawing Graphics In Windows: AutoRedraw And Paint
- Clearing The Drawing Area
- Setting Colors
- Drawing Text
- Working With Fonts
- Drawing Lines
- Drawing Boxes
- Drawing Circles
- Drawing Ellipses
- Drawing Arcs
- Drawing Freehand With The Mouse
- Filling Figures With Color
- Filling Figures With Patterns
- Setting Figure Drawing Style And Drawing Width
- Drawing Points
- Setting The Drawing Mode
- Setting Drawing Scales
- Using The Screen Object
- Resizing Graphics When The Window Is Resized
- Copying Pictures To And Pasting Pictures From The Clipboard
- Printing Graphics
- Layering Graphics With The AutoRedraw And ClipControls Properties
In Depth
This chapter is on one of the most popular topics in Visual Basicgraphics. Here, well cover drawing graphics in Visual Basic. (We wont, however, deal with handling bitmapped images until the next chapter.)
Theres a great deal of graphics power in Visual Basic, and well see that power in this chapter. Here are some of the topics well cover:
- Drawing figures (boxes, circles, and so on)
- Filling figures with color
- Filling figures with patterns
- Setting the drawing mode (for example, XOR drawing)
- Setting the drawing width
- Setting the drawing style
- Using fonts
- Using the Screen object
- Using the Clipboard with graphics
- Printing graphics
- Resizing graphics
- Layering graphics
Weve see some of these techniques before when we worked with picture boxes, but well expand that coverage in this chapter. And as a bonus, well see how to work with the structured graphics control that comes with the Internet Explorer, putting that control to work in Visual Basic.
Graphics Methods Vs. Graphics Controls
There are two principal ways of drawing graphics in Visual Basic: using graphics methods, such as the ones well see in this chapter, and using graphics controls (like the line and shape controls). Graphics methods work well in situations where using graphical controls requires too much work. For example, creating gridlines on a graph would require an array of line controls but only a small amount of code using the Line method. In addition, when you want an effect to appear temporarily, you can write a couple of lines of code for this temporary effect instead of using another control. Also, 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.
All in all, the graphics methods well use in this chapter are usually preferred by programmers when they want to create graphics at runtime, and the graphics controls are preferred to create design elements at design time.
About Visual Basic Coordinates
Because well be drawing figures in forms and controls like picture boxes, we should know how measurements and coordinates are set up in those objects. Visual Basic coordinate systems have the origin (0, 0) at upper left and are specified as (x, y), where x is horizontal and y is vertical (note that y is positive in the downwards direction). When we draw graphics in Visual Basic, well be using this coordinate system.
WARNING! Bear in mind that the origin is at upper left (for forms, thats the upper left of the forms client areathe part that excludes borders, menu bars, and so on); that fact more than any other is responsible for confusing Visual Basic programmers when they start working with graphics.
The default unit of measurement in Visual Basic is twips (or 1/1440s of an inch). That unit was originally chosen to be small enough to be device-independent, but if you dont like working with twips, you can change to other measurement units like millimeters, inches, and so on, as well see in this chapter. You can also define your own measurement units, as well also see.
Thats it for the overview of graphics. Its time to turn to our Immediate Solutions to start digging into the graphics power that Visual Basic has to offer.
Immediate Solutions
Redrawing Graphics In Windows: AutoRedraw And Paint
The Testing Department is on the phone. Did you test out your new program, SuperDuperGraphicsPro, in Windows? Of course, you saywhy? Well, they say, when your programs window is uncovered, it doesnt redraw its displayed graphics automatically. Can you fix that?
One of Visual Basics most popular aspects is that you can make a form or control redraw itself as needed by setting its AutoRedraw property to True. What really happens is that Visual Basic keeps an internal copy of your windows display and refreshes the screen from that copy as needed. This solves one of the biggest headaches of Windows programming in a neat way.
TIP: You must also set a forms AutoRedraw property to True to make a form display graphics when you draw those graphics in the forms Load event handler. Note that AutoRedraw is set to False in forms by default.
However, setting AutoRedraw to True can use a lot of system resources, notably memory, and you might not want to do so in all cases. If not, you can use the Paint event to redraw your graphics, because this event occurs every time a form or control like a picture box is drawn or redrawn. (Note that if you set AutoRedraw to False, you are responsible for handling refreshes of your programs appearance yourself.)
Heres an example. In this case, we draw a circle inscribed in the smaller dimension (width or height) of a form when the form is drawn:
Private Sub Form_Paint()
Form1.Circle (ScaleWidth / 2, ScaleHeight / 2), _
Switch(ScaleWidth >= ScaleHeight, ScaleHeight / 2, _
ScaleWidth < ScaleHeight, ScaleWidth / 2)
End Sub
The result appears in Figure 18.1.
Figure 18.1 Drawing a circle using the Paint event.
Clearing The Drawing Area
One of the first things to learn about drawing graphics is how to clear the drawing area. You do that with the Cls method, which redraws the form or control in the current BackColor. Heres an example where we clear a picture box, Picture1, when the user clicks that picture box:
Private Sub Picture1_Click()
Picture1.Cls
End Sub
|