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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Drawing In Picture Boxes And On Forms

So far, we have seen how to display and manipulate existing images in Picture Boxes and on forms. What if you want to create an image from scratch? Visual Basic has several statements that let you draw just about anything imaginable. When you draw in a Picture Box or on a form, a current position always marks the location where the most recent drawing operation finished. The current position is defined by the object’s CurrentX and CurrentY properties. With no draw operations— or if the object’s graphics have been erased with the Cls method—these properties both default to zero.

The Line Method

When you want to draw lines and rectangles on Form, Picture Box, and Printer objects, use the Line method

object.Line (X1, Y1)-(X2, Y2), color, mode

The arguments are as follows:

  X1, Y1—The coordinates of the start of the line. If they’re omitted, the object’s current position (CurrentX and CurrentY properties) is used.
  X2, Y2—The coordinates of the end of the line.
  color—This optional argument specifies the line color. If it’s omitted, the object’s ForeColor property is used.
  mode—If set to B, this optional argument specifies that a rectangle is drawn with diagonally opposite corners at the given coordinates. The rectangle is filled with a color and pattern as specified by the object’s FillColor and FillStyle properties. If the mode is set to BF, the rectangle is filled with the same color used to draw the border, and the object’s FillColor and FillStyle properties are ignored.

You can specify color in two ways. The RGB function lets you specify a color in terms of the relative intensities of the red, green, and blue components. Here’s the syntax:

RGB(red, green, blue)

Each of the three arguments is a number in the range 0 through 255, specifying the relative intensity of the color. For example, the statement

Form1.Line (0,0)-(500,500), RGB(0,0,255), BF

will draw a rectangle that is completely blue—both its border and interior. You can also specify color with the QBColor function. Its syntax is:

QBColor(color)

Here, the color argument is a number in the range 0 through 15, which corresponds to one of the colors in Table 12.6. The QBColor function gives you easy access to the 16 colors available in the old Quick Basic programming language.


TIP:  Red And Green Make...Yellow?

Some readers may wonder why the RGB function uses red, green, and blue. Aren’t the primary colors red, blue, and yellow? Yes, but those are the subtractive primaries, applicable when the color in question is the result of one color absorbing another, as in mixing paints. For example, red paint looks red because it absorbs, or subtracts, the blue and yellow colors. With a computer screen, we are dealing with additive colors that are created by adding different color lights together. If you’ve ever been in a school play, you’ve probably noticed that the stage lights are composed of red, green, and blue bulbs. For example, adding red light to green light makes yellow—hardly what you would expect, but try it for yourself with the color mixing demonstration program that follows.


Table 12.6 QBColor color arguments.

Number Color Number Color
0 Black 8 Gray
1. Blue 9 Light Blue
2. Green 10 Light Green
3. Cyan 11 Light Cyan
4. Red 12 Light Red
5. Magenta 13 Light Magenta
6. Yellow 14 Light Yellow
7 White 15 Bright White

Color Mixing Demonstration

The main purpose of this demonstration program is to show you how the Line method and the RGB function work, but you’ll also gain some experience in using control arrays and Scroll Bar controls. The program’s single form contains a Scroll Bar and Text Box for each of the three primary colors. The Text Boxes display numerical values—from 0 to 255—that you change by using the Scroll Bar. The values, of course, represent the color intensity. A rectangle displaying the selected color is drawn on the form whenever the user changes one of the Scroll Bar values. The final program is shown in Figure 12.11.

To create the program, start a new project and place a control array of three VScrollBar controls and another array of three Text Box controls on the form. Add three Label controls with the Caption properties set to Red, Green, and Blue, respectively. Position the controls on the form, as shown in Figure 12.11. Be sure that the Scroll Bar and Text Box with Index 0 are under the Red label, that the Scroll Bar and Text Box with Index 1 are under the Green label, and that the Scroll Bar and Text Box with Index 2 are under the Blue label. Change the form’s Caption property to Color Tester and save both the project and the form with the name COLORS.


Figure 12.11  Using the COLORS project to explore the RGB function.

Now we can turn to the code, shown in Listing 12.9. Initialization steps are performed in the Form_Load event procedure, as usual. This consists mainly of setting the Scroll Bar properties. Because the three Scroll Bars are all part of a control array, we can use a For...Next loop to loop through them (another advantage of control arrays). The Scroll Bar’s Value property returns its current setting. You can set several properties to customize each Scroll Bar to suit your current needs:

  Max and Min—Values corresponding to the Scroll Bar’s maximum and minimum positions.
  SmallChange—Amount the Scroll Bar changes when the user clicks on one of the scroll arrows.
  LargeChange—Amount the Scroll Bar changes when the user clicks between the thumb (the small, movable box) and one of the scroll arrows.


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. Read EarthWeb's privacy statement.