#include <windows.h>
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);
long WINAPI WndProc(HWND hwnd, UINT iMessage, UINT wParam, LONG lParam);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow
)
{
MSG Msg;
if (!InitWindowsClass(hInstance)) return FALSE;
//创口类的定义
if (!InitWindows(hInstance, nCmdShow)) return FALSE;
while (GetMessage(&Msg, 0, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
long WINAPI WndProc(HWND hwnd, UINT iMessage, UINT wParam, LONG lParam)
{
HDC hdc; //定义设备环境句柄
HBRUSH hBrush; //定义画刷句柄
HPEN hPen; //定义画笔句柄
PAINTSTRUCT PtStr; //定义指向包含绘图信息的结构体变量
POINT points[6] = {
{100,212},{70,227},{70,250},{130,250},{130,227},{100,212}
}; //初始点又来一次,为了图形闭合,为了放多边形的点
//定义一个POINT数组,包含6个点
switch (iMessage) //处理消息
{
case WM_PAINT: //处理绘图消息
hdc = BeginPaint(hwnd, &PtStr);
hPen = (HPEN)GetStockObject(NULL_PEN);
//必须强制转换,否则不知道是画笔还是画刷
SelectObject(hdc, hPen);//把画笔选入设备环境
hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
//获取系统定义的画刷
SelectObject(hdc, hBrush);//把画笔选入设备环境
LineTo(hdc, 50, 50); //画线,又因为是空笔所以只是把初始点移动而已
DeleteObject(hPen);
hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
//创建画笔
SelectObject(hdc, hPen);
//选入设备环境
LineTo(hdc, 150, 50);
LineTo(hdc, 100, 137);
LineTo(hdc, 50, 50);
//画一个三角形
Polyline(hdc, points, 6);
//画五边形
Arc(hdc, 63, 137, 138, 212, 100, 137, 100, 137);
//画一个圆
Pie(hdc, 213, 137, 288, 212, 240, 137, 260, 137);
//画一个圆饼
RoundRect(hdc, 213, 100, 287, 137, 20, 20);
//画一个圆角长方形
DeleteObject(hPen);
//删除画笔
DeleteObject(hBrush);
//删除画刷
EndPaint(hwnd, &PtStr);
//结束绘图
return 0;
case WM_DESTROY: //结束应用程序
PostQuitMessage(0);
return 0;
default: //其他消息处理程序
return DefWindowProc(hwnd, iMessage, wParam, lParam);
}
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
//定义窗口类
WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
//不加强制转换,系统不知道获取的是笔还是画刷
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, "END");
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = "WinFill";
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpszMenuName = NULL;
return RegisterClass(&WndClass);
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
//创建窗口
HWND hwnd;
//指向窗口的句柄
hwnd = CreateWindow(
"WinFill",
"填充示例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd) //判断窗口创建成功与否
return FALSE;
ShowWindow(hwnd, nCmdShow); //显示窗口
UpdateWindow(hwnd);
return TRUE;
}