home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap13 / bounce.c next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  4.9 KB  |  148 lines

  1. /*---------------------------------------
  2.    BOUNCE.C -- Bouncing Ball Program
  3.                (c) Charles Petzold, 1990
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  9.  
  10. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  11.                     LPSTR lpszCmdLine, int nCmdShow)
  12.      {
  13.      static char szAppName [] = "Bounce" ;
  14.      HWND        hwnd ;
  15.      MSG         msg ;
  16.      WNDCLASS    wndclass ;
  17.  
  18.      if (!hPrevInstance) 
  19.           {
  20.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.           wndclass.lpfnWndProc   = WndProc ;
  22.           wndclass.cbClsExtra    = 0 ;
  23.           wndclass.cbWndExtra    = 0 ;
  24.           wndclass.hInstance     = hInstance ;
  25.           wndclass.hIcon         = NULL ;
  26.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  28.           wndclass.lpszMenuName  = NULL ;
  29.           wndclass.lpszClassName = szAppName ;
  30.  
  31.           RegisterClass (&wndclass) ;
  32.           }
  33.  
  34.      hwnd = CreateWindow (szAppName, "Bouncing Ball",
  35.                           WS_OVERLAPPEDWINDOW,
  36.                           CW_USEDEFAULT, CW_USEDEFAULT,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           NULL, NULL, hInstance, NULL) ;
  39.  
  40.      if (!SetTimer (hwnd, 1, 50, NULL))
  41.           {
  42.           MessageBox (hwnd, "Too many clocks or timers!",
  43.                       szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  44.           return FALSE ;
  45.           }
  46.  
  47.      ShowWindow (hwnd, nCmdShow) ;
  48.      UpdateWindow (hwnd) ;
  49.  
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.           {
  52.           TranslateMessage (&msg) ;
  53.           DispatchMessage (&msg) ;
  54.           }
  55.      return msg.wParam ;
  56.      }
  57.  
  58. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  59.      {
  60.      static HANDLE hBitmap ;
  61.      static short  cxClient, cyClient, xCenter, yCenter, cxTotal, cyTotal,
  62.                    cxRadius, cyRadius, cxMove, cyMove, xPixel, yPixel ;
  63.      HBRUSH        hBrush ;
  64.      HDC           hdc, hdcMem ;
  65.      short         nScale ;
  66.  
  67.      switch (message)
  68.           {
  69.           case WM_CREATE:
  70.                hdc = GetDC (hwnd) ;
  71.                xPixel = GetDeviceCaps (hdc, ASPECTX) ;
  72.                yPixel = GetDeviceCaps (hdc, ASPECTY) ;
  73.                ReleaseDC (hwnd, hdc) ;
  74.                return 0 ;
  75.  
  76.           case WM_SIZE:
  77.                xCenter = (cxClient = LOWORD (lParam)) / 2 ;
  78.                yCenter = (cyClient = HIWORD (lParam)) / 2 ;
  79.  
  80.                nScale = min (cxClient * xPixel, cyClient * yPixel) / 16 ;
  81.  
  82.                cxRadius = nScale / xPixel ;
  83.                cyRadius = nScale / yPixel ;
  84.  
  85.                cxMove = max (1, cxRadius / 4) ;
  86.                cyMove = max (1, cyRadius / 4) ;
  87.  
  88.                cxTotal = 2 * (cxRadius + cxMove) ;
  89.                cyTotal = 2 * (cyRadius + cyMove) ;
  90.  
  91.                if (hBitmap)
  92.                     DeleteObject (hBitmap) ;
  93.  
  94.                hdc = GetDC (hwnd) ;
  95.                hdcMem = CreateCompatibleDC (hdc) ;
  96.                hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal) ;
  97.                ReleaseDC (hwnd, hdc) ;
  98.  
  99.                SelectObject (hdcMem, hBitmap) ;
  100.                Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) ;
  101.  
  102.                hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L) ;
  103.                SelectObject (hdcMem, hBrush) ;
  104.                SetBkColor (hdcMem, RGB (255, 0, 255)) ;
  105.                Ellipse (hdcMem, cxMove, cyMove, cxTotal - cxMove,
  106.                                                 cyTotal - cyMove) ;
  107.                DeleteDC (hdcMem) ;
  108.                DeleteObject (hBrush) ;
  109.                return 0 ;
  110.  
  111.           case WM_TIMER:
  112.                if (!hBitmap)
  113.                     break ;
  114.  
  115.                hdc = GetDC (hwnd) ;
  116.                hdcMem = CreateCompatibleDC (hdc) ;
  117.                SelectObject (hdcMem, hBitmap) ;
  118.  
  119.                BitBlt (hdc, xCenter - cxTotal / 2,
  120.                             yCenter - cyTotal / 2, cxTotal, cyTotal,
  121.                        hdcMem, 0, 0, SRCCOPY) ;
  122.  
  123.                ReleaseDC (hwnd, hdc) ;
  124.                DeleteDC (hdcMem) ;
  125.  
  126.                xCenter += cxMove ;
  127.                yCenter += cyMove ;
  128.  
  129.                if ((xCenter + cxRadius >= cxClient) ||
  130.                    (xCenter - cxRadius <= 0))
  131.                          cxMove = -cxMove ;
  132.  
  133.                if ((yCenter + cyRadius >= cyClient) ||
  134.                    (yCenter - cyRadius <= 0))
  135.                          cyMove = -cyMove ;
  136.                return 0 ;
  137.  
  138.           case WM_DESTROY:
  139.                if (hBitmap)
  140.                     DeleteObject (hBitmap) ;
  141.  
  142.                KillTimer (hwnd, 1) ;
  143.                PostQuitMessage (0) ;
  144.                return 0 ;
  145.           }
  146.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  147.      }
  148.