Remember to experimentAlthough the projects, forms, and so on, have specific names in this chapter, you can experiment with naming convention and with other parts of the code to fit your needs.
Add graphics to a form
FIGURE 17.1 The default value for the BorderStyle property of the PictureBox is 1 - Fixed Single. The default value for the BorderStyle property of the Image control is 0 - None.
FIGURE 17.2 The Windows directory on your system ships with many different types of graphics files.
Many graphics files will workBubbles.bmp is a bitmap that ships with Windows. If for some reason this file isn't present in your Windows directory, choose another bitmap or choose any icon, metafile or enhanced metafile, JPEG, or GIF file. All these formats can be contained within a PictureBox.
FIGURE 17.3 The Image control doesn't support an AutoSize property. It automatically resizes itself to the dimensions of the assigned picture when the graphic is added.
You can change the graphic assigned to the Picture property of a PictureBox or Image control at runtime by changing the value of the control's Picture property.
Download this project's codeThe code for this example can be found at http://www. mcp.com/info. When prompted, enter 078971633x for the ISBN and click the Search button.
Change a control's Picture property at runtime
picMain.Picture = imgMain.Picture
FIGURE 17.4 Setting the PictureBox control's AutoSize property to True resizes the control's area to the same size as the image.
You can use the process of setting a Picture property at runtime to make a custom button that looks different but works the same as a CommandButton.
Make a custom button
FIGURE 17.5 Creating a custom button requires three Image controls. When you don't add an image to an Image control, it appears as a dashed box on your form.
01 Private Sub Form_Load()
02 imgMain.Picture = imgUp.Picture
03 End Sub
04
05 Private Sub imgMain_MouseDown(Button As Integer, _
06 Shift As Integer, X As Single, Y As Single)
07 imgMain.Picture = imgDown.Picture
08 End Sub
09
10 Private Sub imgMain_MouseUp(Button As Integer, _
11 Shift As Integer, X As Single, Y As Single)
12 imgMain.Picture = imgUp.Picture
13 End Sub
MsgBox "I am a custom button!"
FIGURE 17.6 The custom button, imgMain, can still reference the Picture property of the imgDown and imgUp controls, even though the Visible property of these buttons is set to False.
When you run the code in the project CustBut.vbp, the Image control imgMain becomes a custom button with an associated Click event procedure. The key to the construction of the custom button is to have the Image control's MouseDown and MouseUp behavior resemble general button down and button up graphical behaviors of a CommandButton. For example, when you click a CommandButton, notice that it shows a slightly different picture when the mouse button is down compared to when the mouse button is up. The custom button you just made does the same thing (see Figure 17.7). You can program the Click event procedure of the Image control custom button without interfering with other mouse behaviors because the imaging behavior of that control is relegated only to the MouseDown and MouseUp event procedures.
FIGURE 17.7 MouseDown and MouseUp images vary slightly to indicate state changes.
If you want to load a graphic file into a PictureBox or Image control from your hard disk, you use the LoadPicture() function:
MyPicture = LoadPicture(strFilePath)
In this syntax,
You can download this exampleThe code for this example can be found at http://www.mcp.com/info. When prompted, enter 078971633x for the ISBN and click the Search button.
Load a file from disk
FIGURE 17.8 When you set the AutoSize property of a PictureBox to True, be sure to leave enough expansion room for the PictureBox to accommodate a large picture.
picLoad.Picture = LoadPicture("c:\windows\circles.bmp")
FIGURE 17.9 To clear a PictureBox or Image control, call LoadPicture without any arguments: picLoad.Picture = LoadPicture.
Windows programs commonly have an icon embedded within the executable file (.exe) to graphically represent the given program. When you create a program, Visual Basic automatically assigns one of your form's icons to your executable. Unless you change it, Visual Basic will use the default form icon (see Figure 17.10).
FIGURE 17.10 You set the unique properties of your program within the Project Properties dialog.
You can embed a custom icon into your program's executable file, however, by assigning an icon to the Icon property of a form in your project. Visual Basic contains many icons for you to use in the \VB\Graphics\Icons folder within the Visual Basic folder on your hard drive.
Change a form's icon
FIGURE 17.11 In Windows, icon files show their image in all views.
If your project has only one form, adding a custom icon to the Icon property of the form will automatically assign the same icon to the program. If your project has multiple forms, you must set the application icon from the Project Properties dialog.
Creating a new iconYou can't create an icon within Visual Basic or with the MS Paint program that ships with Windows. However, the Visual Basic 6 CD-ROM does contain a program for making icons. It's called the Microsoft Image Editor (imagedit.exe), located in the directory \Tools\Imagedit. If you can't find this program on your system, it probably wasn't installed; run Visual Basic setup again to install it.
Set the icon for your application
Look at the project MoreGrfx.vbp (from http://www.mcp.com/info). This program sums up the techniques you've learned thus far to display graphics (see Figure 17.13). It enables you to display icon or bitmap images embedded in the form and to select a file from disk to display in the main PictureBox control picDisplay.
FIGURE 17.13 The application More Graphics uses OptionButtons to determine which technique the program should use to display images.
MoreGrfx uses a FileListBox (similar to a ListBox) to display the available graphic files. However, whereas the ListBox must be programmed to load string values to display, the FileListBox automatically displays strings that reflect the files of a specific folder. All you need to do is tell the FileListBox which folder by setting the value of the Path property of the FileListBox to the exact location of the folder containing the files you want to display.
Listing 17.2 shows the Form_Load() event procedure for the project MoreGrfx.vbp. This event procedure is where the value of the Path property for the FileListBox File1 is set.
01 Private Sub Form_Load()
02 `Set the path of the FileListbox to be the
03 `directory in which the application resides.
04 File1.Path = App.Path
05
06 `Make it so the FileListbox only displays
07 `bitmap and icon files.
08 File1.Pattern = "*.bmp;*.ico"
09 End Sub
Notice that the value of the FileListBox's Path property is set to the value of the Path property of the App object, which holds information about your application. The value of the App object's Path property reports the directory in which the application executable file resides. Thus, the FileListBox displays the files in the folder from which the application was started.
After a file is selected in the FileListBox File1, the user clicks the CommandButton cmdAssign. Listing 17.3 shows the Click event procedure for this CommandButton.
Study this code closelyMany of this program's nuances can be understood only by working directly with the program and looking at the entire code.
01 Private Sub cmdAssign_Click()
02 Dim strFilePath As String
03
04 `Query the option buttons to see which one
05 `has been checked.
06 If optBitmap.Value = True Then
07 `Show the bitmap graphic
08 picDisplay.Picture = picBitmap.Picture
09 ElseIf optIcon.Value = True Then
10 `Show the icon graphic
11 picDisplay.Picture = picIcon.Picture
12 ElseIf optList.Value = True Then
13 `Assign the exact path of the graphic in the file
14 `list box to the strFilePath string variable.
15 `Don't forget, to get the string in a list box
16 `you must identify the Listindex selected within
17 `a List Control's List array.
18 strFilePath = File1.Path & "\" _
& File1.List(File1.ListIndex)
19 `Load the picture from disk and assign it to the
20 `picture property of the Picture control.
21 picDisplay.Picture = LoadPicture(strFilePath)
22
23 End If
24 End Sub
As shown in Listing 17.3, if the OptionButton optList is checked (line 12), the application loads the selected file from disk into the PictureBox picDisplay by using the LoadPicture() function.
Creating Special Graphic Effects
In the project MoreGrfx.vbp, after users assign a picture to the PictureBox picDisplay, they can decide to distort the picture by clicking the CommandButton cmdDistort. When this button is clicked, the picture in the PictureBox is assigned to the Picture property of the Image control imgDistort with the following statements:
imgDistort.Width = picDisplay.Width * 1.5 imgDistort.Picture = picDisplay.Picture
The Image control distorts the picture because the value of the Stretch property of imgDistort is set to True and the value of the Width property of the Image control is modified to be irregular relative to the PictureBox picDisplay. The PictureBox must have the value of its AutoSize property set to True to resize itself and accommodate the area of pictures assigned to it. The Image control resizes itself by default to accommodate the area of assigned pictures. However, when the value of the Image control's Stretch property is set to True, Visual Basic resizes the assigned picture to the dimensions of the Image control, regardless of the size of the Image control. This can make for some unusual distortions (see Figure 17.14).
However, you can use the Stretch property to enlarge a picture within an Image control. Listing 17.4 shows the code for the cmdEnlarge_Click() event procedure. The cmdEnlarge CommandButton allows users to enlarge the picture displayed in the smaller PictureBox picDisplay. The trick to enlarging a picture is to maintain the width-to-height (aspect) ratio of the picture when sizing the Image control to which the picture will be stretched. The code in Listing 17.4 determines the ratio of the PictureBox picDisplay by calculating the control's width over its height (line 4). Then the code applies that ratio to the Height of the Image control imgDistort (line 8). As a result, the value of imgDistort.Width is reset, and the aspect ratio is maintained.
FIGURE 17.14 You can use the Stretch property of the Image control to distort pictures. To enlarge a picture, size the Image control to the same ratio as the original picture.
01 Private Sub cmdEnlarge_Click()
02 Dim dSizeRatio As Double
03 `Figure out the ratio
04 dSizeRatio = picDisplay.Width / picDisplay.Height
05
06 `Apply the ratio to Image control's Height to
07 `get the new Width
08 imgDistort.Width = imgDistort.Height * dSizeRatio
09 End Sub
© Copyright, Macmillan Computer Publishing. All rights reserved.