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

  1. VERSION 4.00
  2. Begin VB.Form PalAnimForm 
  3.    Caption         =   "Palette Animation"
  4.    ClientHeight    =   4500
  5.    ClientLeft      =   2340
  6.    ClientTop       =   1320
  7.    ClientWidth     =   3975
  8.    Height          =   5190
  9.    Left            =   2280
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   300
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   265
  14.    Top             =   690
  15.    Width           =   4095
  16.    Begin VB.PictureBox Canvas 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   3975
  19.       Left            =   0
  20.       Picture         =   "PALANIM.frx":0000
  21.       ScaleHeight     =   261
  22.       ScaleMode       =   3  'Pixel
  23.       ScaleWidth      =   261
  24.       TabIndex        =   1
  25.       Top             =   0
  26.       Width           =   3975
  27.       Begin VB.Label Label2 
  28.          Alignment       =   2  'Center
  29.          Caption         =   "Animation"
  30.          BeginProperty Font 
  31.             name            =   "MS Sans Serif"
  32.             charset         =   1
  33.             weight          =   700
  34.             size            =   13.5
  35.             underline       =   0   'False
  36.             italic          =   -1  'True
  37.             strikethrough   =   0   'False
  38.          EndProperty
  39.          Height          =   375
  40.          Left            =   1200
  41.          TabIndex        =   3
  42.          Top             =   1800
  43.          Width           =   1575
  44.       End
  45.       Begin VB.Label Label1 
  46.          Alignment       =   2  'Center
  47.          Caption         =   "Palette"
  48.          BeginProperty Font 
  49.             name            =   "MS Sans Serif"
  50.             charset         =   1
  51.             weight          =   700
  52.             size            =   13.5
  53.             underline       =   0   'False
  54.             italic          =   -1  'True
  55.             strikethrough   =   0   'False
  56.          EndProperty
  57.          Height          =   375
  58.          Left            =   1440
  59.          TabIndex        =   2
  60.          Top             =   1440
  61.          Width           =   1095
  62.       End
  63.    End
  64.    Begin VB.CommandButton CmdGo 
  65.       Caption         =   "Go"
  66.       Default         =   -1  'True
  67.       Height          =   495
  68.       Left            =   1560
  69.       TabIndex        =   0
  70.       Top             =   4005
  71.       Width           =   735
  72.    End
  73.    Begin VB.Menu mnuFile 
  74.       Caption         =   "&File"
  75.       Begin VB.Menu mnuFileExit 
  76.          Caption         =   "E&xit"
  77.       End
  78.    End
  79. Attribute VB_Name = "PalAnimForm"
  80. Attribute VB_Creatable = False
  81. Attribute VB_Exposed = False
  82. Option Explicit
  83. Dim SysPalSize As Integer
  84. Dim NumStaticColors As Integer
  85. Dim StaticColor1 As Integer
  86. Dim StaticColor2 As Integer
  87. Dim LogPal As Integer
  88. Dim palentry(0 To 255) As PALETTEENTRY
  89. Dim Running As Boolean
  90. Dim Component(0 To 29) As Byte
  91. Dim StartColor As Integer
  92. ' ************************************************
  93. ' Change the color palette until the user presses
  94. ' the Stop button.
  95. ' ************************************************
  96. Sub AnimateColors()
  97. Dim next_time As Long
  98. Dim mpf As Long
  99.     mpf = 1000 / 20 ' Update 20 times per second.
  100.     next_time = GetTickCount()
  101.     Do While Running
  102.         SetColors
  103.         next_time = next_time + mpf
  104.         WaitTill next_time
  105.     Loop
  106. End Sub
  107. ' ************************************************
  108. ' Draw the circle.
  109. ' ************************************************
  110. Sub xDrawCircle()
  111. Const PI = 3.14159
  112. Const Dtheta = PI / 60
  113. Dim r1 As Single
  114. Dim r2 As Single
  115. Dim xmid As Single
  116. Dim ymid As Single
  117. Dim status As Long
  118. Dim i As Integer
  119. Dim pts(1 To 4) As POINTAPI
  120. Dim theta As Single
  121. Dim clr As Integer
  122. Dim new_pen As Long
  123. Dim old_pen As Long
  124. Dim new_brush As Long
  125. Dim old_brush As Long
  126.     r1 = Canvas.ScaleWidth * 0.4
  127.     r2 = Canvas.ScaleWidth * 0.45
  128.     xmid = Canvas.ScaleWidth / 2
  129.     ymid = Canvas.ScaleHeight / 2
  130.     pts(3).x = xmid + r2
  131.     pts(3).y = ymid + 0
  132.     pts(4).x = xmid + r1
  133.     pts(4).y = ymid + 0
  134.     For theta = Dtheta To 360 Step Dtheta
  135.         pts(1).x = pts(4).x
  136.         pts(1).y = pts(4).y
  137.         pts(2).x = pts(3).x
  138.         pts(2).y = pts(3).y
  139.         pts(3).x = xmid + r2 * Cos(theta)
  140.         pts(3).y = ymid + r2 * Sin(theta)
  141.         pts(4).x = xmid + r1 * Cos(theta)
  142.         pts(4).y = ymid + r1 * Sin(theta)
  143.         
  144.         ' Create the pen and brush.
  145.         new_pen = CreatePen(PS_SOLID, 1, &H1000000 + 100 + clr)
  146.         old_pen = SelectObject(Canvas.hDC, new_pen)
  147.         new_brush = CreateSolidBrush(&H1000000 + 100 + clr)
  148.         old_brush = SelectObject(Canvas.hDC, new_brush)
  149.         status = Polygon(Canvas.hDC, pts(1), 4)
  150.         
  151.         new_pen = SelectObject(Canvas.hDC, old_pen)
  152.         new_brush = SelectObject(Canvas.hDC, old_brush)
  153.         status = DeleteObject(new_pen)
  154.         status = DeleteObject(new_brush)
  155.         
  156.         clr = (clr + 1) Mod 30
  157.     Next theta
  158.     Canvas.Refresh
  159. End Sub
  160. ' ************************************************
  161. ' Draw the circle.
  162. ' ************************************************
  163. Sub DrawCircle()
  164. Const PI = 3.14159
  165. Const Dtheta = PI / 60
  166. Dim r1 As Single
  167. Dim r2 As Single
  168. Dim r3 As Single
  169. Dim xmid As Single
  170. Dim ymid As Single
  171. Dim status As Long
  172. Dim i As Integer
  173. Dim pts1(1 To 4) As POINTAPI
  174. Dim pts2(1 To 4) As POINTAPI
  175. Dim theta As Single
  176. Dim clr As Integer
  177. Dim new_pen As Long
  178. Dim old_pen As Long
  179. Dim new_brush As Long
  180. Dim old_brush As Long
  181.     r1 = Canvas.ScaleWidth * 0.25
  182.     r2 = Canvas.ScaleWidth * 0.35
  183.     r3 = Canvas.ScaleWidth * 0.45
  184.     xmid = Canvas.ScaleWidth / 2
  185.     ymid = Canvas.ScaleHeight / 2
  186.     pts1(3).x = xmid + r2
  187.     pts1(3).y = ymid + 0
  188.     pts1(4).x = xmid + r1
  189.     pts1(4).y = ymid + 0
  190.     pts2(3).x = xmid + r3
  191.     pts2(3).y = ymid + 0
  192.     pts2(4).x = pts1(3).x
  193.     pts2(4).y = pts1(3).y
  194.     For theta = Dtheta To 360 Step Dtheta
  195.         pts1(1).x = pts1(4).x
  196.         pts1(1).y = pts1(4).y
  197.         pts1(2).x = pts1(3).x
  198.         pts1(2).y = pts1(3).y
  199.         pts1(3).x = xmid + r2 * Cos(theta)
  200.         pts1(3).y = ymid + r2 * Sin(theta)
  201.         pts1(4).x = xmid + r1 * Cos(theta)
  202.         pts1(4).y = ymid + r1 * Sin(theta)
  203.         
  204.         pts2(1).x = pts2(4).x
  205.         pts2(1).y = pts2(4).y
  206.         pts2(2).x = pts2(3).x
  207.         pts2(2).y = pts2(3).y
  208.         pts2(3).x = xmid + r3 * Cos(theta)
  209.         pts2(3).y = ymid + r3 * Sin(theta)
  210.         pts2(4).x = pts1(3).x
  211.         pts2(4).y = pts1(3).y
  212.         
  213.         ' Create the pen and brush.
  214.         new_pen = CreatePen(PS_SOLID, 1, &H1000000 + 100 + clr)
  215.         old_pen = SelectObject(Canvas.hDC, new_pen)
  216.         new_brush = CreateSolidBrush(&H1000000 + 100 + clr)
  217.         old_brush = SelectObject(Canvas.hDC, new_brush)
  218.         status = Polygon(Canvas.hDC, pts1(1), 4)
  219.         new_pen = SelectObject(Canvas.hDC, old_pen)
  220.         status = DeleteObject(new_pen)
  221.         new_brush = SelectObject(Canvas.hDC, old_brush)
  222.         status = DeleteObject(new_brush)
  223.         
  224.         new_pen = CreatePen(PS_SOLID, 1, &H1000000 + 100 + 29 - clr)
  225.         old_pen = SelectObject(Canvas.hDC, new_pen)
  226.         new_brush = CreateSolidBrush(&H1000000 + 100 + 29 - clr)
  227.         old_brush = SelectObject(Canvas.hDC, new_brush)
  228.         status = Polygon(Canvas.hDC, pts2(1), 4)
  229.         new_pen = SelectObject(Canvas.hDC, old_pen)
  230.         status = DeleteObject(new_pen)
  231.         new_brush = SelectObject(Canvas.hDC, old_brush)
  232.         status = DeleteObject(new_brush)
  233.         
  234.         clr = (clr + 1) Mod 30
  235.     Next theta
  236.     Canvas.Refresh
  237. End Sub
  238. ' ************************************************
  239. ' Initialize the color component array.
  240. ' ************************************************
  241. Sub InitColors()
  242. Dim i As Integer
  243.     For i = 1 To 16
  244.         Component(i - 1) = i * 16 - 1
  245.     Next i
  246.     For i = 16 To 29
  247.         Component(i) = Component(30 - i)
  248.     Next i
  249. End Sub
  250. ' ************************************************
  251. ' Set the colors values in paltte entries 100
  252. ' through 129 to saturations of blue.
  253. ' ************************************************
  254. Sub SetColors()
  255. Static start_color As Integer
  256. Dim clr As Integer
  257. Dim status As Integer
  258. Dim i As Integer
  259.     clr = start_color
  260.     start_color = (start_color + 1) Mod 30
  261.     For i = 100 To 129
  262.         With palentry(i)
  263.             .peRed = Component(clr)
  264.             .peGreen = Component(clr)
  265.             .peBlue = 255
  266.         End With
  267.         clr = (clr + 1) Mod 30
  268.     Next i
  269.     status = SetPaletteEntries(LogPal, 100, 30, palentry(100))
  270.     status = RealizePalette(Canvas.hDC)
  271. End Sub
  272. Private Sub CmdGo_Click()
  273.     If Running Then
  274.         Running = False
  275.         CmdGo.Caption = "Stopped"
  276.         CmdGo.Enabled = False
  277.     Else
  278.         Running = True
  279.         CmdGo.Caption = "Stop"
  280.         AnimateColors
  281.         Running = False
  282.         CmdGo.Caption = "Go"
  283.         CmdGo.Enabled = True
  284.     End If
  285. End Sub
  286. Private Sub Form_Load()
  287. Const GAP = 3
  288. Dim status As Long
  289. Dim i As Integer
  290.     ' Make sure the screen supports palettes.
  291.     If Not GetDeviceCaps(hDC, RASTERCAPS) And RC_PALETTE Then
  292.         Beep
  293.         MsgBox "This monitor does not support palettes.", _
  294.             vbCritical
  295.         End
  296.     End If
  297.     ' Get system palette size and # static colors.
  298.     SysPalSize = GetDeviceCaps(hDC, SIZEPALETTE)
  299.     NumStaticColors = GetDeviceCaps(hDC, NUMRESERVED)
  300.     StaticColor1 = NumStaticColors \ 2 - 1
  301.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  302.     ' Get the palette entries.
  303.     status = GetSystemPaletteEntries(Canvas.hDC, 0, 256, palentry(0))
  304.     For i = 0 To 255
  305.         palentry(i).peFlags = PC_NOCOLLAPSE
  306.     Next i
  307.     LogPal = Canvas.Picture.hPal
  308.     status = SetPaletteEntries(LogPal, 0, 256, palentry(0))
  309.     ' Place the controls.
  310.     CmdGo.Move (ScaleWidth - CmdGo.Width) / 2, _
  311.         ScaleHeight - CmdGo.Height - GAP
  312.     Canvas.Move 0, 0, ScaleWidth, _
  313.         ScaleHeight - CmdGo.Height - 2 * GAP
  314.     Label1.Move (Canvas.ScaleWidth - Label1.Width) / 2
  315.     Label2.Move (Canvas.ScaleWidth - Label2.Width) / 2
  316.     ' Initialize the colors.
  317.     InitColors
  318.     ' Define the colors.
  319.     SetColors
  320.     ' Set the label colors.
  321.     Label1.ForeColor = &H1000000 + 107
  322.     Label2.ForeColor = &H1000000 + 122
  323.     ' Draw the circle.
  324.     DrawCircle
  325. End Sub
  326. Private Sub Form_Unload(Cancel As Integer)
  327.     End
  328. End Sub
  329. Private Sub mnuFileExit_Click()
  330.     End
  331. End Sub
  332.