65.9K
CodeProject 正在变化。 阅读更多。
Home

无定时器控件的动画

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.80/5 (4投票s)

2008年2月2日

CPOL

2分钟阅读

viewsIcon

34440

downloadIcon

843

使用多媒体MCI控件在VB6中制作动画。

AudioPlayer

引言

在本文中,我使用 Multimedia MCI 控件来实现动画,就像使用 Timer 控件一样。你可以使用 StatusUpdate 事件来实现这一点。StatusUpdate 事件会在你赋予 UpdateInterval 属性的合适值(大于 0)的间隔内自动发生,这与 Timer 控件的间隔完全相同。

背景

使用 Multimedia MCI 控件,你需要知道 StatusUpdate 事件允许应用程序更新显示,以向用户通报当前 MCI 设备的运行状态,例如 Position(位置)、Length(长度)和 Mode(模式)。你可以为 UpdateInterval 属性赋予任何正值(以毫秒为单位)。如果该值较小,显示速度快;如果值较大,显示速度慢;但如果值为 0,则不会发生 StatusUpdate 事件。

我的窗体包含一些控件

  • mciWave (MMControl),不可见
  • ImageList1 包含 30 个图像
  • ImageList2 包含 6 个图像
  • DancePic (PictureBox) 用于显示 ImageList2 中包含的图像
  • MoviePic (PictureBox) 用于显示 ImageList1 中包含的图像
  • btnPlay (Button) 用于执行 (Play) 命令
  • btnPause (Button) 用于执行 (Pause) 命令
  • btnStop (Button) 用于执行 (Stop) 命令
  • btnOpen (Button) 用于加载音乐文件
  • SongName (Label) 用于显示文件名
  • SongTime (Label) 用于显示文件时长
  • ElapsedTime (Label) 用于显示已用时间
  • CommonDialog1 (CommonDialog) 用于加载格式为 (mid, mp3, wav, wma) 的音乐文件

Using the Code

在声明部分将 UpdateInterval 属性的值设置为常量 50。请参考声明部分的其他变量。

LoadMusicFile() 过程

'
' Set the number of milliseconds =0, let StatusUpdate events not occurred.:
'
mciWave.UpdateInterval = 0
'
' Display the File Open dialog box:
'
With CommonDialog1
   .FilterIndex = 1
   .Flags = cdlOFNReadOnly Or cdlOFNFileMustExist
   .CancelError = True
   .FileName = ""
   .Filter = "Sound Formats|*.mid;*.mp3;*.wav;*.wma|mid (*.mid)|
             *.mid|mp3 (*.mp3)|*.mp3|wav (*.wav)|*.wav|wma (*.wma)|*.wma"
   .DialogTitle = "Choose sound file..."
End With
CommonDialog1.ShowOpen
If Err <> 0 Then  ' No file selected to play.
   Exit Sub
End If
MusicFile = CommonDialog1.FileName
MusicName = CommonDialog1.FileTitle
InitPlay ' Go to InitPlay() procedure

InitPlay() 过程

' ' Close a device if open: 
' If Not mciWave.Mode = mciModeNotOpen 
' Then  mciWave.Command = "Close" End If 
' ' Opens a device using the MCI_OPEN command: 
' mciWave.Command = "Open" 
' Time of the song: mciWave.TimeFormat = mciFormatMilliseconds msec = (CDbl(mciWave.Length) / 1000) 

btnPlay_Click() 过程

'
' Set the number of milliseconds = ConInterval, let StatusUpdate events:
'
mciWave.UpdateInterval = ConInterval
'
' Plays a device using the MCI_PLAY command:
'
mciWave.Command = "Play"

btnPause_Click() 过程

'
' StatusUpdate events not occurred:
'
mciWave.UpdateInterval = 0
'
' Pauses playing using the MCI_PAUSE command:
'
mciWave.Command = "Pause"

btnStop_Click() 过程

'
' StatusUpdate events not occurred:
'
mciWave.UpdateInterval = 0
'
' Stops playing using the MCI_STOP command:
'
mciWave.Command = " Stop"

mciWave_StatusUpdate() 过程

'
' If the device is not playing, reset to the beginning:
' 
If mciWave.Position = mciWave.Length Then
   mciWave.UpdateInterval = 0  ' no StatusUpdate events occur.
   Exit Sub
End If
'
' Determine how much of the file has played:
'
CurrentValue = mciWave.Position
Value = CDbl((CurrentValue / 1000))
'
' view elapsed time:
'
ElapsedTime.Caption = Format$(Value, "00:00")
'
' Wait a moment before change new picture from ImageList2:
'
Counter = Counter + 1: If Counter = 25 Then Counter = 0
' view dance picture from ImageList2:
If (Counter / 5) = Int(Counter / 5) Then
   PicNum = Counter / 5 + 1
   DancePic.Picture = ImageList2.ListImages(PicNum).Picture
End If
'
' Wait a moment before change new picture from ImageList1:
'
MasterCounter = MasterCounter + 1: If MasterCounter = 1500 Then MasterCounter = 0
' view dance picture from ImageList1:
If (MasterCounter / 50) = Int(MasterCounter / 50) Then
   ImgNum = MasterCounter / 50 + 1
   MoviePic.Picture = ImageList1.ListImages(ImgNum).Picture ' view new picture
End If

你可以返回项目 AudioPlayer 的源代码文件来阅读完整的代码。如果需要更多帮助,可以参考 Visual Basic 帮助中的 Multimedia MCI Control

备注

解压缩 AudioPlayer.zip 文件后,你可以找到项目 AudioPlayer 的源代码文件。

最后的话

我希望这篇文章对您有所帮助,并能帮助您使用 MMControl 作为 Timer 创建一些应用程序。如果您有任何想法或发现任何问题,请告诉我。感谢 Code Project 以及所有人的支持。

历史

  • 2008 年 2 月 2 日:初始版本
© . All rights reserved.