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

  1. /*----------------------------------------
  2.    SYSPAL2.C -- Displays system palette
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. TCHAR szAppName [] = TEXT ("SysPal2") ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      HWND     hwnd ;
  16.      MSG      msg ;
  17.      WNDCLASS wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("System Palette #2"), 
  38.                           WS_OVERLAPPEDWINDOW, 
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      if (!hwnd)
  44.           return 0 ;
  45.  
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.  
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.      {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.      }
  54.      return msg.wParam ;
  55. }
  56.  
  57. BOOL CheckDisplay (HWND hwnd)
  58. {
  59.      HDC hdc ;
  60.      int iPalSize ;
  61.  
  62.      hdc = GetDC (hwnd) ;
  63.      iPalSize = GetDeviceCaps (hdc, SIZEPALETTE) ;
  64.      ReleaseDC (hwnd, hdc) ;
  65.  
  66.      if (iPalSize != 256)
  67.      {
  68.           MessageBox (hwnd, TEXT ("This program requires that the video ")
  69.                             TEXT ("display mode have a 256-color palette."),
  70.                       szAppName, MB_ICONERROR) ;
  71.           return FALSE ;
  72.      }
  73.      return TRUE ;
  74. }
  75.  
  76. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  77. {
  78.      static HPALETTE hPalette ;
  79.      static int      cxClient, cyClient ;
  80.      HBRUSH          hBrush ;
  81.      HDC             hdc ;
  82.      int             i, x, y ;
  83.      LOGPALETTE    * plp ;
  84.      PAINTSTRUCT     ps ;
  85.      RECT            rect ;
  86.  
  87.      switch (message)
  88.      {
  89.      case WM_CREATE:
  90.           if (!CheckDisplay (hwnd))
  91.                return -1 ;
  92.  
  93.           plp = malloc (sizeof (LOGPALETTE) + 255 * sizeof (PALETTEENTRY)) ;
  94.  
  95.           plp->palVersion    = 0x0300 ;
  96.           plp->palNumEntries = 256 ;
  97.  
  98.           for (i = 0 ; i < 256 ; i++)
  99.           {
  100.                plp->palPalEntry[i].peRed   = i ;
  101.                plp->palPalEntry[i].peGreen = 0 ;
  102.                plp->palPalEntry[i].peBlue  = 0 ;
  103.                plp->palPalEntry[i].peFlags = PC_EXPLICIT ;
  104.           }
  105.           
  106.           hPalette = CreatePalette (plp) ;
  107.           free (plp) ;          
  108.           return 0 ;
  109.      
  110.      case WM_DISPLAYCHANGE:
  111.           if (!CheckDisplay (hwnd))
  112.                DestroyWindow (hwnd) ;
  113.  
  114.           return 0 ;
  115.  
  116.      case WM_SIZE:
  117.           cxClient = LOWORD (lParam) ;
  118.           cyClient = HIWORD (lParam) ;
  119.           return 0 ;
  120.  
  121.      case WM_PAINT:
  122.           hdc = BeginPaint (hwnd, &ps) ;
  123.  
  124.           SelectPalette (hdc, hPalette, FALSE) ;
  125.           RealizePalette (hdc) ;
  126.  
  127.           for (y = 0 ; y < 16 ; y++)
  128.           for (x = 0 ; x < 16 ; x++)
  129.           {
  130.                hBrush = CreateSolidBrush (PALETTEINDEX (16 * y + x)) ;
  131.                SetRect (&rect, x      * cxClient / 16,  y      * cyClient / 16,
  132.                               (x + 1) * cxClient / 16, (y + 1) * cyClient / 16);
  133.                FillRect (hdc, &rect, hBrush) ;
  134.                DeleteObject (hBrush) ;
  135.           }
  136.           EndPaint (hwnd, &ps) ;
  137.           return 0 ;
  138.  
  139.      case WM_PALETTECHANGED:
  140.           if ((HWND) wParam != hwnd)
  141.                InvalidateRect (hwnd, NULL, FALSE) ;
  142.  
  143.           return 0 ;
  144.  
  145.      case WM_DESTROY:
  146.           DeleteObject (hPalette) ;
  147.           PostQuitMessage (0) ;
  148.           return 0 ;
  149.      }
  150.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  151. }
  152.