PictureBox控件中显示GIF动画(图片)

 '在PictureBox控件中显示GIF动画(图片),凡有句柄hwnd的窗体控件均可以
'在窗体Form1上添加2个命令按钮Command1,Command2和1个图片框控件Picture1

Option Explicit

'调用运动图像播放类型库Quartz.dll,在c:/windows/system32目录下
Private pMC As FilgraphManager '定义pMC为FilgraphManager对象
Private pVW As IVideoWindow '定义pVW为IVideoWindow对象

'播放GIF动画
Private Sub Command1_Click()

        '终止播放
        On Error Resume Next
        Set pMC = New FilgraphManager
        pMC.Stop
        pMC.RenderFile ""
       
        '加载GIF文件
        On Error GoTo Lhandle
        pMC.RenderFile "E:/offline.gif" '加载GIF动画(图片),并在PictureBox中播放
       
        On Error Resume Next
        Set pVW = pMC
        pVW.WindowStyle = CLng(&H6000000)
       
        '限定图像在Picture1的大小中
        pVW.Left = 0: pVW.Top = 0
        pVW.Width = Picture1.ScaleWidth
        pVW.Height = Picture1.ScaleHeight
       
        pVW.Owner = Picture1.hWnd '在Picture1中播放
        pVW.MessageDrain = Picture1.hWnd '此行非常重要,用来定义图像的右键菜单所必须
        pMC.Run '一直播放
       
        Exit Sub
Lhandle:
        MsgBox ("加载文件错误!")
End Sub

'停止播放GIF动画
Private Sub Command2_Click()
        pMC.Stop
        Set pVW = Nothing
        Set pMC = Nothing
End Sub

Private Sub Form_Load()
        Me.ScaleMode = 3
        Picture1.ScaleMode = 3
End Sub

你可能感兴趣的:(PictureBox控件中显示GIF动画(图片))