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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The form’s code is presented in Listing 12.7. The comments should be sufficient for you to understand how it works. Basically, the four images are loaded into an array of Picture objects. Then, a Timer control is set to fire once every 0.25 second. With each cycle, the next image is loaded into the Picture Box, looping back to the first image as needed. The result is a crude animation of a spinning wheel. With more images (and a more skillful artist) the effect would be a lot more pleasing.


TIP:  The Timer Control

The Timer control lets you execute code at regular intervals. You do not see the Timer on a form, as it works behind the scenes. Set the Interval property to the desired number of milliseconds (1/1000 of a second) between events, then put the code to be executed in the Timer event procedure. Set the control’s Enabled property to True or False to start or stop it.


Listing 12.7 Code in PICTUREDEMO.FRM.

Option Explicit

‘ Number of pictures.
Const NUMPIX = 4

‘ Array of Picture objects.
Dim pics(NUMPIX - 1) As Picture

‘ Global variable to hold the current pic number.
Dim CurrentPic As Integer

Private Sub Command1_Click(Index As Integer)

Select Case Index
    Case 0  ‘ Start
        Timer1.Enabled = True
    Case 1  ‘ Stop
        Timer1.Enabled = False
    Case 2: ‘ Quit
        End
End Select

End Sub

Private Sub Form_Load()

‘Load the array of Picture objects
Set pics(0) = LoadPicture(“spin1.bmp”)
Set pics(1) = LoadPicture(“spin2.bmp”)
Set pics(2) = LoadPicture(“spin3.bmp”)
Set pics(3) = LoadPicture(“spin4.bmp”)

‘ Set up the timer for 250 mSec.
Timer1.Interval = 250
Timer1.Enabled = False

‘ Set the Picture Box to Autosize.
Picture1.AutoSize = True

‘Display the first image.
CurrentPic = 0
Picture1.Picture = pics(CurrentPic)

End Sub

Private Sub Form_Resize()

‘ Center the Picture Box left to right.
Picture1.Top = 200
Picture1.Left = (Form1.ScaleWidth - Picture1.Width) / 2

End Sub

Private Sub Timer1_Timer()

‘Increment the picture number, looping
‘ back to 0 as needed.
CurrentPic = CurrentPic + 1
If CurrentPic = NUMPIX Then CurrentPic = 0
Picture1.Picture = pics(CurrentPic)

End Sub

The PictureClip Control

The PictureClip control is another method for storing images, somewhat like the Picture object. It is specialized to permit storage of composite images, permitting you to retrieve and display only a portion of the image. It is typically used for small images, such as icons, cursors, or animation frames. You can combine, say, 20 icons into one master image, store the image in a PictureClip control, then retrieve individual icons as needed. This is faster and less resource-intensive than keeping the icon files in 20 different Picture objects.

Usually, the individual images that go into the composite image are the same size, and the composite image is a grid of equally spaced smaller images. For example, you could combine 12 icons, each 32-by-32 pixels, into a master image 128 pixels wide (4 icons) and 96 pixels high (3 icons). Then, set the PictureClip control’s Rows and Cols properties to specify that the image has three rows and four columns. You can then retrieve individual images using the indexed GraphicCell method. In this example, the master image contains 12 subimages, numbered from 0 at the top left to 11 at the bottom right. This statement would load into a Picture Box the icon in the second row, second column:

PictureBox1.Picture = PictureClip.GraphicCell(5)

You can also retrieve arbitrary areas of the master image using these properties of the PictureClip control:

  ClipX, ClipY—Specify the pixel coordinates of the top left corner of the area to be retrieved.
  ClipHeight, ClipWidth—Specify the pixel dimensions of the area to be retrieved.

Once these four properties have been set, use the Clip property to retrieve the specified region of the image in the PictureClip control:

Picture1.Picture = PictureClip1.Clip

To demonstrate, I have modified the demonstration program I used for the Picture object to use a PictureClip control instead. First, I combined the four individual animation frames into a single image, representing a two-by-two grid of the four images. This is shown in Figure 12.10.

The code is shown in Listing 12.8. I called this project PICTURECLIP. You can see that relatively few modifications have to be made to the code in the PICTUREDEMO project. The only difference in the form is the addition of one PictureClip control, with all of its properties left at the default values. The program looks exactly the same when running.


Figure 12.10  The image to be loaded into the PictureClip control.

Listing 12.8 Code in PICTURECLIP.FRM.

Option Explicit

Const NUMPIX = 4
Dim CurrentPic As Integer


Private Sub Command1_Click(Index As Integer)

Select Case Index
    Case 0  ‘ Start
        Timer1.Enabled = True
    Case 1  ‘ Stop
        Timer1.Enabled = False
    Case 2: ‘ Quit
        End
End Select

End Sub

Private Sub Form_Load()

‘ Load the PictureClip control
PictureClip1.Picture = LoadPicture(“pic_clip.bmp”)

‘ Set for 2 rows and 2 columns
PictureClip1.Rows = 2
PictureClip1.Cols = 2

‘ Set the Picture Box to Autosize.
Picture1.AutoSize = True

‘ Set up the Timer
Timer1.Interval = 250
Timer1.Enabled = False

‘Display the first image.
CurrentPic = 0
Picture1.Picture = PictureClip1.GraphicCell(CurrentPic)

End Sub

Private Sub Form_Resize()

‘ Center the Picture Box left to right.
Picture1.Top = 200
Picture1.Left = (Form1.ScaleWidth - Picture1.Width) / 2

End Sub

Private Sub Timer1_Timer()

‘Increment the picture number, looping
‘ back to 0 as needed.
CurrentPic = CurrentPic + 1
If CurrentPic = NUMPIX Then CurrentPic = 0
Picture1.Picture = PictureClip1.GraphicCell(CurrentPic)

End Sub

Putting Pictures On Forms

You can display bitmap and metafile pictures directly on a form without using a control. A picture on a form is always located “behind” the controls on the form, so you can use the picture as a background. You can display a picture on a form in two ways:

  Set its Picture property at design time or runtime; you must use the LoadPicture function at runtime, as you do with a Picture Box control.
  Make it the destination of the PaintPicture method (it can also serve as the source).

These techniques work in the same way as they do for a Picture Box control.


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. Read EarthWeb's privacy statement.