home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / H0 < prev    next >
Encoding:
Text File  |  1990-11-11  |  5.5 KB  |  171 lines

  1. /*-------------------------------------------------
  2.    CHECKER2.C -- Mouse Hit-Test Demo Program No. 2
  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[] = "Checker2" ;
  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, "Checker2 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.      POINT        point ;
  59.      RECT         rect ;
  60.      short        x, y ;
  61.  
  62.      switch (message)
  63.           {
  64.           case WM_SIZE:
  65.                cxBlock = LOWORD (lParam) / DIVISIONS ;
  66.                cyBlock = HIWORD (lParam) / DIVISIONS ;
  67.                return 0 ;
  68.  
  69.           case WM_SETFOCUS:
  70.                ShowCursor (TRUE) ;
  71.                return 0 ;
  72.  
  73.           case WM_KILLFOCUS:
  74.                ShowCursor (FALSE) ;
  75.                return 0 ;
  76.  
  77.           case WM_KEYDOWN:
  78.                GetCursorPos (&point) ;
  79.                ScreenToClient (hwnd, &point) ;
  80.  
  81.                x = max (0, min (DIVISIONS - 1, point.x / cxBlock)) ;
  82.                y = max (0, min (DIVISIONS - 1, point.y / cyBlock)) ;
  83.   
  84.                switch (wParam)
  85.                     {
  86.                     case VK_UP:
  87.                          y -- ;
  88.                          break ;
  89.  
  90.                     case VK_DOWN:
  91.                          y ++ ;
  92.                          break ;
  93.  
  94.                     case VK_LEFT:
  95.                          x -- ;
  96.                          break ;
  97.  
  98.                     case VK_RIGHT:
  99.                          x ++ ;
  100.                          break ;
  101.  
  102.                     case VK_HOME:
  103.                          x = y = 0 ;
  104.                          break ;
  105.  
  106.                     case VK_END:
  107.                          x = y = DIVISIONS - 1 ;
  108.                          break ;
  109.  
  110.                     case VK_RETURN:
  111.                     case VK_SPACE:
  112.                          SendMessage (hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
  113.                                    MAKELONG (x * cxBlock, y * cyBlock)) ;
  114.                          break ;
  115.                     }
  116.                x = (x + DIVISIONS) % DIVISIONS ;
  117.                y = (y + DIVISIONS) % DIVISIONS ;
  118.  
  119.                point.x = x * cxBlock + cxBlock / 2 ;
  120.                point.y = y * cyBlock + cyBlock / 2 ;
  121.  
  122.                ClientToScreen (hwnd, &point) ;
  123.                SetCursorPos (point.x, point.y) ;
  124.                return 0 ;
  125.  
  126.           case WM_LBUTTONDOWN:
  127.                x = LOWORD (lParam) / cxBlock ;
  128.                y = HIWORD (lParam) / cyBlock ;
  129.  
  130.                if (x < DIVISIONS && y < DIVISIONS)
  131.                     {
  132.                     fState[x][y] ^= 1 ;
  133.  
  134.                     rect.left   = x * cxBlock ;
  135.                     rect.top    = y * cyBlock ;
  136.                     rect.right  = (x + 1) * cxBlock ;
  137.                     rect.bottom = (y + 1) * cyBlock ;
  138.  
  139.                     InvalidateRect (hwnd, &rect, FALSE) ;
  140.                     }
  141.                else
  142.                     MessageBeep (0) ;
  143.                return 0 ;
  144.  
  145.           case WM_PAINT:
  146.                hdc = BeginPaint (hwnd, &ps) ;
  147.  
  148.                for (x = 0 ; x < DIVISIONS ; x++)
  149.                     for (y = 0 ; y < DIVISIONS ; y++)
  150.                          {
  151.                          Rectangle (hdc, x * cxBlock, y * cyBlock,
  152.                                    (x + 1) * cxBlock, (y + 1) * cyBlock) ;
  153.  
  154.                          if (fState [x][y])
  155.                               {
  156.                               MoveTo (hdc,  x    * cxBlock,  y    * cyBlock) ;
  157.                               LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;
  158.                               MoveTo (hdc,  x    * cxBlock, (y+1) * cyBlock) ;
  159.                               LineTo (hdc, (x+1) * cxBlock,  y    * cyBlock) ;
  160.                               }
  161.                          }
  162.                EndPaint (hwnd, &ps) ;
  163.                return 0 ;
  164.  
  165.           case WM_DESTROY:
  166.                PostQuitMessage (0) ;
  167.                return 0 ;
  168.           }
  169.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  170.      }
  171.