绘图效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9982b624410ec64ddc6841a6f36a54a1.png)
代码及注释说明
#include<windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
HPEN hPen;
HBRUSH hBrush;
PAINTSTRUCT PtStr;
POINT points[6] = { {100,212},{70,227},{70,250},{130,250},{130,227},{100,212} };
switch (message)
{
case WM_PAINT:
hDC = BeginPaint(hwnd, &PtStr);
hPen = (HPEN)GetStockObject(NULL_PEN);
hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hDC, hPen);
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);
Rectangle(hDC, 213, 212, 287, 250);
RoundRect(hDC, 213, 100, 287, 137, 20, 20);
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hwnd, &PtStr);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
char lpszClassName[] = "Paint";
wndclass.style = 0;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = lpszClassName;
wndclass.lpszMenuName = 0;
return RegisterClass(&wndclass);
}
BOOL InitWindow(HINSTANCE hInstance, int nCmdShow)
{
HWND hwnd;
char lpszTitleName[] = "PaintingTool";
hwnd = CreateWindow(
"Paint",
lpszTitleName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd)
return FALSE;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
if (!InitWindowsClass(hThisInst)) return FALSE;
if (!InitWindow(hThisInst, nCmdShow)) return FALSE;
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}