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

  1. /*-------------------------------------------------
  2.    CHECKER1.C -- Mouse Hit-Test Demo Program No. 1
  3.                  (c) Charles Petzold, 1990
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #define DIVISIONS 5
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10.  
  11. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  12.                     LPSTR  lpszCmdLine, int nCmdShow)
  13.      {
  14.      static char szAppName[] = "Checker1" ;
  15.      HWND        hwnd ;
  16.      MSG         msg ;
  17.      WNDCLASS    wndclass ;
  18.  
  19.      if (!hPrevInstance) 
  20.           {
  21.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.           wndclass.lpfnWndProc   = WndProc ;
  23.           wndclass.cbClsExtra    = 0 ;
  24.           wndclass.cbWndExtra    = 0 ;
  25.           wndclass.hInstance     = hInstance ;
  26.           wndclass.hIcon         = NULL ;
  27.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  29.           wndclass.lpszMenuName  = NULL ;
  30.           wndclass.lpszClassName = szAppName ;
  31.  
  32.           RegisterClass (&wndclass) ;
  33.           }
  34.  
  35.      hwnd = CreateWindow (szAppName, "Checker1 Mouse Hit-Test Demo",
  36.                          WS_OVERLAPPEDWINDOW,
  37.                          CW_USEDEFAULT, CW_USEDEFAULT,
  38.                          CW_USEDEFAULT, CW_USEDEFAULT,
  39.                          NULL, NULL, hInstance, NULL) ;
  40.  
  41.      ShowWindow (hwnd, nCmdShow) ;
  42.      UpdateWindow (hwnd) ;
  43.  
  44.      while (GetMessage (&msg, NULL, 0, 0))
  45.           {
  46.           TranslateMessage (&msg) ;
  47.           DispatchMessage (&msg) ;
  48.           }
  49.      return msg.wParam ;
  50.      }
  51.  
  52. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  53.      {
  54.      static BOOL  fState[DIVISIONS][DIVISIONS] ;
  55.      static short cxBlock, cyBlock ;
  56.      HDC          hdc ;
  57.      PAINTSTRUCT  ps ;
  58.      RECT         rect ;
  59.      short        x, y ;
  60.  
  61.      switch (message)
  62.           {
  63.           case WM_SIZE:
  64.                cxBlock = LOWORD (lParam) / DIVISIONS ;
  65.                cyBlock = HIWORD (lParam) / DIVISIONS ;
  66.                return 0 ;
  67.  
  68.           case WM_LBUTTONDOWN:
  69.                x = LOWORD (lParam) / cxBlock ;
  70.                y = HIWORD (lParam) / cyBlock ;
  71.  
  72.                if (x < DIVISIONS && y < DIVISIONS)
  73.                     {
  74.                     fState [x][y] ^= 1 ;
  75.  
  76.                     rect.left   = x * cxBlock ;
  77.                     rect.top    = y * cyBlock ;
  78.                     rect.right  = (x + 1) * cxBlock ;
  79.                     rect.bottom = (y + 1) * cyBlock ;
  80.  
  81.                     InvalidateRect (hwnd, &rect, FALSE) ;
  82.                     }
  83.                else
  84.                     MessageBeep (0) ;
  85.                return 0 ;
  86.  
  87.           case WM_PAINT:
  88.                hdc = BeginPaint (hwnd, &ps) ;
  89.  
  90.                for (x = 0 ; x < DIVISIONS ; x++)
  91.                     for (y = 0 ; y < DIVISIONS ; y++)
  92.                          {
  93.                          Rectangle (hdc, x * cxBlock, y * cyBlock,
  94.                                    (x + 1) * cxBlock, (y + 1) * cyBlock) ;
  95.  
  96.                          if (fState [x][y])
  97.                               {
  98.                               MoveTo (hdc,  x    * cxBlock,  y    * cyBlock) ;
  99.                               LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;
  100.                               MoveTo (hdc,  x    * cxBlock, (y+1) * cyBlock) ;
  101.                               LineTo (hdc, (x+1) * cxBlock,  y    * cyBlock) ;
  102.                               }
  103.                          }
  104.                EndPaint (hwnd, &ps) ;
  105.                return 0 ;
  106.  
  107.           case WM_DESTROY:
  108.                PostQuitMessage (0) ;
  109.                return 0 ;
  110.           }
  111.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  112.      }
  113.