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

  1. /*-------------------------------------------------
  2.    CHECKER3.C -- Mouse Hit-Test Demo Program No. 3
  3.                  (c) Charles Petzold, 1998
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONS 5
  9.  
  10. LRESULT CALLBACK WndProc   (HWND, UINT, WPARAM, LPARAM) ;
  11. LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. TCHAR szChildClass[] = TEXT ("Checker3_Child") ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17. {
  18.      static TCHAR szAppName[] = TEXT ("Checker3") ;
  19.      HWND         hwnd ;
  20.      MSG          msg ;
  21.      WNDCLASS     wndclass ;
  22.      
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  31.      wndclass.lpszMenuName  = NULL ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      
  34.      if (!RegisterClass (&wndclass))
  35.      {
  36.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  37.                       szAppName, MB_ICONERROR) ;
  38.           return 0 ;
  39.      }
  40.      
  41.      wndclass.lpfnWndProc   = ChildWndProc ;
  42.      wndclass.cbWndExtra    = sizeof (long) ;
  43.      wndclass.hIcon         = NULL ;
  44.      wndclass.lpszClassName = szChildClass ;
  45.      
  46.      RegisterClass (&wndclass) ;
  47.      
  48.      hwnd = CreateWindow (szAppName, TEXT ("Checker3 Mouse Hit-Test Demo"),
  49.                           WS_OVERLAPPEDWINDOW,
  50.                           CW_USEDEFAULT, CW_USEDEFAULT,
  51.                           CW_USEDEFAULT, CW_USEDEFAULT,
  52.                           NULL, NULL, hInstance, NULL) ;
  53.      
  54.      ShowWindow (hwnd, iCmdShow) ;
  55.      UpdateWindow (hwnd) ;
  56.      
  57.      while (GetMessage (&msg, NULL, 0, 0))
  58.      {
  59.           TranslateMessage (&msg) ;
  60.           DispatchMessage (&msg) ;
  61.      }
  62.      return msg.wParam ;
  63. }
  64.  
  65. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  66. {
  67.      static HWND hwndChild[DIVISIONS][DIVISIONS] ;
  68.      int         cxBlock, cyBlock, x, y ;
  69.      
  70.      switch (message)
  71.      {
  72.      case WM_CREATE :
  73.           for (x = 0 ; x < DIVISIONS ; x++)
  74.                for (y = 0 ; y < DIVISIONS ; y++)
  75.                     hwndChild[x][y] = CreateWindow (szChildClass, NULL,
  76.                               WS_CHILDWINDOW | WS_VISIBLE,
  77.                               0, 0, 0, 0,
  78.                               hwnd, (HMENU) (y << 8 | x),
  79.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  80.                               NULL) ;
  81.           return 0 ;
  82.                
  83.      case WM_SIZE :
  84.           cxBlock = LOWORD (lParam) / DIVISIONS ;
  85.           cyBlock = HIWORD (lParam) / DIVISIONS ;
  86.           
  87.           for (x = 0 ; x < DIVISIONS ; x++)
  88.                 for (y = 0 ; y < DIVISIONS ; y++)
  89.                     MoveWindow (hwndChild[x][y],
  90.                                 x * cxBlock, y * cyBlock,
  91.                                 cxBlock, cyBlock, TRUE) ;
  92.           return 0 ;
  93.                
  94.      case WM_LBUTTONDOWN :
  95.           MessageBeep (0) ;
  96.           return 0 ;
  97.           
  98.      case WM_DESTROY :
  99.           PostQuitMessage (0) ;
  100.           return 0 ;
  101.      }
  102.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  103. }
  104.  
  105. LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message, 
  106.                                WPARAM wParam, LPARAM lParam)
  107. {
  108.      HDC         hdc ;
  109.      PAINTSTRUCT ps ;
  110.      RECT        rect ;
  111.      
  112.      switch (message)
  113.      {
  114.      case WM_CREATE :
  115.           SetWindowLong (hwnd, 0, 0) ;       // on/off flag
  116.           return 0 ;
  117.           
  118.      case WM_LBUTTONDOWN :
  119.           SetWindowLong (hwnd, 0, 1 ^ GetWindowLong (hwnd, 0)) ;
  120.           InvalidateRect (hwnd, NULL, FALSE) ;
  121.           return 0 ;
  122.           
  123.      case WM_PAINT :
  124.           hdc = BeginPaint (hwnd, &ps) ;
  125.           
  126.           GetClientRect (hwnd, &rect) ;
  127.           Rectangle (hdc, 0, 0, rect.right, rect.bottom) ;
  128.           
  129.           if (GetWindowLong (hwnd, 0))
  130.           {
  131.                MoveToEx (hdc, 0,          0, NULL) ;
  132.                LineTo   (hdc, rect.right, rect.bottom) ;
  133.                MoveToEx (hdc, 0,          rect.bottom, NULL) ;
  134.                LineTo   (hdc, rect.right, 0) ;
  135.           }
  136.           
  137.           EndPaint (hwnd, &ps) ;
  138.           return 0 ;
  139.      }
  140.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  141. }
  142.