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


At design time, click that property in the Properties window and click the button with an ellipsis (...) in it to open the Load Picture dialog box. Specify the file you want to load into the picture box, and click on OK.

At runtime, you can use LoadPicture() to load in a picture like this, where we load in an image when the user clicks a command button:

Private Sub Command1_Click()
    Picture1.Picture = LoadPicture("c:\vbbb\picturesandimages\image.bmp")
End Sub


TIP:  Besides LoadPicture(), Visual Basic also supports LoadResPicture(), which lets you load pictures from resource files. Using LoadResPicture() is useful for localizing a Visual Basic application—the resources are isolated in one resource file, and there is no need to access the source code or recompile the application.

If you want to get the picture in a picture box, you also use the Picture property. For example, here we copy the picture from Picture1 to Picture2 when the user clicks a command button:

Private Sub Command1_Click()
    Picture2.Picture = Picture1.Picture
End Sub

The Picture property is very useful in Visual Basic because it provides such an easy way of handling images, as you can see in the preceding two code snippets. With the Picture property, you can store images and transfer them between controls.

Besides the Picture property, picture boxes also have an Image property. The Image property is actually the handle to the image’s bitmap in the picture box and as such is very useful when working with Windows calls directly. You can also assign images from an Image property to a Picture property like this:

Private Sub Command1_Click()
    Picture2.Picture = Picture1.Image
End Sub

Adjusting Picture Box Size To Contents

You’ve displayed the image of the company’s Illustrious Founder in a picture box in your new program—but the picture box was a little small, and you can only see most of the I.F.’s forehead. There’s some email waiting for you from the president’s office, and you think you know what it says. How can you make sure picture boxes readjust themselves to fit the picture they’re displaying?

When you load a picture into a picture control, it does not readjust itself to fit the picture (although image controls do)—at least, not by default. Picture boxes will resize themselves to fit their contents if you set their AutoSize properties to True. If AutoSize is set to True, you don’t have to worry about resizing the picture box, even if you load images into the picture box at runtime. This saves a lot of fiddling with the picture box’s Left, Top, Width, and Height properties.

Aligning A Picture Box In A Form

Picture boxes are special controls in that they can contain other controls (in Visual Basic terms, picture boxes are container controls). In fact, if you place option buttons inside a picture box (just draw them inside the picture box), those option buttons act together as a group.

Besides grouping option buttons together, the original idea here was to provide Visual Basic programmers a (rather rudimentary) way of creating toolbars and status bars in their programs. That’s been superceded now by the toolbar and status bar controls, of course.

To let you create toolbars or status bars, picture boxes have an Align property. You use this property to place the picture box at top, bottom, or on a side of a form. Here are the possible values for Align:

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

For example, we’ve aligned the picture box in Figure 10.5 to the top of the form, giving it a few buttons, and we’ve set its BackColor property to deep blue to make a rudimentary toolbar.


Figure 10.5  Creating a toolbar with an aligned picture box.

Handling Picture Box Events (And Creating Image Maps)

The New Products Department is on the phone; they want you to design a program to welcome new employees to the company. The program should display a picture of the main plant, and when the new employee clicks part of that image, “it should sort of zoom in on it.” Can you do something like that in Visual Basic?

Responding to targeted mouse clicks in an image means creating an image map, and you can create one with a picture box. Picture boxes have Click events (and even DblClick events), of course, but Click event handlers only tell you that the picture box was clicked, not where it was clicked:

Private Sub Picture1_Click()

End Sub

The Click event is useful if you want to use picture boxes as sort of image-bearing buttons (although buttons can also display images now). However, if you want to know where in a picture box the user clicked the mouse, use MouseDown. (Besides the full range of mouse events, picture boxes also support key events like KeyDown, KeyPress, and so on.)

Creating An Image Map

Here’s an example where we create an image map. We’ll need to know the exact locations of the various hotspots in the image that do something when clicked, and it’s easy to find their dimensions and location by using a simple graphics program like the Windows Paint program.

Note, however, that programs like Windows Paint will measure your image in pixels, and if you want to use pixel measurements, not twips, you must set the picture box’s ScaleMode property to vbPixels, like this:

Private Sub Form_Load()
    Picture1.ScaleMode = vbPixels
End Sub

We’ll use the image you see in the picture box in Figure 10.6 as our image map and report to users when they click either word, “Picture” or “Box”.


Figure 10.6  Creating an image map with a picture box.

In the MouseDown event handler, we’re passed the location of the mouse click as (X, Y), and we check to see if the mouse went down on either word in the image:

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
...
    End If

    If X>83 And X<125 And Y>11 And Y<36 Then
...
    End If
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.