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


If the user did click one or the other word, we can report that to the user this way:

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, _
    X As Single, Y As Single)
    If X>16 And X<83 And Y>11 And Y<36 Then
        MsgBox "You clicked the word ""Picture"""
    End If

    If X>83 And X<125 And Y>11 And Y<36 Then
        MsgBox "You clicked the word ""Box"""
    End If
End Sub

The result appears in Figure 10.6—now we’re creating image maps in Visual Basic.

One more note here—image controls also have MouseDown events, so if you’re just creating an image map, you should consider an image control because they use far fewer system resources.


TIP:  Other picture box events that can be useful include the Resize, Change, and Paint events.

Picture Box Animation

One easy way to support graphics animation in Visual Basic is to use a picture box. For example, you may have a control array of picture boxes, only one of which is visible at any one time. You can then make the others appear (at the same location) by setting the first picture box’s Visible property to False, the next one’s Visible property to True, and so on, cycling through the picture boxes.

That method is very wasteful of memory, however; if you’re going to use picture boxes to support animation, a better idea is to use one picture box and keep changing its Picture property to display successive frames of an animation. You can store the images themselves in an image list control.

To add an image list control, follow these steps:

1.  Select the Project|Components menu item.
2.  Select the Controls tab in the Components box.
3.  Select the Microsoft Windows Common Controls item in the Components box and click on OK to close that box.
4.  Add a new image list control to your program using the Image List tool in the toolbox.
5.  Right-click the new image list control and select the Properties item in the menu that opens.
6.  Click the Images tab in the Property Pages box that opens, and load the images you want to use in the image list using the Insert Picture button.
7.  Close the Property Pages box by clicking on OK.

All that remains is to add the code you need. For example, here we’ve added a timer control, Timer1, to the program, set its Enabled property to False, and set its Interval property to 1000 (the Interval property is measured in milliseconds, 1/1000s of a second), which means the Timer1_Timer() event handler will be called once a second.

For the purposes of this example, we will just switch back and forth between two images in the picture box. These two images are the first two images in an image list, ImageList1. To switch back and forth, we use a static Boolean flag named blnImage1 like this (for more information on using image lists, see Chapter 16):

Private Sub Timer1_Timer()
    Static blnImage1 As Boolean

    If blnImage1 Then
        Picture1.Picture = ImageList1.ListImages(1).Picture
    Else
        Picture1.Picture = ImageList1.ListImages(2).Picture
    End If
...

At the end of Timer1_Timer(), we toggle the blnImage1 flag this way:

Private Sub Timer1_Timer()
    Static blnImage1 As Boolean

    If blnImage1 Then
        Picture1.Picture = ImageList1.ListImages(1).Picture
    Else
        Picture1.Picture = ImageList1.ListImages(2).Picture
    End If

    blnImage1 = Not blnImage1
End Sub

And that’s all we need—now we’re supporting a rudimentary animation using picture boxes.

Grouping Other Controls In A Picture Box

The Aesthetic Design Department is on the phone again. They like the new option buttons you’ve added to your program, but wouldn’t it be nice if you could display pictures behind each group of option buttons?

You can do that with picture boxes. Picture boxes are container controls, which means they can contain other controls. You usually use this capability to group option buttons together, because those controls work as a group (you can also group option buttons together by form or frame control).

The important thing here is to make sure that you paint the option buttons in the target picture box; don’t just double-click the Option Button tool. Only when an option button is drawn entirely inside a picture box from the start is it associated with that picture box.

For example, we’ve added nine option buttons to two picture boxes in the form in Figure 10.7. As you can see in that figure, we can click option buttons in the two groups independently—they function as separate groups.


Figure 10.7  Grouping option buttons with picture boxes.

Picture boxes can also contain other controls, of course, like command buttons (see “Aligning A Picture Box In A Form” earlier in this chapter to see how to create rudimentary toolbars and status bars this way) or checkboxes.

Using A Picture Box In An MDI Form

Another special use of picture boxes is to draw toolbars or status bars in an MDI form. This method has been superceded by the toolbar and status bar controls, but it used to be the way you could add those items to MDI forms.

For example, to add a Picture Box toolbar to an MDI form (only controls that support the Align property may be added to MDI forms), you just draw that control in the MDI form. Visual Basic will align the picture box with the top of the client area of the MDI form by default, but you can align it at bottom or on either side as well. Here are the possible values for the picture box’s Align property:

  0—Align none
  1—Align top
  2—Align bottom
  3—Align left
  4—Align right

As an example, we’ve added a picture box to the MDI form in Figure 10.8 and placed a few command buttons in that picture box to create a rudimentary toolbar. As you can see in that figure, the MDI form draws a border at the bottom of the new toolbar automatically.


Figure 10.8  Using a picture box to create a toolbar in an MDI form.

Although this used to be the way to create toolbars and status bars in MDI forms, it’s now better to use the controls specifically designed for this purpose, the toolbar and status bar controls.

Drawing Lines And Circles In A Picture Box

The Testing Department is on the phone again. The new picture box-based image map you’ve put in your program is terrific, but can you draw a box around the hotspots in the map as the user clicks them? That would make things much clearer.


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.