[toc]

在windows窗口程序中画立体图形

1.在windows窗口程序中画正方体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <windows.h>

// 绘制正方体的函数
void DrawCube(HDC hdc) {
// 设置正方体的参数
int size = 100; // 正方体的边长
int offsetX = 150; // 正方体的X坐标
int offsetY = 100; // 正方体的Y坐标

// 计算正方体的顶点
POINT front[4] = {
{ offsetX, offsetY }, // 左上
{ offsetX + size, offsetY }, // 右上
{ offsetX + size, offsetY + size }, // 右下
{ offsetX, offsetY + size } // 左下
};

POINT back[4] = {
{ offsetX + 30, offsetY - 30 }, // 左上
{ offsetX + size + 30, offsetY - 30 }, // 右上
{ offsetX + size + 30, offsetY + size - 30 }, // 右下
{ offsetX + 30, offsetY + size - 30 } // 左下
};

// 绘制前面(白色)
SelectObject(hdc, CreateSolidBrush(RGB(255, 255, 255))); // 白色
Polygon(hdc, front, 4);
DeleteObject(SelectObject(hdc, GetStockObject(BLACK_BRUSH))); // 恢复默认画刷

// 绘制后面(白色)
SelectObject(hdc, CreateSolidBrush(RGB(255, 255, 255))); // 白色
Polygon(hdc, back, 4);
DeleteObject(SelectObject(hdc, GetStockObject(BLACK_BRUSH))); // 恢复默认画刷

// 绘制可见边缘(实线)
HPEN hSolidPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); // 黑色实线
SelectObject(hdc, hSolidPen);

MoveToEx(hdc, front[0].x, front[0].y, NULL);
LineTo(hdc, front[1].x, front[1].y);
LineTo(hdc, front[2].x, front[2].y);
LineTo(hdc, front[3].x, front[3].y);

// 绘制不可见边缘(虚线)
HPEN hDashPen = CreatePen(PS_DASH, 1, RGB(0, 0, 0)); // 黑色虚线
SelectObject(hdc, hDashPen);

MoveToEx(hdc, front[0].x, front[0].y, NULL);
LineTo(hdc, back[0].x, back[0].y);
LineTo(hdc, back[1].x, back[1].y);
LineTo(hdc, front[1].x, front[1].y);
MoveToEx(hdc, front[2].x, front[2].y, NULL);
LineTo(hdc, back[2].x, back[2].y);
LineTo(hdc, back[3].x, back[3].y);
LineTo(hdc, front[3].x, front[3].y);

// 清理
DeleteObject(hSolidPen); // 删除实线画笔
DeleteObject(hDashPen); // 删除虚线画笔
}

// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

// 清除窗口背景
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

// 调用绘制正方体的函数
DrawCube(hdc);

EndPaint(hwnd, &ps);
} break;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}

// WinMain 函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
const char CLASS_NAME[] = "Cube Window Class";

WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

HWND hwnd = CreateWindowEx(
0, CLASS_NAME, "Draw a Cube",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
NULL, NULL, hInstance, NULL
);

ShowWindow(hwnd, nShowCmd);

// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

2.在windows窗口程序中画球体(GDI只能绘制2d图形不能绘制3D图形)