#include <Windows.h>
#include <math.h>
#define NUM 1000
#define TWOPI (2*3.14159)
void DrawBezier(HDC hdc, POINT apt[])
{
PolyBezier(hdc,apt,4);
MoveToEx(hdc,apt[0].x,apt[0].y,NULL);
LineTo(hdc,apt[1].x,apt[1].y);
MoveToEx(hdc,apt[2].x,apt[2].y,NULL);
LineTo(hdc,apt[3].x,apt[3].y);
};
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
int cxClient,cyClient;
HDC hdc;
PAINTSTRUCT ps;
static POINT apt[4];
switch(uMsg)
{
case WM_CHAR:
{
return 0;
}break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MOUSEMOVE:
{
if(wParam &MK_LBUTTON || wParam &MK_RBUTTON)
{
hdc = GetDC(hwnd);
SelectObject(hdc,GetStockObject(WHITE_PEN));
DrawBezier(hdc,apt);
if(wParam & MK_LBUTTON)
{
apt[1].x = LOWORD(lParam);
apt[1].y = HIWORD(lParam);
}
if(wParam & MK_RBUTTON)
{
apt[2].x = LOWORD(lParam);
apt[2].y = HIWORD(lParam);
}
SelectObject(hdc,GetStockObject(BLACK_PEN));
DrawBezier(hdc,apt);
ReleaseDC(hwnd,hdc);
}
}break;
case WM_PAINT:
{
//hdc = BeginPaint(hwnd,&ps);
/*
MoveToEx(hdc,0,cyClient/2,NULL);
LineTo(hdc,cxClient,cyClient/2);
for(i = 0;i<NUM;i++)
{
apt[i].x = i*cxClient/NUM;
apt[i].y = (int)(cyClient /2*(1 - sin(TWOPI*i/NUM)));
}
Polyline(hdc,apt,NUM);
*/
//Rectangle(hdc,cxClient/8,cyClient/8,7*cxClient/8,7*cyClient/8);
//MoveToEx(hdc,0,0,NULL);
//LineTo(hdc,cxClient,cyClient);
//Ellipse(hdc,cxClient/8,cyClient/8,7*cxClient/8,7*cyClient/8);
//Ellipse(hdc,cxClient/8,cxClient/8,cxClient,cyClient);
//EndPaint(hwnd,&ps);
InvalidateRect(hwnd,NULL,TRUE);
hdc = BeginPaint(hwnd,&ps);
DrawBezier(hdc,apt);
EndPaint(hwnd,&ps);
}break;
case WM_SIZE:
{
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
apt[0].x = cxClient/4;
apt[0].y = cyClient/2;
apt[1].x = cxClient/2;
apt[1].y = cyClient/4;
apt[2].x = cxClient/2;
apt[2].y = 3*cyClient/4;
apt[3].x = 3*cxClient/4;
apt[3].y = cyClient/2;
}break;
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
//配置属性
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
//窗体背景
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
//鼠标样式
wndclass.hCursor = LoadCursor(NULL,IDC_HAND);
//窗体图标
wndclass.hIcon = LoadIcon(NULL,IDI_WARNING);
//窗体句柄
wndclass.hInstance = hInstance;
//消息处理
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = "Magic";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW|CS_HREDRAW;
RegisterClass(&wndclass);
HWND hwnd = CreateWindow("Magic","图片艺术",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
BOOL flag = 0;
while((flag = GetMessage(&msg,NULL,0,0))!=0)
{
if(flag ==-1)
{
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}