Partager via


Visual Basic Concepts

Using the Animation Control

The Animation control displays silent Audio Video Interleaved (AVI) clips. An AVI clip is a series of bitmap frames like a movie.

One example is the piece of paper that "flies" between folders when copying files in the Windows 95 (or later) system:

Although AVI clips can have sound, such clips cannot be used with the Animation control, and an error will occur if you try to load such a file. Only silent AVI clips can be used. To play .avi files with sound, use the Multimedia (MCI) control. See "Using the Multimedia Control" later in this chapter for more information about the MCI control.

Note   A variety of soundless .avi files can be found on Disk 1 of Visual Studio in the Common\Graphics\AVI directory.

At run time, the Animation control does not have a visible frame.

The Animation control maintains a separate thread of execution while it is playing. Therefore, your application will not be blocked, and can continue to execute within its process.

Possible Uses

  • To create dialog boxes that inform the user of the length and nature of an operation.

  • To play silent instructional video clips about your application.

  • To allow users to play files dropped onto the control.

Basic Operation: Open, Play, Stop, and Close Methods

When using the control, you open an .avi file using the Open method, play it using the Play method, and stop it with the Stop method. After a video has played, use the Close method to close the file. You do not need to close the file before opening a new one.

The following code uses two CommandButton controls, cmdPlay and cmdStop, and a CommonDialog control named dlgOpen. Set the caption of cmdPlay to "Open and Play." The caption of the CommandButton control cmdStop is set to "Stop."

Private Sub cmdPlay_Click()
   ' Configure a CommonDialog control to allow the
   ' user to find .avi files to play. The CommonDialog
   ' control is named "dlgOpen." The Animation control
   ' is named "anmAVI."
   dlgOpen.Filter = "avi files (*.avi)|*.avi"
   dlgOpen.ShowOpen
   anmAvi.Open dlgOpen.FileName
   anmAVI.Play
End Sub

This code stops the video playing:

Private Sub cmdStop_Click()
   anmAVI.Stop
End Sub

Play Method Arguments: Repeat, Start, and Stop

The Play method has three arguments — repeat, start, and stop — which determine how many times a file is played, at which frame to begin playing, and where to stop the file.

If the repeat argument is not supplied, the file will play continuously. For example, the following code will play a file continuously until the user clicks the cmdStop button:

Private Sub cmdPlay_Click()
   dlgOpen.Filter = "avi files (*.avi)|*.avi"
   dlgOpen.ShowOpen
   anmAVI.Open dlgOpen.FileName
   ' Play the file indefinitely.
   anmAVI.Play
End Sub

Private Sub cmdStop_Click()
   anmAVI.Stop
End Sub

The following code plays the file ten times, from the sixth to sixteenth frames (the first frame is frame 0):

anmAVI.Play 10, 5, 15

Play Files Automatically Using the AutoPlay Property

If the AutoPlay property is set to True, the control will begin playing a file as soon as it is loaded. Conversely, to stop a file from playing, set the AutoPlay property to False, as shown in the following code:

Private Sub cmdPlay_Click()
   ' Setting the AutoPlay property to True plays
   ' the file as soon as it is loaded. Thus there's
   ' no need for a Play method.
   dlgOpen.Filter = "avi files (*.avi)|*.avi"
   dlgOpen.ShowOpen
   anmAvi.AutoPlay = True
   anmAVI.File = dlgOpen.FileName
End Sub

Private Sub cmdStop_Click()
   ' Set AutoPlay to False to stop playing.
   anmAVI.AutoPlay = False
End Sub

Center the Play Area Using the Center Property

You can specify whether or not to center the video in the control by using the Center property. When the Center property is set to False, the control automatically sizes itself at run time to the size of the video. At design time, the left and top edges of the control define the area where the video will be displayed as seen:

When the Center property is set to True, the control does not resize itself. Instead, the video will be displayed in the center of the area defined by the control, as seen:

Note   If the area defined by the control at design time is smaller than the video, the edges of the video will be clipped.

Distribution Note   The Animation control is part of a group of ActiveX controls that are found in the MSCOMCT2.OCX file. To use the Animation control in your application, you must add the MSCOMCT2.OCX file to the project. When distributing your application, install the MSCOMCT2.OCX file in the user’s Microsoft Windows System or System 32 directory. For more information on how to add an ActiveX control to a project, see the Programmer’s Guide.