Condividi tramite


Visual Basic Concepts

Using the Picture Box Control

The picture box control is used to display graphics, to act as a container for other controls, and to display output from graphics methods or text using the Print method.

Figure 7.41   The picture box control

The picture box control is similar to the image control in that each can be used to display graphics in your application — each supports the same graphic formats. The picture box control, however, contains functionality which the image control does not, for example: the ability to act as a container for other controls and support for graphics methods.

For More Information   See "Working with the Picture Box Control" "Forms, Controls, and Menus" for a demonstration of the use of picture boxes.

Supported Graphic Formats

The picture box control can display picture files in any of the following formats: bitmap, cursor, icon, metafile, enhanced metafile, or as JPEG or GIF files.

For More Information   See "Using the Image Control" earlier in this chapter for detailed descriptions of these graphic formats.

Loading a Graphic Into the Picture Box Control

Pictures can be loaded into the picture box control at design time by selecting the Picture property from the control's Properties window, or at run time by using the Picture property and the LoadPicture function.

Set Picture1.Picture = _
LoadPicture("c:\Windows\Winlogo.cur", vbLPLarge, vbLPColor)

You may want to use icon (.ico) and cursor (.cur) files containing separate images at different sizes and color depths to support a range of display devices. The LoadPicture function's settings allow you to select images with specific color depths and sizes from an .ico or .cur file. In cases where an exact match to the requested settings isn't available, LoadPicture loads the image with the closest match available.

To clear the graphic from the picture box control, use the LoadPicture function without specifying a file name. For example:

Set Picture1.Picture = LoadPicture

This will clear the picture box control even if a graphic was loaded into the Picture property at design time.

Using the Clipboard

You can also add a graphic to a picture box control at design time by pasting it from another application. For example, you may want to add a bitmap image that was created in Windows Paint. Simply copy the image to the clipboard, select the picture box control, and either use the keyboard shortcut CTRL+V or the Paste command from the Edit menu.

Sizing a Picture

By default, graphics are loaded into a picture box at their original size, meaning that if the graphic is larger than the control, the image will be clipped — the picture box control does not provide scroll bars. To make a picture box control automatically resize to display an entire graphic, set its AutoSize property to True. The control will then size to the graphic — growing or shrinking.

Unlike the image control, the picture box control cannot stretch the image to fit the size of the control.

For More Information   See "Scroll Bar Controls Scenario: Creating a Scrollable Graphics Viewport" earlier in this chapter for information on using picture boxes to create a scrollable graphics viewport.

Using the Picture Box Control as a Container

You can use the picture box control as a container for other controls. For example, since the picture box can be placed inside the internal area of a MDI form, it is often used to manually create a toolbar or status bar.

For More Information   See "Creating a Toolbar" in "Creating a User Interface" for more information on using the picture box control as a container for other controls.

Graphics Methods

Picture boxes, like forms, can be used to receive the output of graphics methods such as Circle, Line, and Point. For example, you can use the Circle method to draw a circle in a picture box by setting the control's AutoRedraw property to True.

Picture1.AutoRedraw = True
Picture1.Circle (1200, 1000), 750

Setting AutoRedraw to True allows the output from these methods to be drawn to the control and automatically redrawn when the picture box control is resized or redisplayed after being hidden by another object.

For More Information   See "Using Graphics Methods" "Working with Text and Graphics" for more information about using the picture box control with the graphics methods.

Using the Print Method

You can use the picture box control to output text by using the Print method and setting the AutoRedraw property to True. For example:

Picture1.Print "A text string"

When using the Print method you can also modify the font style and size or use the CurrentX, CurrentY, Height, and Width properties to align text within a picture box.

For More Information   See "Working with Text and Graphics" and "Print Method" for more information.