windows程序设计里面,画贝塞尔曲线的程序

看到它对鼠标点击之后,重画曲线的处理方法:

首先设置画笔为白色,重画一遍(相当于把原来的擦除了)

然后更新新的坐标,把画壁设为黑色

再画一遍

觉得这真是一个新奇的思路

代码如下:

  1 /*---------------------------------------
  2    BEZIER.C -- Bezier Splines Demo
  3                (c) Charles Petzold, 1998
  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 TCHAR szAppName[] = TEXT ("Bezier") ;
 14      HWND         hwnd ;
 15      MSG          msg ;
 16      WNDCLASS     wndclass ;
 17      
 18      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
 19      wndclass.lpfnWndProc   = WndProc ;
 20      wndclass.cbClsExtra    = 0 ;
 21      wndclass.cbWndExtra    = 0 ;
 22      wndclass.hInstance     = hInstance ;
 23      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
 24      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
 25      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
 26      wndclass.lpszMenuName  = NULL ;
 27      wndclass.lpszClassName = szAppName ;
 28      
 29      if (!RegisterClass (&wndclass))
 30      {
 31           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
 32                       szAppName, MB_ICONERROR) ;
 33           return 0 ;
 34      }
 35      
 36      hwnd = CreateWindow (szAppName, TEXT ("Bezier Splines"),
 37                           WS_OVERLAPPEDWINDOW,
 38                           CW_USEDEFAULT, CW_USEDEFAULT,
 39                           CW_USEDEFAULT, CW_USEDEFAULT,
 40                           NULL, NULL, hInstance, NULL) ;
 41      
 42      ShowWindow (hwnd, iCmdShow) ;
 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 void DrawBezier (HDC hdc, POINT apt[])
 54 {
 55      PolyBezier (hdc, apt, 4) ;
 56      
 57      MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
 58      LineTo   (hdc, apt[1].x, apt[1].y) ;
 59      
 60      MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ;
 61      LineTo   (hdc, apt[3].x, apt[3].y) ;
 62 }
 63 
 64 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 65 {
 66      static POINT apt[4] ;
 67      HDC          hdc ;
 68      int          cxClient, cyClient ;
 69      PAINTSTRUCT  ps ;
 70      
 71      switch (message)
 72      {
 73      case WM_SIZE:
 74           cxClient = LOWORD (lParam) ;
 75           cyClient = HIWORD (lParam) ;
 76           
 77           apt[0].x = cxClient / 4 ;
 78           apt[0].y = cyClient / 2 ;
 79           
 80           apt[1].x = cxClient / 2 ;
 81           apt[1].y = cyClient / 4 ;
 82           
 83           apt[2].x =     cxClient / 2 ;
 84           apt[2].y = 3 * cyClient / 4 ;
 85           
 86           apt[3].x = 3 * cxClient / 4 ;
 87           apt[3].y =     cyClient / 2 ;
 88           
 89           return 0 ;
 90 
 91      case WM_LBUTTONDOWN:
 92      case WM_RBUTTONDOWN:
 93      case WM_MOUSEMOVE:
 94           if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
 95           {
 96                hdc = GetDC (hwnd) ;
 97                
 98                //擦除原来的线条
 99                SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
100                DrawBezier (hdc, apt) ;
101                
102                if (wParam & MK_LBUTTON)
103                {
104                     apt[1].x = LOWORD (lParam) ;
105                     apt[1].y = HIWORD (lParam) ;
106                }
107                
108                if (wParam & MK_RBUTTON)
109                {
110                     apt[2].x = LOWORD (lParam) ;
111                     apt[2].y = HIWORD (lParam) ;
112                }
113                
114                //画出新的线条
115                SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
116                DrawBezier (hdc, apt) ;
117                ReleaseDC (hwnd, hdc) ;
118           }
119           return 0 ;
120           
121      case WM_PAINT:
122           InvalidateRect (hwnd, NULL, TRUE) ;
123           
124           hdc = BeginPaint (hwnd, &ps) ;
125           
126           DrawBezier (hdc, apt) ;
127           
128           EndPaint (hwnd, &ps) ;
129           return 0 ;
130           
131      case WM_DESTROY:
132           PostQuitMessage (0) ;
133           return 0 ;
134      }
135      return DefWindowProc (hwnd, message, wParam, lParam) ;
136 }