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

  1. /*--------------------------------------------------
  2.    CLOVER.C -- Clover Drawing Program using Regions
  3.                (c) Charles Petzold, 1990
  4.   --------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #define TWO_PI (2.0 * 3.14159)
  9.  
  10. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  11.  
  12. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  13.                     LPSTR lpszCmdLine, int nCmdShow)
  14.      {
  15.      static char szAppName[] = "Clover" ;
  16.      HWND        hwnd ;
  17.      MSG         msg ;
  18.      WNDCLASS    wndclass ;
  19.  
  20.      if (!hPrevInstance) 
  21.           {
  22.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.           wndclass.lpfnWndProc   = WndProc ;
  24.           wndclass.cbClsExtra    = 0 ;
  25.           wndclass.cbWndExtra    = 0 ;
  26.           wndclass.hInstance     = hInstance ;
  27.           wndclass.hIcon         = NULL ;
  28.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  30.           wndclass.lpszMenuName  = NULL ;
  31.           wndclass.lpszClassName = szAppName ;
  32.  
  33.           RegisterClass (&wndclass) ;
  34.           }
  35.  
  36.      hwnd = CreateWindow (szAppName, "Draw a Clover",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, nCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.           {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.           }
  50.      return msg.wParam ;
  51.      }
  52.  
  53. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  54.      {
  55.      static HRGN  hRgnClip ;
  56.      static short cxClient, cyClient ;
  57.      double       fAngle, fRadius ;
  58.      HCURSOR      hCursor ;
  59.      HDC          hdc ;
  60.      HRGN         hRgnTemp [6] ;
  61.      PAINTSTRUCT  ps ;
  62.      short        i ;
  63.  
  64.      switch (message)
  65.           {
  66.           case WM_SIZE:
  67.                cxClient = LOWORD (lParam) ;
  68.                cyClient = HIWORD (lParam) ;
  69.  
  70.                hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  71.                ShowCursor (TRUE) ;
  72.  
  73.                if (hRgnClip)
  74.                     DeleteObject (hRgnClip) ;
  75.  
  76.                hRgnTemp [0] = CreateEllipticRgn (0, cyClient / 3,
  77.                                         cxClient / 2, 2 * cyClient / 3) ;
  78.                hRgnTemp [1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
  79.                                         cxClient, 2 * cyClient / 3) ;
  80.                hRgnTemp [2] = CreateEllipticRgn (cxClient / 3, 0,
  81.                                         2 * cxClient / 3, cyClient / 2) ;
  82.                hRgnTemp [3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
  83.                                         2 * cxClient / 3, cyClient) ;
  84.                hRgnTemp [4] = CreateRectRgn (0, 0, 1, 1) ;
  85.                hRgnTemp [5] = CreateRectRgn (0, 0, 1, 1) ;
  86.                hRgnClip     = CreateRectRgn (0, 0, 1, 1) ;
  87.  
  88.                CombineRgn (hRgnTemp [4], hRgnTemp [0], hRgnTemp [1], RGN_OR) ;
  89.                CombineRgn (hRgnTemp [5], hRgnTemp [2], hRgnTemp [3], RGN_OR) ;
  90.                CombineRgn (hRgnClip,     hRgnTemp [4], hRgnTemp [5], RGN_XOR) ;
  91.  
  92.                for (i = 0 ; i < 6 ; i++)
  93.                     DeleteObject (hRgnTemp [i]) ;
  94.  
  95.                SetCursor (hCursor) ;
  96.                ShowCursor (FALSE) ;
  97.                return 0 ;
  98.  
  99.           case WM_PAINT:
  100.                hdc = BeginPaint (hwnd, &ps) ;
  101.  
  102.                SetViewportOrg (hdc, cxClient / 2, cyClient / 2) ;
  103.                SelectClipRgn (hdc, hRgnClip) ;
  104.  
  105.                fRadius = hypot (cxClient / 2.0, cyClient / 2.0) ;
  106.  
  107.                for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)
  108.                     {
  109.                     MoveTo (hdc, 0, 0) ;
  110.                     LineTo (hdc, (short) ( fRadius * cos (fAngle) + 0.5),
  111.                                  (short) (-fRadius * sin (fAngle) + 0.5)) ;
  112.                     }
  113.                EndPaint (hwnd, &ps) ;
  114.                return 0 ;
  115.  
  116.           case WM_DESTROY:
  117.                DeleteObject (hRgnClip) ;
  118.                PostQuitMessage (0) ;
  119.                return 0 ;
  120.           }
  121.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  122.      }
  123.