Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 Basic—graphics. Here, we’ll cover drawing graphics in Visual Basic. (We won’t, however, deal with handling bitmapped images until the next chapter.)

There’s a great deal of graphics power in Visual Basic, and we’ll see that power in this chapter. Here are some of the topics we’ll 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

We’ve see some of these techniques before when we worked with picture boxes, but we’ll expand that coverage in this chapter. And as a bonus, we’ll 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 we’ll 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 we’ll 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 we’ll 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, we’ll be using this coordinate system.


WARNING!  Bear in mind that the origin is at upper left (for forms, that’s the upper left of the form’s client area—the 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 don’t like working with twips, you can change to other measurement units like millimeters, inches, and so on, as we’ll see in this chapter. You can also define your own measurement units, as we’ll also see.

That’s it for the overview of graphics. It’s 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 say—why? Well, they say, when your program’s window is uncovered, it doesn’t redraw its displayed graphics automatically. Can you fix that?

One of Visual Basic’s 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 window’s 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 form’s AutoRedraw property to True to make a form display graphics when you draw those graphics in the form’s 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 program’s appearance yourself.)

Here’s 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. Here’s an example where we clear a picture box, Picture1, when the user clicks that picture box:

Private Sub Picture1_Click()
    Picture1.Cls
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.