/ 导入必要的依赖项
#include <windows.h>
// 全局变量用于存储当前选择的图形类型
int currentShape = 0;
// 函数声明
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void DrawSquare(HDC hdc);
void DrawRectangle(HDC hdc);
void DrawIsoscelesTriangle(HDC hdc);
void DrawCircle(HDC hdc);
// 主函数入口点
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
// 注册窗口类
const char CLASS_NAME[] = "Drawing Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0, // 扩展样式
CLASS_NAME, // 窗口类名
"简单画图工具", // 窗口标题
WS_OVERLAPPEDWINDOW, // 窗口风格
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, // 位置和大小
NULL, // 父窗口句柄
NULL, // 菜单句柄
hInstance, // 实例句柄
NULL // 附加参数
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
// 消息循环
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static bool isDrawing = false;
HDC hdc;
PAINTSTRUCT ps;
switch (uMsg) {
case WM_KEYDOWN:
switch ((int)wParam) {
case '1':
currentShape = 1;
InvalidateRect(hwnd, NULL, TRUE);
break;
case '2':
currentShape = 2;
InvalidateRect(hwnd, NULL, TRUE);
break;
case '3':
currentShape = 3;
InvalidateRect(hwnd, NULL, TRUE);
break;
case '4':
currentShape = 4;
InvalidateRect(hwnd, NULL, TRUE);
break;
default:
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
RECT clientRect;
GetClientRect(hwnd, &clientRect);
switch (currentShape) {
case 1:
DrawSquare(hdc);
break;
case 2:
DrawRectangle(hdc);
break;
case 3:
DrawIsoscelesTriangle(hdc);
break;
case 4:
DrawCircle(hdc);
break;
default:
break;
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
// 绘制正方形
void DrawSquare(HDC hdc) {
RECT clientRect;
GetClientRect(NULL, &clientRect); // 获取客户区大小
int side = min(clientRect.right, clientRect.bottom) / 2;
int left = (clientRect.right - side) / 2;
int top = (clientRect.bottom - side) / 2;
Rectangle(hdc, left, top, left + side, top + side);
}
// 绘制长方形
void DrawRectangle(HDC hdc) {
RECT clientRect;
GetClientRect(NULL, &clientRect); // 获取客户区大小
int width = clientRect.right / 2;
int height = clientRect.bottom / 3;
int left = (clientRect.right - width) / 2;
int top = (clientRect.bottom - height) / 2;
Rectangle(hdc, left, top, left + width, top + height);
}
// 绘制等腰三角形
void DrawIsoscelesTriangle(HDC hdc) {
RECT clientRect;
GetClientRect(NULL, &clientRect); // 获取客户区大小
int base = clientRect.right / 2;
int height = clientRect.bottom / 4;
POINT points[3];
points[0].x = (clientRect.right - base) / 2;
points[0].y = clientRect.bottom - height;
points[1].x = (clientRect.right + base) / 2;
points[1].y = clientRect.bottom - height;
points[2].x = clientRect.right / 2;
points[2].y = clientRect.bottom - (height * 2);
Polygon(hdc, points, 3);
}
// 绘制圆形
void DrawCircle(HDC hdc) {
RECT clientRect;
GetClientRect(NULL, &clientRect); // 获取客户区大小
int radius = min(clientRect.right, clientRect.bottom) / 4;
int centerX = clientRect.right / 2;
int centerY = clientRect.bottom / 2;
Ellipse(hdc, centerX - radius, centerY - radius, centerX + radius, centerY + radius);
}