第五章 绘图基础(BEZIER)
程序员文章站
2022-05-09 08:42:23
1 /* 2 BEZIER.C -- Bezier Splines Demo 3 (c) Charles Petzold, 1998 4 */ 5 6 #include 7 8 LRESULT CALLBACK WndPorc(HWND, UINT, WPARAM, LPAR ......
1 /*----------------------------- 2 BEZIER.C -- Bezier Splines Demo 3 (c) Charles Petzold, 1998 4 -----------------------------*/ 5 6 #include <Windows.h> 7 8 LRESULT CALLBACK WndPorc(HWND, UINT, WPARAM, LPARAM); 9 10 int WINAPI WinMain( __in HINSTANCE hInstance 11 , __in_opt HINSTANCE hPrevInstance 12 , __in LPSTR lpCmdLine 13 , __in int nShowCmd ) 14 { 15 static TCHAR szAppName[] = TEXT("Bezier"); 16 HWND hwnd; 17 MSG msg; 18 WNDCLASS wndclass; 19 20 wndclass.style = CS_HREDRAW | CS_VREDRAW; 21 wndclass.lpfnWndProc = WndPorc; 22 wndclass.cbClsExtra = 0; 23 wndclass.cbWndExtra = 0; 24 wndclass.hInstance = hInstance; 25 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 26 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 27 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 28 wndclass.lpszMenuName = NULL; 29 wndclass.lpszClassName = szAppName; 30 31 if (!RegisterClass(&wndclass)) 32 { 33 MessageBox(NULL, TEXT("Program requires Windows NT!") 34 , szAppName, MB_ICONERROR); 35 return 0; 36 } 37 38 hwnd = CreateWindow(szAppName, TEXT("Bezier Splines") 39 , WS_OVERLAPPEDWINDOW 40 , CW_USEDEFAULT, CW_USEDEFAULT 41 , CW_USEDEFAULT, CW_USEDEFAULT 42 , NULL, NULL, hInstance, NULL); 43 44 ShowWindow(hwnd, nShowCmd); 45 UpdateWindow(hwnd); 46 47 while (GetMessage(&msg, NULL, 0, 0)) 48 { 49 TranslateMessage(&msg); 50 DispatchMessage(&msg); 51 } 52 53 return msg.wParam; 54 } 55 56 void DrawBezier(HDC hdc, POINT apt[]) 57 { 58 PolyBezier(hdc, apt, 4); 59 60 MoveToEx(hdc, apt[0].x, apt[0].y, NULL); 61 LineTo(hdc, apt[1].x, apt[1].y); 62 63 MoveToEx(hdc, apt[2].x, apt[2].y, NULL); 64 LineTo(hdc, apt[3].x, apt[3].y); 65 } 66 67 LRESULT CALLBACK WndPorc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 68 { 69 static POINT apt[4]; 70 HDC hdc; 71 int cxClient, cyClient; 72 PAINTSTRUCT ps; 73 74 switch (message) 75 { 76 case WM_SIZE: 77 cxClient = LOWORD(lParam); 78 cyClient = HIWORD(lParam); 79 80 apt[0].x = cxClient / 4; 81 apt[0].y = cyClient / 2; 82 83 apt[1].x = cxClient / 2; 84 apt[1].y = cyClient / 4; 85 86 apt[2].x = cxClient / 2; 87 apt[2].y = 3 * cyClient / 4; 88 89 apt[3].x = 3 * cxClient / 4; 90 apt[3].y = cyClient / 2; 91 92 return 0; 93 94 case WM_LBUTTONDOWN: 95 case WM_RBUTTONDOWN: 96 case WM_MOUSEMOVE: 97 if (wParam & MK_LBUTTON || wParam & MK_RBUTTON) 98 { 99 hdc = GetDC(hwnd); 100 SelectObject(hdc, GetStockObject(WHITE_PEN)); 101 102 DrawBezier(hdc, apt); 103 104 if (wParam & MK_LBUTTON) 105 { 106 apt[1].x = LOWORD(lParam); 107 apt[1].y = HIWORD(lParam); 108 } 109 110 if (wParam & MK_RBUTTON) 111 { 112 apt[2].x = LOWORD(lParam); 113 apt[2].y = HIWORD(lParam); 114 } 115 116 SelectObject(hdc, GetStockObject(BLACK_PEN)); 117 118 DrawBezier(hdc, apt); 119 120 ReleaseDC(hwnd, hdc); 121 } 122 return 0; 123 124 case WM_PAINT: 125 InvalidateRect(hwnd, NULL, TRUE); 126 hdc = BeginPaint(hwnd, &ps); 127 128 DrawBezier(hdc, apt); 129 130 EndPaint(hwnd, &ps); 131 return 0; 132 133 case WM_DESTROY: 134 PostQuitMessage(0); 135 return 0; 136 } 137 138 return DefWindowProc(hwnd, message, wParam, lParam); 139 }
BEZIER程序的显示结果