home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / BEZIER.C next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.9 KB  |  132 lines

  1. /*---------------------------------------
  2.    BEIZER.C -- Bezier Splines Demo
  3.                (c) Charles Petzold, 1996
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12.      {
  13.      static char szAppName[] = "Bezier" ;
  14.      HWND        hwnd ;
  15.      MSG         msg ;
  16.      WNDCLASSEX  wndclass ;
  17.  
  18.      wndclass.cbSize        = sizeof (wndclass) ;
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  30.  
  31.      RegisterClassEx (&wndclass) ;
  32.  
  33.      hwnd = CreateWindow (szAppName, "Bezier Splines",
  34.                           WS_OVERLAPPEDWINDOW,
  35.                           CW_USEDEFAULT, CW_USEDEFAULT,
  36.                           CW_USEDEFAULT, CW_USEDEFAULT,
  37.                           NULL, NULL, hInstance, NULL) ;
  38.  
  39.      ShowWindow (hwnd, iCmdShow) ;
  40.      UpdateWindow (hwnd) ;
  41.  
  42.      while (GetMessage (&msg, NULL, 0, 0))
  43.           {
  44.           TranslateMessage (&msg) ;
  45.           DispatchMessage (&msg) ;
  46.           }
  47.      return msg.wParam ;
  48.      }
  49.  
  50. void DrawBezier (HDC hdc, POINT apt[])
  51.      {
  52.      PolyBezier (hdc, apt, 4) ;
  53.  
  54.      MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
  55.      LineTo   (hdc, apt[1].x, apt[1].y) ;
  56.  
  57.      MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ;
  58.      LineTo   (hdc, apt[3].x, apt[3].y) ;
  59.      }
  60.  
  61. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  62.      {
  63.      static POINT apt[4] ;
  64.      HDC          hdc ;
  65.      int          cxClient, cyClient ;
  66.      PAINTSTRUCT  ps ;
  67.  
  68.      switch (iMsg)
  69.           {
  70.           case WM_SIZE:
  71.                cxClient = LOWORD (lParam) ;
  72.                cyClient = HIWORD (lParam) ;
  73.  
  74.                apt[0].x = cxClient / 4 ;
  75.                apt[0].y = cyClient / 2 ;
  76.  
  77.                apt[1].x = cxClient / 2 ;
  78.                apt[1].y = cyClient / 4 ;
  79.  
  80.                apt[2].x =     cxClient / 2 ;
  81.                apt[2].y = 3 * cyClient / 4 ;
  82.  
  83.                apt[3].x = 3 * cxClient / 4 ;
  84.                apt[3].y =     cyClient / 2 ;
  85.  
  86.                return 0 ;
  87.  
  88.           case WM_MOUSEMOVE:
  89.                if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
  90.                     {
  91.                     hdc = GetDC (hwnd) ;
  92.  
  93.                     SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
  94.                     DrawBezier (hdc, apt) ;
  95.  
  96.                     if (wParam & MK_LBUTTON)
  97.                          {
  98.                          apt[1].x = LOWORD (lParam) ;
  99.                          apt[1].y = HIWORD (lParam) ;
  100.                          }
  101.  
  102.                     if (wParam & MK_RBUTTON)
  103.                          {
  104.                          apt[2].x = LOWORD (lParam) ;
  105.                          apt[2].y = HIWORD (lParam) ;
  106.                          }
  107.  
  108.                     SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
  109.                     DrawBezier (hdc, apt) ;
  110.                     ReleaseDC (hwnd, hdc) ;
  111.                     }
  112.  
  113.                return 0 ;
  114.  
  115.           case WM_PAINT:
  116.                InvalidateRect (hwnd, NULL, TRUE) ;
  117.  
  118.                hdc = BeginPaint (hwnd, &ps) ;
  119.  
  120.                DrawBezier (hdc, apt) ;
  121.  
  122.                EndPaint (hwnd, &ps) ;
  123.                return 0 ;
  124.  
  125.           case WM_DESTROY:
  126.                PostQuitMessage (0) ;
  127.                return 0 ;
  128.           }
  129.  
  130.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  131.      }
  132.