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

  1. /*-------------------------------------------------
  2.    CHECKER3.C -- Mouse Hit-Test Demo Program No. 3
  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. long FAR PASCAL ChildWndProc (HWND, WORD, WORD, LONG) ;
  11.  
  12. char szChildClass[] = "Checker3_Child" ;
  13.  
  14. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  15.                     LPSTR lpszCmdLine, int nCmdShow)
  16.      {
  17.      static char szAppName[] = "Checker3" ;
  18.      HWND        hwnd ;
  19.      MSG         msg ;
  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 (NULL, IDI_APPLICATION) ;
  30.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  31.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  32.           wndclass.lpszMenuName  = NULL ;
  33.           wndclass.lpszClassName = szAppName ;
  34.  
  35.           RegisterClass (&wndclass) ;
  36.  
  37.           wndclass.lpfnWndProc   = ChildWndProc ;
  38.           wndclass.cbWndExtra    = sizeof (WORD) ;
  39.           wndclass.hIcon         = NULL ;
  40.           wndclass.lpszClassName = szChildClass ;
  41.  
  42.           RegisterClass (&wndclass) ;
  43.           }
  44.  
  45.      hwnd = CreateWindow (szAppName, "Checker3 Mouse Hit-Test Demo",
  46.                          WS_OVERLAPPEDWINDOW,
  47.                          CW_USEDEFAULT, CW_USEDEFAULT,
  48.                          CW_USEDEFAULT, CW_USEDEFAULT,
  49.                          NULL, NULL, hInstance, NULL) ;
  50.  
  51.      ShowWindow (hwnd, nCmdShow) ;
  52.      UpdateWindow (hwnd) ;
  53.  
  54.      while (GetMessage (&msg, NULL, 0, 0))
  55.           {
  56.           TranslateMessage (&msg) ;
  57.           DispatchMessage (&msg) ;
  58.           }
  59.      return msg.wParam ;
  60.      }
  61.  
  62. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  63.      {
  64.      static HWND hwndChild [DIVISIONS] [DIVISIONS] ;
  65.      short       cxBlock, cyBlock, x, y ;
  66.  
  67.      switch (message)
  68.           {
  69.           case WM_CREATE:
  70.                for (x = 0 ; x < DIVISIONS ; x++)
  71.                     for (y = 0 ; y < DIVISIONS ; y++)
  72.                          {
  73.                          hwndChild [x][y] = CreateWindow (szChildClass, NULL,
  74.                               WS_CHILDWINDOW | WS_VISIBLE,
  75.                               0, 0, 0, 0,
  76.                               hwnd, y << 8 | x,
  77.                               GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  78.                          }
  79.                return 0 ;
  80.  
  81.           case WM_SIZE:
  82.                cxBlock = LOWORD (lParam) / DIVISIONS ;
  83.                cyBlock = HIWORD (lParam) / DIVISIONS ;
  84.  
  85.                for (x = 0 ; x < DIVISIONS ; x++)
  86.                     for (y = 0 ; y < DIVISIONS ; y++)
  87.                          MoveWindow (hwndChild [x][y],
  88.                               x * cxBlock, y * cyBlock,
  89.                               cxBlock, cyBlock, TRUE) ;
  90.                return 0 ;
  91.  
  92.           case WM_LBUTTONDOWN:
  93.                MessageBeep (0) ;
  94.                return 0 ;
  95.  
  96.           case WM_DESTROY:
  97.                PostQuitMessage (0) ;
  98.                return 0 ;
  99.           }
  100.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  101.      }
  102.  
  103. long FAR PASCAL ChildWndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  104.      {
  105.      HDC         hdc ;
  106.      PAINTSTRUCT ps ;
  107.      RECT        rect ;
  108.  
  109.      switch (message)
  110.           {
  111.           case WM_CREATE:
  112.                SetWindowWord (hwnd, 0, 0) ;       // on/off flag
  113.                return 0 ;
  114.  
  115.           case WM_LBUTTONDOWN:
  116.                SetWindowWord (hwnd, 0, 1 ^ GetWindowWord (hwnd, 0)) ;
  117.                InvalidateRect (hwnd, NULL, FALSE) ;
  118.                return 0 ;
  119.  
  120.           case WM_PAINT:
  121.                hdc = BeginPaint (hwnd, &ps) ;
  122.  
  123.                GetClientRect (hwnd, &rect) ;
  124.                Rectangle (hdc, 0, 0, rect.right, rect.bottom) ;
  125.  
  126.                if (GetWindowWord (hwnd, 0))
  127.                     {
  128.                     MoveTo (hdc, 0,          0) ;
  129.                     LineTo (hdc, rect.right, rect.bottom) ;
  130.                     MoveTo (hdc, 0,          rect.bottom) ;
  131.                     LineTo (hdc, rect.right, 0) ;
  132.                     }
  133.  
  134.                EndPaint (hwnd, &ps) ;
  135.                return 0 ;
  136.           }
  137.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  138.      }
  139.