#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* The 'main' function of Win32 GUI programs: this is where execution starts */ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口类名*/ 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= (HBRUSH)GetStockObject(WHITE_BRUSH);/*窗口背景画刷*/ wndclass.lpszMenuName = NULL; /*窗口菜单*/ wndclass.lpszClassName= szClassName; /*窗口类名*/ /*注册窗口*/ RegisterClass(&wndclass); /*************第二步:创建窗口(并让窗口显示出来)***************/ hwnd = CreateWindow( szClassName, /*窗口名字*/ TEXT("Welcome"), /*窗口标题*/ WS_OVERLAPPEDWINDOW, /*窗口风格*/ CW_USEDEFAULT, /*初始化x轴的位置*/ CW_USEDEFAULT, /*初始化y轴的位置*/ 640, /*窗口宽度*/ 480, /*窗口高度*/ NULL, /*父窗口句柄*/ NULL, /*窗口菜单句柄*/ hInstance, /*当前窗口句柄*/ NULL /*不使用该值*/ ); if(hwnd == NULL) { MessageBox(NULL, "创建窗口出错!", "Error", MB_OK); return -1; } /*显示窗口*/ ShowWindow(hwnd, nCmdShow); /*更新(绘制)窗口*/ UpdateWindow(hwnd); /*************第三步:消息循环*************/ while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); /*翻译消息*/ DispatchMessage(&msg); /*分派消息*/ } return msg.wParam; /*当GetMessage程序返回FALSE是程序结束*/ } /*************第四步:窗口过程*****************/ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch(message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); /*画圆角矩形,左上角坐标(50,50),右下角坐标(150,150),圆角宽10,高15*/ RoundRect(hdc, 50, 50, 150, 150, 10, 15); EndPaint(hwnd, &ps); return 0; /*窗口销毁消息*/ case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }
椭圆函数原型:
BOOL Ellipse(
HDC hdc, //设备环境句柄
int nLeftRect, //左上角x坐标
int nTopRect, //左上角y坐标
int nRightRect, //右下角x坐标
int nBottomRect //右下角y坐标
);
画直线:
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
MoveToEx(hdc, 150, 150, NULL); //设定起始点,不保存当前点坐标
LineTo(hdc,200, 60); //第一条线
LineTo(hdc, 250, 150); //第二条线
LineTo(hdc, 150, 150); //第三条线
EndPaint(hwnd, &ps);
return 0 ;