涉及函数(后续在函数板块补充):

  1. SetPixel
  2. MoveToEx
  3. LineTo
  4. GetCurrentPositionEx
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
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
int i, j;
POINT current_pos;
TCHAR szBuffer[128];

switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);

/* 第一种:靠循环在每个点位画点
for (i = rect.left; i < rect.right; ++i)
SetPixel(hdc, i, 100, RGB(255, 0, 0));*/

/* 第二种:使用API函数
MoveToEx(hdc, 100, 100, NULL);
LineTo(hdc, 600, 100);
GetCurrentPositionEx(hdc, &current_pos); // 获取当前绘点位置
StringCchPrintf(szBuffer, 128, TEXT("当前绘点位置为: (%d, %d)"), current_pos.x, current_pos.y);
DrawText(hdc, szBuffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);*/

/* 利用API函数设计网格
// 画横线
for (i = 0; i <= rect.bottom; i += 50)
{
MoveToEx(hdc, rect.left, i, NULL);
LineTo(hdc, rect.right, i);
}
// 画竖线
for (j = 0; j <= rect.right; j += 50)
{
MoveToEx(hdc, j, rect.top, NULL);
LineTo(hdc, j, rect.bottom);
}*/

// 画五角星, emm有点丑陋
MoveToEx(hdc, 300, 100, NULL);
LineTo(hdc, 200, 500);
LineTo(hdc, 500, 200);
LineTo(hdc, 100, 200);
LineTo(hdc, 400, 500);
LineTo(hdc, 300, 100);

EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0); // 正式关闭窗口
return 0;
}

return DefWindowProc(hwnd, message, wParam, lParam);
}