Add the following code to a BAS module: --------------------------------------------------------------------------------
Option ExplicitPublic Function LoadGif(sFile As String, aImg As Variant) As Long
Dim hFile As Long Dim sImgHeader As String Dim sFileHeader As String Dim sBuff As String Dim sPicsBuff As String Dim nImgCount As Long Dim i As Long Dim j As Long Dim xOff As Long Dim yOff As Long Dim TimeWait As Long Dim sGifMagic As String
If Dir$(sFile) = "" Or sFile = "" Then MsgBox "File " & sFile & " not found", vbCritical Exit Function End If
'magic string signifying end of 'header and end of a gif frame sGifMagic = Chr$(0) & Chr$(33) & Chr$(249)
If aImg.Count > 1 Then For i = 1 To aImg.Count - 1 Unload aImg(i) Next i End If
'load the gif into a string buffer hFile = FreeFile
Open sFile For Binary Access Read As hFile sBuff = String(LOF(hFile), Chr(0)) Get #hFile, , sBuff Close #hFile
'begin process of splitting the loaded 'string into individual gif images. 'Each image will be assigned to its own Image control in a control array. The 'First, separate the header information from 'the image info. i = 1 nImgCount = 0 j = InStr(1, sBuff, sGifMagic) + 1 sFileHeader = Left(sBuff, j)
'A gif's first characters are "GIF" 'followed by the gif type, ie "GIF89am", 'so if this missing, its not a gif. If Left$(sFileHeader, 3) <> "GIF" Then MsgBox "This file is not a *.gif file", vbCritical Exit Function End If
LoadGif = True
'set pointer ahead 2 bytes from the 'end of the gif magic number i = j + 2
'if the fileheader size was greater than '127, the info on how many individual 'frames the gif has is located within the header. If Len(sFileHeader) >= 127 Then RepeatTimes& = Asc(Mid(sFileHeader, 126, 1)) + (Asc(Mid(sFileHeader, 127, 1)) * 256&) Else RepeatTimes = 0 End If
'create a temporary file in the current directory hFile = FreeFile Open "temp.gif" For Binary As hFile
'split out each frame of the gif, and 'write each the frame to the temporary file. 'Then load an image control for the frame, 'and load the temp file into that control. Do
'increment counter nImgCount = nImgCount + 1
'locate next frame end j = InStr(i, sBuff, sGifMagic) + 3
'another check If j > Len(sGifMagic) Then
'pad an output string, fill with the 'frame info, and write to disk. A header 'needs to be added as well, to assure 'LoadPicture recognizes it as a gif. 'Since VB's LoadPicture command ignores 'header info and loads animated gifs as 'static, we can safely reuse the header 'extracted above. sPicsBuff = String(Len(sFileHeader) + j - i, Chr$(0)) sPicsBuff = sFileHeader & Mid(sBuff, i - 1, j - i) Put #hFile, 1, sPicsBuff
'The first part of the 'extracted data is frame info sImgHeader = Left(Mid(sBuff, i - 1, j - i), 16)
'embedded in the frame info is a 'field that represents the frame delay TimeWait = ((Asc(Mid(sImgHeader, 4, 1))) + (Asc(Mid(sImgHeader, 5, 1)) * 256&)) * 10&
'assign the data. If nImgCount > 1 Then
'if this is the second or later 'frame, load an image control 'for the frame Load aImg(nImgCount - 1)
'the frame header also contains 'the x and y offsets of the image 'in relation to the first (0) image. xOff = Asc(Mid(sImgHeader, 9, 1)) + (Asc(Mid(sImgHeader, 10, 1)) * 256&) yOff = Asc(Mid(sImgHeader, 11, 1)) + (Asc(Mid(sImgHeader, 12, 1)) * 256&)
'position the image controls at 'the required position aImg(nImgCount - 1).Left = aImg(0).Left + (xOff * Screen.TwipsPerPixelX) aImg(nImgCount - 1).Top = aImg(0).Top + (yOff * Screen.TwipsPerPixelY)
End If
'use each control's .Tag property to 'store the frame delay period, and 'load the picture into the image control. aImg(nImgCount - 1).Tag = TimeWait aImg(nImgCount - 1).Picture = LoadPicture("temp.gif")
'update pointer i = j End If
'when the j = Instr() command above returns 0, '3 is added, so if j = 3 there was no more 'data in the header. We're done. Loop Until j = 3
'close and nuke the temp file Close #hFile Kill "temp.gif" TotalFrames = aImg.Count - 1
LoadGif = TotalFrames Exit Function
ErrHandler: MsgBox "Error No. " & Err.Number & " when reading file", vbCritical LoadGif = False On Error GoTo 0
End Function '--end block--'
Form Code
Add a text box (Text1), a Listbox (List1), a label (Label1), and three command buttons (Command1, Command2, Command3) to a form. Add an image control, and set its Index property to 0 to create a control array. Add the following code to the form: --------------------------------------------------------------------------------
Option Explicit
Private FrameCount As Long Private Const LB_DIR As Long = &H18D Private Const DDL_ARCHIVE As Long = &H20 Private Const DDL_EXCLUSIVE As Long = &H8000 Private Const DDL_FLAGS As Long = DDL_ARCHIVE Or DDL_EXCLUSIVE
Private Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long
End IfEnd Sub Private Sub Timer1_Timer() Dim i As Long
If FrameCount < TotalFrames Then
Image1(FrameCount).Visible = False FrameCount = FrameCount + 1 Else FrameCount = 0 For i = 1 To Image1.Count - 1 Image1(i).Visible = False Next i End If
email [email protected]
狼兄好:
源码运行时产生错误:
选择提供的ball.gif和clip.gif时此句j = InStr(1, buf, GifEnd) + 1得值1;然后提示
This file is not a *.gif file。
选择另一gif文件,则执行此句时会产生溢出(错误号6)
TimeWait = ((Asc(Mid(imgHeader, 4, 1))) + (Asc(Mid(imgHeader, 5, 1)) * 256&)) * 10&
请指教!谢谢。
其实,呵呵,很简单,把ie控件放在一个picture控件里面,然后调节picture的大小,把滚动条挡住就可以了,哈哈
Option ExplicitPublic Function LoadGif(sFile As String, aImg As Variant) As Long
Dim hFile As Long
Dim sImgHeader As String
Dim sFileHeader As String
Dim sBuff As String
Dim sPicsBuff As String
Dim nImgCount As Long
Dim i As Long
Dim j As Long
Dim xOff As Long
Dim yOff As Long
Dim TimeWait As Long
Dim sGifMagic As String
If Dir$(sFile) = "" Or sFile = "" Then
MsgBox "File " & sFile & " not found", vbCritical
Exit Function
End If
'magic string signifying end of
'header and end of a gif frame
sGifMagic = Chr$(0) & Chr$(33) & Chr$(249)
If aImg.Count > 1 Then
For i = 1 To aImg.Count - 1
Unload aImg(i)
Next i
End If
'load the gif into a string buffer
hFile = FreeFile
Open sFile For Binary Access Read As hFile
sBuff = String(LOF(hFile), Chr(0))
Get #hFile, , sBuff
Close #hFile
'begin process of splitting the loaded
'string into individual gif images. 'Each image will be assigned to its own Image control in a control array. The
'First, separate the header information from
'the image info.
i = 1
nImgCount = 0
j = InStr(1, sBuff, sGifMagic) + 1
sFileHeader = Left(sBuff, j)
'A gif's first characters are "GIF"
'followed by the gif type, ie "GIF89am",
'so if this missing, its not a gif.
If Left$(sFileHeader, 3) <> "GIF" Then
MsgBox "This file is not a *.gif file", vbCritical
Exit Function
End If
LoadGif = True
'set pointer ahead 2 bytes from the
'end of the gif magic number
i = j + 2
'if the fileheader size was greater than
'127, the info on how many individual
'frames the gif has is located within the header.
If Len(sFileHeader) >= 127 Then
RepeatTimes& = Asc(Mid(sFileHeader, 126, 1)) + (Asc(Mid(sFileHeader, 127, 1)) * 256&)
Else
RepeatTimes = 0
End If
'create a temporary file in the current directory
hFile = FreeFile
Open "temp.gif" For Binary As hFile
'split out each frame of the gif, and
'write each the frame to the temporary file.
'Then load an image control for the frame,
'and load the temp file into that control.
Do
'increment counter
nImgCount = nImgCount + 1
'locate next frame end
j = InStr(i, sBuff, sGifMagic) + 3
'another check
If j > Len(sGifMagic) Then
'pad an output string, fill with the
'frame info, and write to disk. A header
'needs to be added as well, to assure
'LoadPicture recognizes it as a gif.
'Since VB's LoadPicture command ignores
'header info and loads animated gifs as
'static, we can safely reuse the header
'extracted above.
sPicsBuff = String(Len(sFileHeader) + j - i, Chr$(0))
sPicsBuff = sFileHeader & Mid(sBuff, i - 1, j - i)
Put #hFile, 1, sPicsBuff
'The first part of the
'extracted data is frame info
sImgHeader = Left(Mid(sBuff, i - 1, j - i), 16)
'embedded in the frame info is a
'field that represents the frame delay
TimeWait = ((Asc(Mid(sImgHeader, 4, 1))) + (Asc(Mid(sImgHeader, 5, 1)) * 256&)) * 10&
'assign the data.
If nImgCount > 1 Then
'if this is the second or later
'frame, load an image control
'for the frame
Load aImg(nImgCount - 1)
'the frame header also contains
'the x and y offsets of the image
'in relation to the first (0) image.
xOff = Asc(Mid(sImgHeader, 9, 1)) + (Asc(Mid(sImgHeader, 10, 1)) * 256&)
yOff = Asc(Mid(sImgHeader, 11, 1)) + (Asc(Mid(sImgHeader, 12, 1)) * 256&)
'position the image controls at
'the required position
aImg(nImgCount - 1).Left = aImg(0).Left + (xOff * Screen.TwipsPerPixelX)
aImg(nImgCount - 1).Top = aImg(0).Top + (yOff * Screen.TwipsPerPixelY)
End If
'use each control's .Tag property to
'store the frame delay period, and
'load the picture into the image control.
aImg(nImgCount - 1).Tag = TimeWait
aImg(nImgCount - 1).Picture = LoadPicture("temp.gif")
'update pointer
i = j
End If
'when the j = Instr() command above returns 0,
'3 is added, so if j = 3 there was no more
'data in the header. We're done.
Loop Until j = 3
'close and nuke the temp file
Close #hFile
Kill "temp.gif" TotalFrames = aImg.Count - 1
LoadGif = TotalFrames
Exit Function
ErrHandler: MsgBox "Error No. " & Err.Number & " when reading file", vbCritical
LoadGif = False
On Error GoTo 0
End Function
'--end block--'
Form Code
Add a text box (Text1), a Listbox (List1), a label (Label1), and three command buttons (Command1, Command2, Command3) to a form. Add an image control, and set its Index property to 0 to create a control array. Add the following code to the form: --------------------------------------------------------------------------------
Option Explicit
Private FrameCount As Long
Private Const LB_DIR As Long = &H18D
Private Const DDL_ARCHIVE As Long = &H20
Private Const DDL_EXCLUSIVE As Long = &H8000
Private Const DDL_FLAGS As Long = DDL_ARCHIVE Or DDL_EXCLUSIVE
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Sub Command1_Click()
Dim nFrames As Long
Timer1.Enabled = False
Text1.Text = App.Path & "\" & List1.List(List1.ListIndex)
nFrames = LoadGif(Text1, Image1)
If nFrames > 0 Then
Label1.Caption = "Frames: " & nFrames
FrameCount = 0
Timer1.Interval = CLng(Image1(0).Tag)
Timer1.Enabled = True
End If
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Command3_Click()
Timer1.Enabled = True
End Sub
Private Sub Form_Load() Timer1.Enabled = False
Call SendMessage(List1.hwnd, _
LB_DIR, _
DDL_FLAGS, _
ByVal App.Path & "\*.gif")
Text1.Text = ""
If List1.ListCount > 0 Then
List1.ListIndex = 0
Text1.Text = List1.List(List1.ListIndex)
End If
End Sub
Private Sub List1_DblClick() Dim nFrames As Long
Text1.Text = App.Path & "\" & List1.List(List1.ListIndex)
nFrames = LoadGif(Text1, Image1)
If nFrames > 0 Then
Label1.Caption = "Frames: " & nFrames
FrameCount = 0
Timer1.Interval = CLng(Image1(0).Tag)
Timer1.Enabled = True
End IfEnd Sub
Private Sub Timer1_Timer() Dim i As Long
If FrameCount < TotalFrames Then
Image1(FrameCount).Visible = False
FrameCount = FrameCount + 1
Else
FrameCount = 0
For i = 1 To Image1.Count - 1
Image1(i).Visible = False
Next i
End If
Image1(FrameCount).Visible = True
Timer1.Interval = CLng(Image1(FrameCount).Tag)
End Sub
'--end block--'
sGifMagic = Chr$(0) & Chr$(33) & Chr$(249)
这句是不能通过的!我尝试很多次都这样!!!!!!!!!!