home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap10 / about2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  6.1 KB  |  185 lines

  1. /*------------------------------------------
  2.    ABOUT2.C -- About Box Demo Program No. 2
  3.                (c) Charles Petzold, 1990
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "about2.h"
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  10.  
  11. short nCurrentColor  = IDD_BLACK,
  12.       nCurrentFigure = IDD_RECT ;
  13.  
  14. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  15.                     LPSTR lpszCmdLine, int nCmdShow)
  16.      {
  17.      static char szAppName [] = "About2" ;
  18.      MSG         msg;
  19.      HWND        hwnd ;
  20.      WNDCLASS    wndclass ;
  21.  
  22.      if (!hPrevInstance) 
  23.           {
  24.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  25.           wndclass.lpfnWndProc   = WndProc ;
  26.           wndclass.cbClsExtra    = 0 ;
  27.           wndclass.cbWndExtra    = 0 ;
  28.           wndclass.hInstance     = hInstance ;
  29.           wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  30.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  31.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  32.           wndclass.lpszMenuName  = szAppName ;
  33.           wndclass.lpszClassName = szAppName ;
  34.  
  35.           RegisterClass (&wndclass) ;
  36.           }
  37.  
  38.      hwnd = CreateWindow (szAppName, "About Box Demo Program",
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, nCmdShow) ;
  45.      UpdateWindow (hwnd); 
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.           {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.           }
  52.      return msg.wParam ;
  53.      }
  54.  
  55. void PaintWindow (HWND hwnd, short nColor, short nFigure)
  56.      {
  57.      static DWORD dwColor [8] = { RGB (0,     0, 0), RGB (  0,   0, 255),
  58.                                   RGB (0,   255, 0), RGB (  0, 255, 255),
  59.                                   RGB (255,   0, 0), RGB (255,   0, 255),
  60.                                   RGB (255, 255, 0), RGB (255, 255, 255) } ;
  61.      HBRUSH       hBrush ;
  62.      HDC          hdc ;
  63.      RECT         rect ;
  64.  
  65.      hdc = GetDC (hwnd) ;
  66.      GetClientRect (hwnd, &rect) ;
  67.      hBrush = CreateSolidBrush (dwColor [nColor - IDD_BLACK]) ;
  68.      hBrush = SelectObject (hdc, hBrush) ;
  69.  
  70.      if (nFigure == IDD_RECT)
  71.           Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  72.      else
  73.           Ellipse   (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  74.  
  75.      DeleteObject (SelectObject (hdc, hBrush)) ;
  76.      ReleaseDC (hwnd, hdc) ;
  77.      }
  78.  
  79. void PaintTheBlock (HWND hCtrl, short nColor, short nFigure)
  80.      {
  81.      InvalidateRect (hCtrl, NULL, TRUE) ;
  82.      UpdateWindow (hCtrl) ;
  83.      PaintWindow (hCtrl, nColor, nFigure) ;
  84.      }
  85.  
  86. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  87.      {
  88.      static HWND  hCtrlBlock ;
  89.      static short nColor, nFigure ;
  90.  
  91.      switch (message)
  92.           {
  93.           case WM_INITDIALOG:
  94.                nColor  = nCurrentColor ;
  95.                nFigure = nCurrentFigure ;
  96.  
  97.                CheckRadioButton (hDlg, IDD_BLACK, IDD_WHITE, nColor) ; 
  98.                CheckRadioButton (hDlg, IDD_RECT,  IDD_ELL,   nFigure) ;
  99.  
  100.                hCtrlBlock = GetDlgItem (hDlg, IDD_PAINT) ;
  101.  
  102.                SetFocus (GetDlgItem (hDlg, nColor)) ;
  103.                return FALSE ;
  104.  
  105.           case WM_COMMAND:
  106.                switch (wParam)
  107.                     {
  108.                     case IDOK:
  109.                          nCurrentColor  = nColor ;
  110.                          nCurrentFigure = nFigure ;
  111.                          EndDialog (hDlg, TRUE) ;
  112.                          return TRUE ;
  113.  
  114.                     case IDCANCEL:
  115.                          EndDialog (hDlg, FALSE) ;
  116.                          return TRUE ;
  117.  
  118.                     case IDD_BLACK:
  119.                     case IDD_RED:
  120.                     case IDD_GREEN:
  121.                     case IDD_YELLOW:
  122.                     case IDD_BLUE:
  123.                     case IDD_MAGENTA:
  124.                     case IDD_CYAN:
  125.                     case IDD_WHITE:
  126.                          nColor = wParam ;
  127.                          CheckRadioButton (hDlg, IDD_BLACK, IDD_WHITE, wParam);
  128.                          PaintTheBlock (hCtrlBlock, nColor, nFigure) ;
  129.                          return TRUE ;
  130.  
  131.                     case IDD_RECT:
  132.                     case IDD_ELL:
  133.                          nFigure = wParam ;
  134.                          CheckRadioButton (hDlg, IDD_RECT, IDD_ELL, wParam) ;
  135.                          PaintTheBlock (hCtrlBlock, nColor, nFigure) ;
  136.                          return TRUE ;
  137.                     }
  138.                break ;
  139.  
  140.           case WM_PAINT:
  141.                PaintTheBlock (hCtrlBlock, nColor, nFigure) ;
  142.                break ;
  143.           }
  144.      return FALSE ;
  145.      }
  146.  
  147. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  148.      {
  149.      static FARPROC lpfnAboutDlgProc ;
  150.      static HANDLE  hInstance ;
  151.      PAINTSTRUCT    ps ;
  152.  
  153.      switch (message)
  154.           {
  155.           case WM_CREATE:
  156.                hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  157.  
  158.                lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInstance) ;
  159.                return 0 ;
  160.  
  161.           case WM_COMMAND:
  162.                switch (wParam)
  163.                     {
  164.                     case IDM_ABOUT:
  165.                          if (DialogBox (hInstance, "AboutBox", hwnd,
  166.                                         lpfnAboutDlgProc))
  167.                               InvalidateRect (hwnd, NULL, TRUE) ;
  168.                          return 0 ;
  169.                     }
  170.                break ;
  171.  
  172.           case WM_PAINT:
  173.                BeginPaint (hwnd, &ps) ;
  174.                EndPaint (hwnd, &ps) ;
  175.  
  176.                PaintWindow (hwnd, nCurrentColor, nCurrentFigure) ;
  177.                return 0 ;
  178.                     
  179.           case WM_DESTROY :
  180.                PostQuitMessage (0) ;
  181.                return 0 ;
  182.           }
  183.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  184.      }
  185.