home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / SCENES.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  24.4 KB  |  803 lines

  1. VERSION 4.00
  2. Begin VB.Form SceneForm 
  3.    Caption         =   "Scenes"
  4.    ClientHeight    =   3840
  5.    ClientLeft      =   1635
  6.    ClientTop       =   1230
  7.    ClientWidth     =   5400
  8.    Height          =   4530
  9.    Left            =   1575
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   256
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   360
  14.    Top             =   600
  15.    Width           =   5520
  16.    Begin VB.CommandButton CmdCenterWipe 
  17.       Caption         =   "Center Wipe"
  18.       Height          =   495
  19.       Left            =   0
  20.       TabIndex        =   8
  21.       Top             =   1320
  22.       Width           =   1455
  23.    End
  24.    Begin VB.CommandButton CmdHWipe 
  25.       Caption         =   "Horizontal Wipe"
  26.       Height          =   495
  27.       Left            =   0
  28.       TabIndex        =   7
  29.       Top             =   720
  30.       Width           =   1455
  31.    End
  32.    Begin VB.CommandButton CmdSpiralWipe 
  33.       Caption         =   "Spiral Wipe"
  34.       Height          =   495
  35.       Left            =   0
  36.       TabIndex        =   6
  37.       Top             =   1920
  38.       Width           =   1455
  39.    End
  40.    Begin VB.CommandButton CmdVWipe 
  41.       Caption         =   "Vertical Wipe"
  42.       Height          =   495
  43.       Left            =   0
  44.       TabIndex        =   5
  45.       Top             =   120
  46.       Width           =   1455
  47.    End
  48.    Begin VB.CommandButton CmdTileOver 
  49.       Caption         =   "Tile Over"
  50.       Height          =   495
  51.       Left            =   0
  52.       TabIndex        =   4
  53.       Top             =   2520
  54.       Width           =   1455
  55.    End
  56.    Begin VB.CommandButton CmdFade 
  57.       Caption         =   "Fade"
  58.       Height          =   495
  59.       Left            =   0
  60.       TabIndex        =   3
  61.       Top             =   3120
  62.       Width           =   1455
  63.    End
  64.    Begin VB.PictureBox Canvas 
  65.       AutoRedraw      =   -1  'True
  66.       Height          =   3810
  67.       Left            =   1560
  68.       Picture         =   "SCENES.frx":0000
  69.       ScaleHeight     =   250
  70.       ScaleMode       =   3  'Pixel
  71.       ScaleWidth      =   250
  72.       TabIndex        =   2
  73.       Top             =   0
  74.       Width           =   3810
  75.    End
  76.    Begin VB.PictureBox Pict 
  77.       AutoRedraw      =   -1  'True
  78.       AutoSize        =   -1  'True
  79.       Height          =   3810
  80.       Index           =   1
  81.       Left            =   120
  82.       Picture         =   "SCENES.frx":FA5A
  83.       ScaleHeight     =   250
  84.       ScaleMode       =   3  'Pixel
  85.       ScaleWidth      =   250
  86.       TabIndex        =   1
  87.       Top             =   3840
  88.       Visible         =   0   'False
  89.       Width           =   3810
  90.    End
  91.    Begin VB.PictureBox Pict 
  92.       AutoRedraw      =   -1  'True
  93.       AutoSize        =   -1  'True
  94.       Height          =   3810
  95.       Index           =   0
  96.       Left            =   0
  97.       Picture         =   "SCENES.frx":1F4B4
  98.       ScaleHeight     =   250
  99.       ScaleMode       =   3  'Pixel
  100.       ScaleWidth      =   250
  101.       TabIndex        =   0
  102.       Top             =   3720
  103.       Visible         =   0   'False
  104.       Width           =   3810
  105.    End
  106.    Begin VB.Menu mnuFile 
  107.       Caption         =   "&File"
  108.       Begin VB.Menu mnuFileExit 
  109.          Caption         =   "E&xit"
  110.       End
  111.    End
  112. Attribute VB_Name = "SceneForm"
  113. Attribute VB_Creatable = False
  114. Attribute VB_Exposed = False
  115. Option Explicit
  116. Dim SysPalSize As Integer
  117. Dim NumStaticColors As Integer
  118. Dim StaticColor1 As Integer
  119. Dim StaticColor2 As Integer
  120. Dim ActiveImage As Integer
  121. ' ***********************************************
  122. ' Give the form and all the picture boxes an
  123. ' hourglass cursor.
  124. ' ***********************************************
  125. Sub WaitStart()
  126.     MousePointer = vbHourglass
  127.     Canvas.MousePointer = vbHourglass
  128.     DoEvents
  129. End Sub
  130. ' ***********************************************
  131. ' Restore the mouse pointers for the form and all
  132. ' the picture boxes.
  133. ' ***********************************************
  134. Sub WaitEnd()
  135.     MousePointer = vbDefault
  136.     Canvas.MousePointer = vbDefault
  137. End Sub
  138. ' ************************************************
  139. ' Wipe tpic over fpic from the inside out.
  140. ' ************************************************
  141. Sub CenterWipe(fpic As Control, tpic As Control)
  142. Dim bm As BITMAP
  143. Dim hbm As Integer
  144. Dim wid As Long
  145. Dim hgt As Long
  146. Dim fbytes() As Byte
  147. Dim tbytes() As Byte
  148. Dim status As Long
  149. Dim dx As Integer
  150. Dim dy As Integer
  151. Dim xmin As Integer
  152. Dim ymin As Integer
  153. Dim xmid As Integer
  154. Dim ymid As Integer
  155. Dim xmax As Integer
  156. Dim ymax As Integer
  157. Dim next_time As Long
  158. Dim dt As Long
  159. Dim i As Integer
  160. Dim j As Integer
  161. Dim piece As Integer
  162.     ' Get the new image's pixels.
  163.     hbm = tpic.Image
  164.     status = GetObject(hbm, BITMAP_SIZE, bm)
  165.     wid = bm.bmWidthBytes
  166.     hgt = bm.bmHeight
  167.     ReDim tbytes(1 To wid, 1 To hgt)
  168.     status = GetBitmapBits(hbm, wid * hgt, tbytes(1, 1))
  169.     ' Get the old image's pixels.
  170.     hbm = fpic.Image
  171.     status = GetObject(hbm, BITMAP_SIZE, bm)
  172.     wid = bm.bmWidthBytes
  173.     hgt = bm.bmHeight
  174.     ReDim fbytes(1 To wid, 1 To hgt)
  175.     status = GetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  176.     ' Display the new image in 20 increments.
  177.     xmid = (wid + 1) / 2
  178.     ymid = (hgt + 1) / 2
  179.     dx = CInt(wid / 40 + 1)
  180.     dy = CInt(hgt / 40 + 1)
  181.     dt = 1000 \ 20
  182.     next_time = GetTickCount()
  183.     For piece = 1 To 20
  184.         xmin = xmid - piece * dx
  185.         If xmin < 1 Then xmin = 1
  186.         xmax = xmid + piece * dx
  187.         If xmax > wid Then xmax = wid
  188.         
  189.         ymin = ymid - piece * dy
  190.         If ymin < 1 Then ymin = 1
  191.         ymax = ymid + piece * dy
  192.         If ymax > hgt Then ymax = hgt
  193.         
  194.         For i = xmin To xmax
  195.             For j = ymin To ymin + dy
  196.                 fbytes(i, j) = tbytes(i, j)
  197.             Next j
  198.             For j = ymax - dy To ymax
  199.                 fbytes(i, j) = tbytes(i, j)
  200.             Next j
  201.         Next i
  202.         For j = ymin + dx To ymax - dx
  203.             For i = xmin To xmin + dx
  204.                 fbytes(i, j) = tbytes(i, j)
  205.             Next i
  206.             For i = xmax - dx To xmax
  207.                 fbytes(i, j) = tbytes(i, j)
  208.             Next i
  209.         Next j
  210.         status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  211.         fpic.Refresh
  212.         
  213.         next_time = next_time + dt
  214.         WaitTill next_time
  215.     Next piece
  216. End Sub
  217. ' ************************************************
  218. ' Copy tpic over fpic in a spiral pattern.
  219. ' ************************************************
  220. Sub SpiralWipe(fpic As Control, tpic As Control)
  221. Const PER_SIDE = 7      ' # pieces per side.
  222. Const CHUNKS = PER_SIDE * PER_SIDE
  223. Dim bm As BITMAP
  224. Dim hbm As Integer
  225. Dim wid As Long
  226. Dim hgt As Long
  227. Dim fbytes() As Byte
  228. Dim tbytes() As Byte
  229. Dim status As Long
  230. Dim i As Integer
  231. Dim j As Integer
  232. Dim row As Integer
  233. Dim col As Integer
  234. Dim dx As Integer
  235. Dim dy As Integer
  236. Dim xmin As Integer
  237. Dim ymin As Integer
  238. Dim xmax As Integer
  239. Dim ymax As Integer
  240. Dim rmin As Integer
  241. Dim cmin As Integer
  242. Dim rmax As Integer
  243. Dim cmax As Integer
  244. Dim next_time As Long
  245. Dim dt As Long
  246.     ' Get the new image's pixels.
  247.     hbm = tpic.Image
  248.     status = GetObject(hbm, BITMAP_SIZE, bm)
  249.     wid = bm.bmWidthBytes
  250.     hgt = bm.bmHeight
  251.     ReDim tbytes(1 To wid, 1 To hgt)
  252.     status = GetBitmapBits(hbm, wid * hgt, tbytes(1, 1))
  253.     ' Get the old image's pixels.
  254.     hbm = fpic.Image
  255.     status = GetObject(hbm, BITMAP_SIZE, bm)
  256.     wid = bm.bmWidthBytes
  257.     hgt = bm.bmHeight
  258.     ReDim fbytes(1 To wid, 1 To hgt)
  259.     status = GetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  260.     ' Display the pieces of tpic.
  261.     dx = CInt(wid / PER_SIDE + 1)
  262.     dy = CInt(hgt / PER_SIDE + 1)
  263.     dt = 1000 \ CHUNKS
  264.     next_time = GetTickCount()
  265.     rmin = 0
  266.     cmin = 0
  267.     rmax = PER_SIDE - 1
  268.     cmax = PER_SIDE - 1
  269.     Do
  270.         ' Display the top row.
  271.         For col = cmin To cmax
  272.             xmin = col * dx + 1
  273.             ymin = rmin * dy + 1
  274.             xmax = xmin + dx - 1
  275.             If xmax > wid Then xmax = wid
  276.             ymax = ymin + dy - 1
  277.             If ymax > hgt Then ymax = hgt
  278.             For i = xmin To xmax
  279.                 For j = ymin To ymax
  280.                     fbytes(i, j) = tbytes(i, j)
  281.                 Next j
  282.             Next i
  283.             status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  284.             fpic.Refresh
  285.             next_time = next_time + dt
  286.             WaitTill next_time
  287.         Next col
  288.         rmin = rmin + 1
  289.         If rmin > rmax Then Exit Do
  290.         
  291.         ' Display the right column.
  292.         For row = rmin To rmax
  293.             xmin = cmax * dx + 1
  294.             ymin = row * dy + 1
  295.             xmax = xmin + dx - 1
  296.             If xmax > wid Then xmax = wid
  297.             ymax = ymin + dy - 1
  298.             If ymax > hgt Then ymax = hgt
  299.             For i = xmin To xmax
  300.                 For j = ymin To ymax
  301.                     fbytes(i, j) = tbytes(i, j)
  302.                 Next j
  303.             Next i
  304.             status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  305.             fpic.Refresh
  306.             next_time = next_time + dt
  307.             WaitTill next_time
  308.         Next row
  309.         cmax = cmax - 1
  310.         If cmax < cmin Then Exit Do
  311.         
  312.         ' Display the bottom row.
  313.         For col = cmax To cmin Step -1
  314.             xmin = col * dx + 1
  315.             ymin = rmax * dy + 1
  316.             xmax = xmin + dx - 1
  317.             If xmax > wid Then xmax = wid
  318.             ymax = ymin + dy - 1
  319.             If ymax > hgt Then ymax = hgt
  320.             For i = xmin To xmax
  321.                 For j = ymin To ymax
  322.                     fbytes(i, j) = tbytes(i, j)
  323.                 Next j
  324.             Next i
  325.             status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  326.             fpic.Refresh
  327.             next_time = next_time + dt
  328.             WaitTill next_time
  329.         Next col
  330.         rmax = rmax - 1
  331.         If rmin > rmax Then Exit Do
  332.         
  333.         ' Display the left column.
  334.         For row = rmax To rmin Step -1
  335.             xmin = cmin * dx + 1
  336.             ymin = row * dy + 1
  337.             xmax = xmin + dx - 1
  338.             If xmax > wid Then xmax = wid
  339.             ymax = ymin + dy - 1
  340.             If ymax > hgt Then ymax = hgt
  341.             For i = xmin To xmax
  342.                 For j = ymin To ymax
  343.                     fbytes(i, j) = tbytes(i, j)
  344.                 Next j
  345.             Next i
  346.             status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  347.             fpic.Refresh
  348.             next_time = next_time + dt
  349.             WaitTill next_time
  350.         Next row
  351.         cmin = cmin + 1
  352.         If cmax < cmin Then Exit Do
  353.     Loop
  354. End Sub
  355. ' ************************************************
  356. ' Fade fpic out and then fade tpic in.
  357. ' ************************************************
  358. Sub Fade(fpic As Control, tpic As Control)
  359. Dim bm As BITMAP
  360. Dim hbm As Integer
  361. Dim wid As Long
  362. Dim hgt As Long
  363. Dim bytes() As Byte
  364. Dim status As Long
  365. Dim hpal As Integer
  366. Dim pal(0 To 255) As PALETTEENTRY
  367. Dim newpal(0 To 255) As PALETTEENTRY
  368. Dim level As Single
  369. Dim next_time As Long
  370. Dim num_entries As Integer
  371. Dim i As Integer
  372.     ' Flag all palette entries NOCOLLAPSE.
  373.     For i = 0 To 255
  374.         newpal(i).peFlags = PC_NOCOLLAPSE
  375.     Next i
  376.     ' Get fpic's logical palette.
  377.     fpic.ZOrder
  378.     DoEvents
  379.     hpal = fpic.Picture.hpal
  380.     num_entries = GetPaletteEntries(hpal, 0, 256, pal(0))
  381.     ' Fade out using 20 intensity levels in about
  382.     ' 1 second.
  383.     For level = 0.95 To 0# Step -0.05
  384.         For i = StaticColor1 + 1 To StaticColor2 - 1
  385.             With newpal(i)
  386.                 .peRed = pal(i).peRed * level
  387.                 .peGreen = pal(i).peGreen * level
  388.                 .peBlue = pal(i).peBlue * level
  389.             End With
  390.         Next i
  391.         status = SetPaletteEntries(hpal, StaticColor1 + 1, StaticColor2 - StaticColor1 + 1, newpal(StaticColor1 + 1))
  392.         status = RealizePalette(fpic.hdc)
  393.         fpic.Refresh
  394.         next_time = next_time + 50
  395.         WaitTill next_time
  396.     Next level
  397.     ' Get the new image's pixels.
  398.     hbm = tpic.Image
  399.     status = GetObject(hbm, BITMAP_SIZE, bm)
  400.     wid = bm.bmWidthBytes
  401.     hgt = bm.bmHeight
  402.     ReDim bytes(1 To wid, 1 To hgt)
  403.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  404.     ' Assign the new pixels to fpic.
  405.     status = SetBitmapBits(fpic.Image, wid * hgt, bytes(1, 1))
  406.     fpic.Refresh
  407.     ' Get tpic's logical palette.
  408.     num_entries = GetPaletteEntries(tpic.Picture.hpal, 0, 256, pal(0))
  409.     ' Fade in using 20 intensity levels in about
  410.     ' 1 second.
  411.     For level = 0.05 To 1# Step 0.05
  412.         For i = 0 To num_entries - 1
  413.             With newpal(i)
  414.                 .peRed = pal(i).peRed * level
  415.                 .peGreen = pal(i).peGreen * level
  416.                 .peBlue = pal(i).peBlue * level
  417.             End With
  418.         Next i
  419.         status = SetPaletteEntries(hpal, StaticColor1 + 1, StaticColor2 - StaticColor1 + 1, newpal(StaticColor1 + 1))
  420.         status = RealizePalette(fpic.hdc)
  421.         fpic.Refresh
  422.         next_time = next_time + 50
  423.         WaitTill next_time
  424.     Next level
  425. End Sub
  426. ' ************************************************
  427. ' Wipe tpic onto fpic vertically.
  428. ' ************************************************
  429. Sub VWipe(fpic As Control, tpic As Control)
  430. Dim bm As BITMAP
  431. Dim hbm As Integer
  432. Dim wid As Long
  433. Dim hgt As Long
  434. Dim fbytes() As Byte
  435. Dim tbytes() As Byte
  436. Dim status As Long
  437. Dim i As Integer
  438. Dim j As Integer
  439. Dim x As Integer
  440. Dim y As Integer
  441. Dim dy As Integer
  442. Dim lasty As Integer
  443. Dim next_time As Long
  444.     ' Get the new image's pixels.
  445.     hbm = tpic.Image
  446.     status = GetObject(hbm, BITMAP_SIZE, bm)
  447.     wid = bm.bmWidthBytes
  448.     hgt = bm.bmHeight
  449.     ReDim tbytes(1 To wid, 1 To hgt)
  450.     status = GetBitmapBits(hbm, wid * hgt, tbytes(1, 1))
  451.     ' Get the old image's pixels.
  452.     hbm = fpic.Image
  453.     status = GetObject(hbm, BITMAP_SIZE, bm)
  454.     wid = bm.bmWidthBytes
  455.     hgt = bm.bmHeight
  456.     ReDim fbytes(1 To wid, 1 To hgt)
  457.     status = GetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  458.     ' Use 20 images in about 1 second.
  459.     dy = CInt(hgt / 20 + 1)
  460.     next_time = GetTickCount()
  461.     For y = 1 To hgt Step dy
  462.         lasty = y + dy - 1
  463.         If lasty > hgt Then lasty = hgt
  464.         For j = y To lasty
  465.             For i = 1 To wid
  466.                 fbytes(i, j) = tbytes(i, j)
  467.             Next i
  468.         Next j
  469.         
  470.         status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  471.         fpic.Refresh
  472.         
  473.         next_time = next_time + 50
  474.         WaitTill next_time
  475.     Next y
  476. End Sub
  477. ' ************************************************
  478. ' Wipe tpic onto fpic horizontally.
  479. ' ************************************************
  480. Sub HWipe(fpic As Control, tpic As Control)
  481. Dim bm As BITMAP
  482. Dim hbm As Integer
  483. Dim wid As Long
  484. Dim hgt As Long
  485. Dim fbytes() As Byte
  486. Dim tbytes() As Byte
  487. Dim status As Long
  488. Dim i As Integer
  489. Dim j As Integer
  490. Dim x As Integer
  491. Dim y As Integer
  492. Dim dx As Integer
  493. Dim lastx As Integer
  494. Dim next_time As Long
  495.     ' Get the new image's pixels.
  496.     hbm = tpic.Image
  497.     status = GetObject(hbm, BITMAP_SIZE, bm)
  498.     wid = bm.bmWidthBytes
  499.     hgt = bm.bmHeight
  500.     ReDim tbytes(1 To wid, 1 To hgt)
  501.     status = GetBitmapBits(hbm, wid * hgt, tbytes(1, 1))
  502.     ' Get the old image's pixels.
  503.     hbm = fpic.Image
  504.     status = GetObject(hbm, BITMAP_SIZE, bm)
  505.     wid = bm.bmWidthBytes
  506.     hgt = bm.bmHeight
  507.     ReDim fbytes(1 To wid, 1 To hgt)
  508.     status = GetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  509.     ' Use 20 images in about 1 second.
  510.     dx = CInt(wid / 20 + 1)
  511.     next_time = GetTickCount()
  512.     For x = 1 To wid Step dx
  513.         lastx = x + dx - 1
  514.         If lastx > wid Then lastx = wid
  515.         For i = x To lastx
  516.             For j = 1 To hgt
  517.                 fbytes(i, j) = tbytes(i, j)
  518.             Next j
  519.         Next i
  520.         
  521.         status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  522.         fpic.Refresh
  523.         
  524.         next_time = next_time + 50
  525.         WaitTill next_time
  526.     Next x
  527. End Sub
  528. ' ************************************************
  529. ' Dissolve tpic over fpic in random chunks.
  530. ' ************************************************
  531. Private Sub CmdTileOver_Click()
  532.     ActiveImage = 1 - ActiveImage
  533.     WaitStart
  534.     TileOver Canvas, Pict(ActiveImage)
  535.     WaitEnd
  536. End Sub
  537. ' ************************************************
  538. ' Perform a vertical wipe.
  539. ' ************************************************
  540. Private Sub CmdVWipe_Click()
  541.     ActiveImage = 1 - ActiveImage
  542.     WaitStart
  543.     VWipe Canvas, Pict(ActiveImage)
  544.     WaitEnd
  545. End Sub
  546. ' ************************************************
  547. ' Expand the new image from the center out.
  548. ' ************************************************
  549. Private Sub CmdCenterWipe_Click()
  550.     ActiveImage = 1 - ActiveImage
  551.     WaitStart
  552.     CenterWipe Canvas, Pict(ActiveImage)
  553.     WaitEnd
  554. End Sub
  555. ' ************************************************
  556. ' Drop the new picture over the old in a spiral
  557. ' chunk pattern.
  558. ' ************************************************
  559. Private Sub CmdSpiralWipe_Click()
  560.     ActiveImage = 1 - ActiveImage
  561.     WaitStart
  562.     SpiralWipe Canvas, Pict(ActiveImage)
  563.     WaitEnd
  564. End Sub
  565. ' ************************************************
  566. ' Fade one image out and the other in.
  567. ' ************************************************
  568. Private Sub CmdFade_Click()
  569.     ActiveImage = 1 - ActiveImage
  570.     WaitStart
  571.     Fade Canvas, Pict(ActiveImage)
  572.     WaitEnd
  573. End Sub
  574. ' ************************************************
  575. ' Tile tpic over fpic in random chunks.
  576. ' ************************************************
  577. Sub TileOver(fpic As Control, tpic As Control)
  578. Const PER_SIDE = 7      ' # chunks per side.
  579. Const CHUNKS = PER_SIDE * PER_SIDE
  580. Dim bm As BITMAP
  581. Dim hbm As Integer
  582. Dim wid As Long
  583. Dim hgt As Long
  584. Dim fbytes() As Byte
  585. Dim tbytes() As Byte
  586. Dim status As Long
  587. Dim i As Integer
  588. Dim j As Integer
  589. Dim row As Integer
  590. Dim col As Integer
  591. Dim piece As Integer
  592. Dim next_piece(0 To CHUNKS - 1) As Integer
  593. Dim remaining As Integer
  594. Dim dx As Integer
  595. Dim dy As Integer
  596. Dim xmin As Integer
  597. Dim ymin As Integer
  598. Dim xmax As Integer
  599. Dim ymax As Integer
  600. Dim next_time As Long
  601. Dim dt As Long
  602.     ' Get the new image's pixels.
  603.     hbm = tpic.Image
  604.     status = GetObject(hbm, BITMAP_SIZE, bm)
  605.     wid = bm.bmWidthBytes
  606.     hgt = bm.bmHeight
  607.     ReDim tbytes(1 To wid, 1 To hgt)
  608.     status = GetBitmapBits(hbm, wid * hgt, tbytes(1, 1))
  609.     ' Get the old image's pixels.
  610.     hbm = fpic.Image
  611.     status = GetObject(hbm, BITMAP_SIZE, bm)
  612.     wid = bm.bmWidthBytes
  613.     hgt = bm.bmHeight
  614.     ReDim fbytes(1 To wid, 1 To hgt)
  615.     status = GetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  616.     ' Initialize the list of pieces.
  617.     For piece = 0 To CHUNKS - 1
  618.         next_piece(piece) = piece
  619.     Next piece
  620.     ' Display randomly selected pieces of tpic.
  621.     dx = CInt(wid / PER_SIDE + 1)
  622.     dy = CInt(hgt / PER_SIDE + 1)
  623.     dt = 1000 \ CHUNKS
  624.     next_time = GetTickCount()
  625.     For remaining = CHUNKS To 1 Step -1
  626.         ' Select a random piece to display.
  627.         piece = Int((remaining - 1) * Rnd)
  628.         
  629.         ' Display the piece.
  630.         row = next_piece(piece) \ PER_SIDE
  631.         col = next_piece(piece) Mod PER_SIDE
  632.         xmin = col * dx + 1
  633.         ymin = row * dy + 1
  634.         xmax = xmin + dx - 1
  635.         If xmax > wid Then xmax = wid
  636.         ymax = ymin + dy - 1
  637.         If ymax > hgt Then ymax = hgt
  638.         For i = xmin To xmax
  639.             For j = ymin To ymax
  640.                 fbytes(i, j) = tbytes(i, j)
  641.             Next j
  642.         Next i
  643.         status = SetBitmapBits(hbm, wid * hgt, fbytes(1, 1))
  644.         fpic.Refresh
  645.         
  646.         ' Remove the piece from the piece list.
  647.         For i = piece + 1 To remaining - 1
  648.             next_piece(i - 1) = next_piece(i)
  649.         Next i
  650.         
  651.         next_time = next_time + dt
  652.         WaitTill next_time
  653.     Next remaining
  654. End Sub
  655. ' ************************************************
  656. ' Perform a horizontal wipe.
  657. ' ************************************************
  658. Private Sub CmdHWipe_Click()
  659.     ActiveImage = 1 - ActiveImage
  660.     WaitStart
  661.     HWipe Canvas, Pict(ActiveImage)
  662.     WaitEnd
  663. End Sub
  664. Private Sub Form_Load()
  665.     Randomize
  666.     ' Make sure the screen supports palettes.
  667.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  668.         Beep
  669.         MsgBox "This monitor does not support palettes.", _
  670.             vbCritical
  671.         End
  672.     End If
  673.     ' Get system palette size and # static colors.
  674.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  675.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  676.     StaticColor1 = NumStaticColors \ 2 - 1
  677.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  678.     ' Get the bitmaps' bits.
  679.     Me.Show
  680.     WaitStart
  681.     MatchGrayPalette Canvas
  682.     MatchGrayPalette Pict(0)
  683.     MatchGrayPalette Pict(1)
  684.     Canvas.ZOrder
  685.     DoEvents
  686.     Pict(0).ZOrder
  687.     DoEvents
  688.     Pict(1).ZOrder
  689.     DoEvents
  690.     WaitEnd
  691. End Sub
  692. ' ************************************************
  693. ' Return the index of the nonstatic gray closest
  694. ' to the given value (assuming the non-static
  695. ' colors are a gray scale created by
  696. ' MatchGrayPalette).
  697. ' ************************************************
  698. Function NearestNonstaticGray(c As Integer) As Integer
  699. Dim dgray As Single
  700.     If c < 0 Then
  701.         c = 0
  702.     ElseIf c > 255 Then
  703.         c = 255
  704.     End If
  705.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  706.     NearestNonstaticGray = c / dgray + StaticColor1 + 1
  707. End Function
  708. ' ***********************************************
  709. ' Load the control's palette so the non-static
  710. ' colors are grays. Map the logical palette to
  711. ' match the system palette. Convert the image to
  712. ' use the non-static grays.
  713. ' ***********************************************
  714. Sub MatchGrayPalette(pic As Control)
  715. Dim logpal As Integer
  716. Dim sys(0 To 255) As PALETTEENTRY
  717. Dim palentry(0 To 255) As PALETTEENTRY
  718. Dim i As Integer
  719. Dim bm As BITMAP
  720. Dim hbm As Integer
  721. Dim status As Long
  722. Dim x As Integer
  723. Dim y As Integer
  724. Dim gray As Single
  725. Dim dgray As Single
  726. Dim c As Integer
  727. Dim clr As Integer
  728. Dim wid As Long
  729. Dim hgt As Long
  730. Dim bytes() As Byte
  731.     ' Make sure pic has the foreground palette.
  732.     pic.ZOrder
  733.     i = RealizePalette(pic.hdc)
  734.     DoEvents
  735.     ' Get the system palette entries.
  736.     i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
  737.         
  738.     ' Get the image pixels.
  739.     hbm = pic.Image
  740.     status = GetObject(hbm, BITMAP_SIZE, bm)
  741.     wid = bm.bmWidthBytes
  742.     hgt = bm.bmHeight
  743.     ReDim bytes(1 To wid, 1 To hgt)
  744.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  745.     ' Make the logical palette as big as possible.
  746.     logpal = pic.Picture.hpal
  747.     If ResizePalette(logpal, SysPalSize) = 0 Then
  748.         Beep
  749.         MsgBox "Error resizing logical palette.", _
  750.             vbExclamation
  751.         Exit Sub
  752.     End If
  753.     ' Blank the non-static colors.
  754.     For i = 0 To StaticColor1
  755.         palentry(i) = sys(i)
  756.     Next i
  757.     For i = StaticColor1 + 1 To StaticColor2 - 1
  758.         With palentry(i)
  759.             .peRed = 0
  760.             .peGreen = 0
  761.             .peBlue = 0
  762.             .peFlags = PC_NOCOLLAPSE
  763.         End With
  764.     Next i
  765.     For i = StaticColor2 To 255
  766.         palentry(i) = sys(i)
  767.     Next i
  768.     i = SetPaletteEntries(logpal, 0, SysPalSize, palentry(0))
  769.     ' Insert the non-static grays.
  770.     gray = 0
  771.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  772.     For i = StaticColor1 + 1 To StaticColor2 - 1
  773.         c = gray
  774.         gray = gray + dgray
  775.         With palentry(i)
  776.             .peRed = c
  777.             .peGreen = c
  778.             .peBlue = c
  779.         End With
  780.     Next i
  781.     i = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  782.     ' Recreate the image using the new colors.
  783.     For y = 1 To hgt
  784.         For x = 1 To wid
  785.             clr = bytes(x, y)
  786.             With sys(clr)
  787.                 c = (CInt(.peRed) + .peGreen + .peBlue) / 3
  788.             End With
  789.             bytes(x, y) = NearestNonstaticGray(c)
  790.         Next x
  791.     Next y
  792.     status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  793.     ' Realize the gray palette.
  794.     i = RealizePalette(pic.hdc)
  795.     pic.Refresh
  796. End Sub
  797. Private Sub Form_Unload(Cancel As Integer)
  798.     End
  799. End Sub
  800. Private Sub mnuFileExit_Click()
  801.     Unload Me
  802. End Sub
  803.