|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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.6now were creating image maps in Visual Basic. One more note hereimage controls also have MouseDown events, so if youre just creating an image map, you should consider an image control because they use far fewer system resources.
Picture Box AnimationOne 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 boxs Visible property to False, the next ones Visible property to True, and so on, cycling through the picture boxes. That method is very wasteful of memory, however; if youre 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:
All that remains is to add the code you need. For example, here weve 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 thats all we neednow were supporting a rudimentary animation using picture boxes. Grouping Other Controls In A Picture BoxThe Aesthetic Design Department is on the phone again. They like the new option buttons youve added to your program, but wouldnt 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; dont 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, weve 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 independentlythey function as separate groups.
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 FormAnother 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 boxs Align property:
As an example, weve 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.
Although this used to be the way to create toolbars and status bars in MDI forms, its now better to use the controls specifically designed for this purpose, the toolbar and status bar controls. Drawing Lines And Circles In A Picture BoxThe Testing Department is on the phone again. The new picture box-based image map youve 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.
|
|
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. |