home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / AllColor / AllColor.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.0 KB  |  116 lines

  1. /*-----------------------------------------
  2.    ALLCOLOR.C -- Palette Animation Demo
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER    1
  9.  
  10. TCHAR szAppName [] = TEXT ("AllColor") ;
  11. TCHAR szTitle   [] = TEXT ("AllColor: Palette Animation Demo") ;
  12.  
  13. static int          iIncr ;
  14. static PALETTEENTRY pe ;
  15.  
  16. HPALETTE CreateRoutine (HWND hwnd)
  17. {
  18.      HDC        hdc ;
  19.      HPALETTE   hPalette ;
  20.      LOGPALETTE lp ;
  21.  
  22.           // Determine the color resolution and set iIncr
  23.  
  24.      hdc = GetDC (hwnd) ;
  25.      iIncr = 1 << (8 - GetDeviceCaps (hdc, COLORRES) / 3) ;
  26.      ReleaseDC (hwnd, hdc) ;
  27.  
  28.           // Create the logical palette
  29.      
  30.      lp.palVersion             = 0x0300 ;
  31.      lp.palNumEntries          = 1 ;
  32.      lp.palPalEntry[0].peRed   = 0 ;
  33.      lp.palPalEntry[0].peGreen = 0 ;
  34.      lp.palPalEntry[0].peBlue  = 0 ;
  35.      lp.palPalEntry[0].peFlags = PC_RESERVED ;
  36.    
  37.      hPalette = CreatePalette (&lp) ;
  38.  
  39.           // Save global for less typing
  40.  
  41.      pe = lp.palPalEntry[0] ;
  42.      
  43.      SetTimer (hwnd, ID_TIMER, 10, NULL) ;
  44.      return hPalette ;
  45. }
  46.  
  47. void DisplayRGB (HDC hdc, PALETTEENTRY * ppe)
  48. {
  49.      TCHAR szBuffer [16] ;
  50.  
  51.      wsprintf (szBuffer, TEXT (" %02X-%02X-%02X "),
  52.                ppe->peRed, ppe->peGreen, ppe->peBlue) ;
  53.  
  54.      TextOut (hdc, 0, 0, szBuffer, lstrlen (szBuffer)) ;
  55. }
  56.  
  57. void PaintRoutine (HDC hdc, int cxClient, int cyClient)
  58. {
  59.      HBRUSH   hBrush ;
  60.      RECT     rect ;
  61.  
  62.           // Draw Palette Index 0 on entire window
  63.  
  64.      hBrush = CreateSolidBrush (PALETTEINDEX (0)) ;
  65.      SetRect (&rect, 0, 0, cxClient, cyClient) ;
  66.      FillRect (hdc, &rect, hBrush) ;
  67.      DeleteObject (SelectObject (hdc, GetStockObject (WHITE_BRUSH))) ;
  68.  
  69.           // Display the RGB value
  70.  
  71.      DisplayRGB (hdc, &pe) ;
  72.      return ;
  73. }
  74.  
  75. void TimerRoutine (HDC hdc, HPALETTE hPalette)
  76. {
  77.      static BOOL  bRedUp = TRUE, bGreenUp = TRUE, bBlueUp = TRUE ;
  78.  
  79.           // Define new color value
  80.  
  81.      pe.peBlue += (bBlueUp ? iIncr : -iIncr) ;
  82.  
  83.      if (pe.peBlue == (BYTE) (bBlueUp ? 0 : 256 - iIncr))
  84.      {
  85.           pe.peBlue = (bBlueUp ? 256 - iIncr : 0) ;
  86.           bBlueUp ^= TRUE ;
  87.           pe.peGreen += (bGreenUp ? iIncr : -iIncr) ;
  88.  
  89.           if (pe.peGreen == (BYTE) (bGreenUp ? 0 : 256 - iIncr))
  90.           {
  91.                pe.peGreen = (bGreenUp ? 256 - iIncr : 0) ;
  92.                bGreenUp ^= TRUE ;
  93.                pe.peRed += (bRedUp ? iIncr : -iIncr) ;
  94.  
  95.                if (pe.peRed == (BYTE) (bRedUp ? 0 : 256 - iIncr))
  96.                {
  97.                     pe.peRed = (bRedUp ? 256 - iIncr : 0) ;
  98.                     bRedUp ^= TRUE ;
  99.                }
  100.           }
  101.      }
  102.  
  103.           // Animate the palette
  104.      
  105.      AnimatePalette (hPalette, 0, 1, &pe) ;
  106.      DisplayRGB (hdc, &pe) ;
  107.      return ;
  108. }
  109.  
  110. void DestroyRoutine (HWND hwnd, HPALETTE hPalette)
  111. {
  112.      KillTimer (hwnd, ID_TIMER) ;
  113.      DeleteObject (hPalette) ;
  114.      return ;
  115. }
  116.