home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / BMPPLAY.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  9.3 KB  |  314 lines

  1. VERSION 4.00
  2. Begin VB.Form PlayerForm 
  3.    Caption         =   "Bitmap Player"
  4.    ClientHeight    =   3825
  5.    ClientLeft      =   1680
  6.    ClientTop       =   975
  7.    ClientWidth     =   5850
  8.    Height          =   4515
  9.    Left            =   1620
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Top             =   345
  15.    Width           =   5970
  16.    Begin VB.TextBox NumFramesText 
  17.       Height          =   285
  18.       Left            =   1560
  19.       TabIndex        =   11
  20.       Text            =   "100"
  21.       Top             =   120
  22.       Width           =   375
  23.    End
  24.    Begin VB.OptionButton RunType 
  25.       Caption         =   "Looping"
  26.       Height          =   255
  27.       Index           =   2
  28.       Left            =   360
  29.       TabIndex        =   9
  30.       Top             =   2400
  31.       Width           =   1095
  32.    End
  33.    Begin VB.OptionButton RunType 
  34.       Caption         =   "Reversing"
  35.       Height          =   255
  36.       Index           =   1
  37.       Left            =   360
  38.       TabIndex        =   8
  39.       Top             =   2040
  40.       Width           =   1095
  41.    End
  42.    Begin VB.OptionButton RunType 
  43.       Caption         =   "One time"
  44.       Height          =   255
  45.       Index           =   0
  46.       Left            =   360
  47.       TabIndex        =   7
  48.       Top             =   1680
  49.       Value           =   -1  'True
  50.       Width           =   1095
  51.    End
  52.    Begin VB.TextBox FPSText 
  53.       Height          =   285
  54.       Left            =   1560
  55.       TabIndex        =   6
  56.       Text            =   "20"
  57.       Top             =   1080
  58.       Width           =   375
  59.    End
  60.    Begin VB.PictureBox MovieImage 
  61.       AutoRedraw      =   -1  'True
  62.       AutoSize        =   -1  'True
  63.       Height          =   375
  64.       Index           =   0
  65.       Left            =   600
  66.       ScaleHeight     =   21
  67.       ScaleMode       =   3  'Pixel
  68.       ScaleWidth      =   21
  69.       TabIndex        =   2
  70.       Top             =   3480
  71.       Visible         =   0   'False
  72.       Width           =   375
  73.    End
  74.    Begin VB.CommandButton CmdStart 
  75.       Caption         =   "Start"
  76.       Default         =   -1  'True
  77.       Enabled         =   0   'False
  78.       Height          =   375
  79.       Left            =   600
  80.       TabIndex        =   1
  81.       Top             =   3000
  82.       Width           =   855
  83.    End
  84.    Begin VB.PictureBox Canvas 
  85.       Height          =   3810
  86.       Left            =   2040
  87.       ScaleHeight     =   250
  88.       ScaleMode       =   3  'Pixel
  89.       ScaleWidth      =   250
  90.       TabIndex        =   0
  91.       Top             =   0
  92.       Width           =   3810
  93.    End
  94.    Begin VB.Label Label2 
  95.       Caption         =   "Frames to load:"
  96.       Height          =   255
  97.       Left            =   120
  98.       TabIndex        =   10
  99.       Top             =   120
  100.       Width           =   1455
  101.    End
  102.    Begin VB.Label Label1 
  103.       Caption         =   "Frames per second:"
  104.       Height          =   255
  105.       Index           =   1
  106.       Left            =   120
  107.       TabIndex        =   5
  108.       Top             =   1080
  109.       Width           =   1455
  110.    End
  111.    Begin VB.Label NumLabel 
  112.       BorderStyle     =   1  'Fixed Single
  113.       Caption         =   "0"
  114.       Height          =   255
  115.       Left            =   1560
  116.       TabIndex        =   4
  117.       Top             =   480
  118.       Width           =   375
  119.    End
  120.    Begin VB.Label Label1 
  121.       Caption         =   "Frame:"
  122.       Height          =   255
  123.       Index           =   0
  124.       Left            =   120
  125.       TabIndex        =   3
  126.       Top             =   480
  127.       Width           =   615
  128.    End
  129.    Begin MSComDlg.CommonDialog FileDialog 
  130.       Left            =   0
  131.       Top             =   3360
  132.       _version        =   65536
  133.       _extentx        =   847
  134.       _extenty        =   847
  135.       _stockprops     =   0
  136.       cancelerror     =   -1  'True
  137.    End
  138.    Begin VB.Menu mnuFile 
  139.       Caption         =   "&File"
  140.       Begin VB.Menu mnuFileLoad 
  141.          Caption         =   "&Load..."
  142.          Shortcut        =   ^L
  143.       End
  144.       Begin VB.Menu mnuFileSep 
  145.          Caption         =   "-"
  146.       End
  147.       Begin VB.Menu mnuFileExit 
  148.          Caption         =   "E&xit"
  149.       End
  150.    End
  151. Attribute VB_Name = "PlayerForm"
  152. Attribute VB_Creatable = False
  153. Attribute VB_Exposed = False
  154. Option Explicit
  155. Dim NumImages As Integer
  156. Dim MaxImage As Integer
  157. Dim Playing As Boolean
  158. ' ************************************************
  159. ' Load the images.
  160. ' ************************************************
  161. Sub LoadImages(fname As String)
  162. Dim base As String
  163. Dim i As Integer
  164.     ' Get the base file name.
  165.     base = Left$(fname, Len(fname) - 5)
  166.     ' See how many frames the user wants to load.
  167.     If Not IsNumeric(NumFramesText.Text) Then _
  168.         NumFramesText.Text = Format$(10)
  169.     NumImages = CInt(NumFramesText.Text)
  170.     ' Create any needed picture boxes.
  171.     For i = MaxImage + 1 To NumImages - 1
  172.         Load MovieImage(i)
  173.     Next i
  174.     ' Get rid of any that are no longer needed.
  175.     For i = NumImages To MaxImage
  176.         Unload MovieImage(i)
  177.     Next i
  178.     MaxImage = NumImages - 1
  179.     ' Load the images.
  180.     On Error GoTo LoadPictureError
  181.     i = 0
  182.     Do While i < NumImages
  183.         NumLabel.Caption = Format$(i + 1)
  184.         NumLabel.Refresh
  185.         MovieImage(i).Picture = LoadPicture(base & Format$(i) & ".bmp")
  186.         i = i + 1
  187.     Loop
  188.     Canvas.Picture = MovieImage(0).Image
  189.     NumLabel.Caption = "0"
  190.     NumFramesText.Text = Format$(NumImages)
  191.     Exit Sub
  192. LoadPictureError:
  193.     ' We ran out of images early.
  194.     NumImages = i
  195.     NumFramesText.Text = Format$(NumImages)
  196.     Resume Next
  197. End Sub
  198. ' ***********************************************
  199. ' Run the animation until Playing is false.
  200. ' ***********************************************
  201. Sub PlayImages()
  202. Const RUN_TYPE_ONE_TIME = 0
  203. Const RUN_TYPE_BACK_AND_FORTH = 1
  204. Const RUN_TYPE_LOOPING = 2
  205. Dim i As Integer
  206. Dim ms_per_frame As Integer
  207. Dim next_time As Long
  208. Dim run_type As Integer
  209.     ' See long it should be between frames.
  210.     If Not IsNumeric(FPSText.Text) Then _
  211.         FPSText.Text = "20"
  212.     ms_per_frame = 1000 / CInt(FPSText.Text)
  213.                 
  214.     ' See what kind of run it is (looping, etc.).
  215.     For i = 0 To 2
  216.         If RunType(i).Value Then Exit For
  217.     Next i
  218.     run_type = i
  219.     ' Start the animation.
  220.     next_time = GetTickCount
  221.     Do While Playing
  222.         For i = 0 To NumImages - 1
  223.             Canvas.Picture = MovieImage(i).Image
  224.             NumLabel.Caption = Format$(i)
  225.             next_time = next_time + ms_per_frame
  226.             WaitTill next_time
  227.             
  228.             If Not Playing Then Exit Sub
  229.         Next i
  230.             
  231.         ' If this is a one time run, stop.
  232.         If run_type = RUN_TYPE_ONE_TIME Then Exit Do
  233.         
  234.         ' If this is a back and forth run, go back.
  235.         If run_type = RUN_TYPE_BACK_AND_FORTH Then
  236.             For i = NumImages - 2 To 1 Step -1
  237.                 Canvas.Picture = MovieImage(i).Image
  238.                 NumLabel.Caption = Format$(i)
  239.                 next_time = next_time + ms_per_frame
  240.                 WaitTill next_time
  241.                 
  242.                 If Not Playing Then Exit Sub
  243.             Next i
  244.         End If
  245.     Loop
  246. End Sub
  247. ' ************************************************
  248. ' Start or stop playing.
  249. ' ************************************************
  250. Private Sub CmdStart_Click()
  251.     If Playing Then
  252.         Playing = False
  253.         CmdStart.Caption = "Stopped"
  254.         CmdStart.Enabled = False
  255.     Else
  256.         CmdStart.Caption = "Stop"
  257.         Playing = True
  258.         PlayImages
  259.         Playing = False
  260.         CmdStart.Caption = "Start"
  261.         CmdStart.Enabled = True
  262.     End If
  263. End Sub
  264. Private Sub Form_Unload(Cancel As Integer)
  265.     End
  266. End Sub
  267. Private Sub mnuFileExit_Click()
  268.     Unload Me
  269. End Sub
  270. ' ***********************************************
  271. ' Load new image files.
  272. ' ***********************************************
  273. Private Sub mnuFileLoad_Click()
  274. Dim fname As String
  275.     ' Allow the user to pick a file.
  276.     On Error Resume Next
  277.     FileDialog.filename = "*_0.BMP"
  278.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  279.     FileDialog.ShowOpen
  280.     If Err.Number = cdlCancel Then
  281.         Exit Sub
  282.     ElseIf Err.Number <> 0 Then
  283.         Beep
  284.         MsgBox "Error selecting file.", , vbExclamation
  285.         Exit Sub
  286.     End If
  287.     On Error GoTo 0
  288.     fname = Trim$(FileDialog.filename)
  289.     FileDialog.InitDir = Left$(fname, Len(fname) _
  290.         - Len(FileDialog.FileTitle) - 1)
  291.     ' Load the pictures.
  292.     WaitStart
  293.     LoadImages fname
  294.     WaitEnd
  295.     CmdStart.Enabled = True
  296. End Sub
  297. ' ***********************************************
  298. ' Restore the mouse pointers for the form and all
  299. ' the picture boxes.
  300. ' ***********************************************
  301. Sub WaitEnd()
  302.     MousePointer = vbDefault
  303.     Canvas.MousePointer = vbDefault
  304. End Sub
  305. ' ***********************************************
  306. ' Give the form and all the picture boxes an
  307. ' hourglass cursor.
  308. ' ***********************************************
  309. Sub WaitStart()
  310.     MousePointer = vbHourglass
  311.     Canvas.MousePointer = vbHourglass
  312.     DoEvents
  313. End Sub
  314.