home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / animat3a / frmanigi.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-10-17  |  5.7 KB  |  166 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    Caption         =   "Animated GIF"
  4.    ClientHeight    =   1905
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   6195
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   1905
  10.    ScaleWidth      =   6195
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.TextBox txFile 
  13.       Height          =   330
  14.       Left            =   165
  15.       TabIndex        =   1
  16.       Top             =   150
  17.       Width           =   3780
  18.    End
  19.    Begin VB.Timer AnimationTimer 
  20.       Interval        =   1000
  21.       Left            =   195
  22.       Top             =   555
  23.    End
  24.    Begin VB.CommandButton btnPlay 
  25.       Caption         =   "PLAY"
  26.       Height          =   465
  27.       Left            =   4200
  28.       TabIndex        =   0
  29.       Top             =   105
  30.       Width           =   1380
  31.    End
  32.    Begin VB.Image AnimatedGIF 
  33.       Appearance      =   0  'Flat
  34.       Height          =   900
  35.       Index           =   0
  36.       Left            =   855
  37.       Top             =   615
  38.       Width           =   1500
  39.    End
  40. Attribute VB_Name = "frmMain"
  41. Attribute VB_GlobalNameSpace = False
  42. Attribute VB_Creatable = False
  43. Attribute VB_PredeclaredId = True
  44. Attribute VB_Exposed = False
  45. Dim RepeatTimes&
  46. Dim RepeatCount&
  47. Private Sub btnPlay_Click()
  48.        
  49.     Call LoadAniGif(txFile.Text, AnimatedGIF)
  50. End Sub
  51. Sub LoadAniGif(xFile As String, xImgArray)
  52.     If Not IIf(Dir$(xFile) = "", False, True) Or xFile = "" Then
  53.         MsgBox "File not found.", vbExclamation, "File Error"
  54.         Exit Sub
  55.     End If
  56.         
  57.     Dim F1, F2
  58.     Dim AnimatedGIFs() As String
  59.     Dim imgHeader As String
  60.     Static buf$, picbuf$
  61.     Dim fileHeader As String
  62.     Dim imgCount
  63.     Dim i&, j&, xOff&, yOff&, TimeWait&
  64.     Dim GifEnd
  65.     GifEnd = Chr(0) & "!
  66.     AnimationTimer.Enabled = False
  67.     For i = 1 To xImgArray.Count - 1
  68.         Unload xImgArray(i)
  69.     Next i
  70.     F1 = FreeFile
  71. On Error GoTo badFile:
  72.     Open xFile For Binary Access Read As F1
  73.         buf = String(LOF(F1), Chr(0))
  74.         Get #F1, , buf
  75.     Close F1
  76.     i = 1
  77.     imgCount = 0
  78.     j = (InStr(1, buf, GifEnd) + Len(GifEnd)) - 2
  79.     fileHeader = Left(buf, j)
  80.     i = j + 2
  81.     RepeatTimes& = Asc(Mid(fileHeader, 126, 1)) + (Asc(Mid(fileHeader, 127, 1)) * 256)
  82.     Do
  83.         imgCount = imgCount + 1
  84.         j = InStr(i, buf, GifEnd) + Len(GifEnd)
  85.         If j > Len(GifEnd) Then
  86.             F2 = FreeFile
  87.             Open "tmp.gif" For Binary As F2
  88.                 picbuf = String(Len(fileHeader) + j - i, Chr(0))
  89.                 picbuf = fileHeader & Mid(buf, i - 1, j - i)
  90.                 Put #F2, 1, picbuf
  91.                 imgHeader = Left(Mid(buf, i - 1, j - i), 16)
  92.             Close F2
  93.             
  94.             TimeWait = ((Asc(Mid(imgHeader, 4, 1))) + (Asc(Mid(imgHeader, 5, 1)) * 256)) * 10
  95.             If imgCount > 1 Then
  96.                 xOff = Asc(Mid(imgHeader, 9, 1)) + (Asc(Mid(imgHeader, 10, 1)) * 256)
  97.                 yOff = Asc(Mid(imgHeader, 11, 1)) + (Asc(Mid(imgHeader, 12, 1)) * 2561)
  98.                 Load xImgArray(imgCount - 1)
  99.                 xImgArray(imgCount - 1).ZOrder 0
  100.                 xImgArray(imgCount - 1).Left = xImgArray(0).Left + (xOff * 15)
  101.                 xImgArray(imgCount - 1).Top = xImgArray(0).Top + (yOff * 15)
  102.             End If
  103.             xImgArray(imgCount - 1).Tag = TimeWait
  104.             xImgArray(imgCount - 1).Picture = LoadPicture("tmp.gif")
  105.             Kill ("tmp.gif")
  106.             
  107.             i = j '+ 1
  108.         End If
  109.     Loop Until j = Len(GifEnd)
  110.     If i < Len(buf) Then
  111.         F2 = FreeFile
  112.         Open "tmp.gif" For Binary As F2
  113.             picbuf = String(Len(fileHeader) + Len(buf) - i, Chr(0))
  114.             picbuf = fileHeader & Mid(buf, i - 1, Len(buf) - i)
  115.             Put #F2, 1, picbuf
  116.             imgHeader = Left(Mid(buf, i - 1, Len(buf) - i), 16)
  117.         Close F2
  118.         TimeWait = ((Asc(Mid(imgHeader, 4, 1))) + (Asc(Mid(imgHeader, 5, 1)) * 256)) * 10
  119.         If imgCount > 1 Then
  120.             xOff = Asc(Mid(imgHeader, 9, 1)) + (Asc(Mid(imgHeader, 10, 1)) * 256)
  121.             yOff = Asc(Mid(imgHeader, 11, 1)) + (Asc(Mid(imgHeader, 12, 1)) * 2561)
  122.             Load xImgArray(imgCount - 1)
  123.             xImgArray(imgCount - 1).ZOrder 0
  124.             xImgArray(imgCount - 1).Left = xImgArray(0).Left + (xOff * 15)
  125.             xImgArray(imgCount - 1).Top = xImgArray(0).Top + (yOff * 15)
  126.         End If
  127.         xImgArray(imgCount - 1).Tag = TimeWait
  128.         xImgArray(imgCount - 1).Picture = LoadPicture("tmp.gif")
  129.         Kill ("tmp.gif")
  130.     End If
  131. On Error GoTo badTime
  132.     AnimationTimer.Interval = CInt(xImgArray(0).Tag)
  133. badTime:
  134.     AnimationTimer.Enabled = True
  135. Exit Sub
  136. badFile:
  137.     MsgBox "File not found.", vbExclamation, "File Error"
  138. End Sub
  139. Private Sub AnimationTimer_Timer()
  140.     For i = 0 To AnimatedGIF.Count
  141.         If i = AnimatedGIF.Count Then
  142.             If RepeatTimes > 0 Then
  143.                 RepeatCount = RepeatCount + 1
  144.                 If RepeatCount > RepeatTimes Then
  145.                     AnimationTimer.Enabled = False
  146.                     Exit Sub
  147.                 End If
  148.             End If
  149.             For j = 1 To AnimatedGIF.Count - 1
  150.                 AnimatedGIF(j).Visible = False
  151.             Next j
  152. On Error GoTo badTime
  153.             AnimationTimer.Interval = CLng(AnimatedGIF(0).Tag)
  154. badTime:
  155.             Exit For
  156.         End If
  157.         If AnimatedGIF(i).Visible = False Then
  158.             AnimationTimer.Interval = CLng(AnimatedGIF(i).Tag)
  159. On Error GoTo badTime2
  160.             AnimatedGIF(i).Visible = True
  161. badTime2:
  162.             Exit For
  163.         End If
  164.     Next i
  165. End Sub
  166.