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

  1. VERSION 4.00
  2. Begin VB.Form StockForm 
  3.    Caption         =   "Stocks"
  4.    ClientHeight    =   3990
  5.    ClientLeft      =   1875
  6.    ClientTop       =   1665
  7.    ClientWidth     =   5190
  8.    Height          =   4680
  9.    Left            =   1815
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   3990
  12.    ScaleWidth      =   5190
  13.    Top             =   1035
  14.    Width           =   5310
  15.    Begin VB.TextBox FPSText 
  16.       Height          =   285
  17.       Left            =   1440
  18.       TabIndex        =   5
  19.       Text            =   "10"
  20.       Top             =   3600
  21.       Width           =   375
  22.    End
  23.    Begin VB.CommandButton CmdStart 
  24.       Caption         =   "Start"
  25.       Default         =   -1  'True
  26.       Height          =   495
  27.       Left            =   2160
  28.       TabIndex        =   1
  29.       Top             =   3480
  30.       Width           =   855
  31.    End
  32.    Begin VB.PictureBox GraphPict 
  33.       AutoRedraw      =   -1  'True
  34.       Height          =   3375
  35.       Left            =   0
  36.       ScaleHeight     =   -100
  37.       ScaleLeft       =   0.5
  38.       ScaleMode       =   0  'User
  39.       ScaleTop        =   100
  40.       ScaleWidth      =   10.75
  41.       TabIndex        =   0
  42.       Top             =   0
  43.       Width           =   5175
  44.    End
  45.    Begin VB.Label Label1 
  46.       Caption         =   "Frames per second:"
  47.       Height          =   255
  48.       Index           =   1
  49.       Left            =   0
  50.       TabIndex        =   4
  51.       Top             =   3600
  52.       Width           =   1455
  53.    End
  54.    Begin VB.Label DayLabel 
  55.       BorderStyle     =   1  'Fixed Single
  56.       Height          =   255
  57.       Left            =   4800
  58.       TabIndex        =   3
  59.       Top             =   3600
  60.       Width           =   375
  61.    End
  62.    Begin VB.Label Label1 
  63.       Caption         =   "Day:"
  64.       Height          =   255
  65.       Index           =   0
  66.       Left            =   4440
  67.       TabIndex        =   2
  68.       Top             =   3600
  69.       Width           =   375
  70.    End
  71.    Begin VB.Menu mnuFile 
  72.       Caption         =   "&File"
  73.       Begin VB.Menu mnuFileExit 
  74.          Caption         =   "E&xit"
  75.       End
  76.    End
  77. Attribute VB_Name = "StockForm"
  78. Attribute VB_Creatable = False
  79. Attribute VB_Exposed = False
  80. Option Explicit
  81. Const NUM_STOCKS = 10
  82. Const NUM_FRAMES = 100
  83. Dim Data(1 To NUM_FRAMES, 1 To NUM_STOCKS) As Integer
  84. Dim Playing As Boolean
  85. ' ************************************************
  86. ' Generate some random data.
  87. ' ************************************************
  88. Sub InitData()
  89. Dim stock As Integer
  90. Dim frame As Integer
  91.     ' Set the initial values between 30 and 70.
  92.     For stock = 1 To NUM_STOCKS
  93.         Data(1, stock) = Int(41 * Rnd + 30)
  94.     Next stock
  95.     ' Make values for the other frames.
  96.     ' Each value is up to +/- 5 different than the
  97.     ' previous value for the same stock.
  98.     For frame = 2 To NUM_FRAMES
  99.         For stock = 1 To NUM_STOCKS
  100.             Data(frame, stock) = _
  101.                 Data(frame - 1, stock) + _
  102.                 Int(11 * Rnd - 5)
  103.             If Data(frame, stock) < 0 Then _
  104.                 Data(frame, stock) = 5
  105.             If Data(frame, stock) > 100 Then _
  106.                 Data(frame, stock) = 95
  107.         Next stock
  108.     Next frame
  109. End Sub
  110. ' ************************************************
  111. ' Start the animation.
  112. ' ************************************************
  113. Private Sub CmdStart_Click()
  114.     If Playing Then
  115.         Playing = False
  116.         CmdStart.Caption = "Stopped"
  117.         CmdStart.Enabled = False
  118.     Else
  119.         CmdStart.Caption = "Stop"
  120.         Playing = True
  121.         InitData
  122.         PlayData
  123.         Playing = False
  124.         CmdStart.Caption = "Start"
  125.         CmdStart.Enabled = True
  126.     End If
  127. End Sub
  128. ' ************************************************
  129. ' Play the animation.
  130. ' ************************************************
  131. Sub PlayData()
  132. Dim mpf As Long     ' Milliseconds per frame.
  133. Dim frame As Integer
  134. Dim stock As Integer
  135. Dim next_time As Long
  136. Dim old_style As Integer
  137.     ' Set FillStyle to vbSolid.
  138.     old_style = GraphPict.FillStyle
  139.     GraphPict.FillStyle = vbSolid
  140.     ' See how fast we should go.
  141.     If Not IsNumeric(FPSText.Text) Then _
  142.         FPSText.Text = "10"
  143.     mpf = 1000 \ CLng(FPSText.Text)
  144.     ' Draw the background.
  145.     '
  146.     ' Note that we must cover all of the background
  147.     ' so it becomes part of the image. Then Cls
  148.     ' will restore it.
  149.     GraphPict.Line (0, 0)-(NUM_STOCKS + 1.25, 50), RGB(128, 128, 128), BF
  150.     GraphPict.Line (0, 50)-(NUM_STOCKS + 1.25, 100), GraphPict.BackColor, BF
  151.     ' Make the picture a permanent part of the
  152.     ' background.
  153.     GraphPict.Picture = GraphPict.Image
  154.     ' Start the animation.
  155.     next_time = GetTickCount()
  156.     For frame = 1 To NUM_FRAMES
  157.         If Not Playing Then Exit For
  158.         
  159.         ' Draw the graph.
  160.         GraphPict.Cls
  161.         For stock = 1 To NUM_STOCKS
  162.             If Data(frame, stock) > 50 Then
  163.                 GraphPict.Line (stock, 0)-(stock + 0.75, 50), vbRed, BF
  164.                 GraphPict.Line (stock, 50)-(stock + 0.75, Data(frame, stock)), vbGreen, BF
  165.             Else
  166.                 GraphPict.Line (stock, 0)-(stock + 0.75, Data(frame, stock)), vbRed, BF
  167.             End If
  168.         Next stock
  169.         GraphPict.Line (0, 50)-(NUM_STOCKS + 1.25, 50), vbBlack, BF
  170.         DayLabel.Caption = Format$(frame)
  171.             
  172.         ' Wait until it's time for the next frame.
  173.         next_time = next_time + mpf
  174.         WaitTill next_time
  175.     Next frame
  176.     ' Restore the old FillStyle.
  177.     GraphPict.FillStyle = old_style
  178. End Sub
  179. Private Sub Form_Unload(Cancel As Integer)
  180.     End
  181. End Sub
  182. Private Sub mnuFileExit_Click()
  183.     Unload Me
  184. End Sub
  185.