#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("DrawBezier");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void DrawBezier(HDC hdc, POINT pt[]) {
PolyBezier(hdc, pt, 4);
MoveToEx(hdc, pt[0].x, pt[0].y, NULL);
LineTo(hdc, pt[1].x, pt[1].y);
MoveToEx(hdc, pt[2].x, pt[2].y, NULL);
LineTo(hdc, pt[3].x, pt[3].y);
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
static int cxClient, cyClient;
PAINTSTRUCT ps;
static POINT pt[4];
HDC hdc;
switch(message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
pt[0].x = 0;
pt[0].y = cyClient / 2;
pt[1].x = cxClient / 4;
pt[1].y = cyClient / 4;
pt[2].x = 3 * cxClient / 4;
pt[2].y = 3 * cyClient / 4;
pt[3].x = cxClient;
pt[3].y = cyClient / 2;
return 0;
// 在这里我单独处理了WM_LBUTTONDOWN消息
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(WHITE_PEN));
DrawBezier(hdc, pt);
pt[1].x = LOWORD(lParam);
pt[1].y = HIWORD(lParam);
SelectObject(hdc, GetStockObject(BLACK_PEN));
DrawBezier(hdc, pt);
ReleaseDC(hwnd, hdc);
return 0;
// 在这里我单独处理了WM_RBUTTONDOWN消息
case WM_RBUTTONDOWN:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(WHITE_PEN));
DrawBezier(hdc, pt);
pt[2].x = LOWORD(lParam);
pt[2].y = HIWORD(lParam);
SelectObject(hdc, GetStockObject(BLACK_PEN));
DrawBezier(hdc, pt);
ReleaseDC(hwnd, hdc);
return 0;
// 在这里我处理了WM_MOUSEMOVE消息
case WM_MOUSEMOVE:
hdc = GetDC(hwnd);
if(wParam & MK_LBUTTON) {
SelectObject(hdc, GetStockObject(WHITE_PEN));
DrawBezier(hdc, pt);
pt[1].x = LOWORD(lParam);
pt[1].y = HIWORD(lParam);
SelectObject(hdc, GetStockObject(BLACK_PEN));
DrawBezier(hdc, pt);
}
if(wParam & MK_RBUTTON) {
SelectObject(hdc, GetStockObject(WHITE_PEN));
DrawBezier(hdc, pt);
pt[2].x = LOWORD(lParam);
pt[2].y = HIWORD(lParam);
SelectObject(hdc, GetStockObject(BLACK_PEN));
DrawBezier(hdc, pt);
}
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
DrawBezier(hdc, pt);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|