边框绘制函数

边框绘制函数

Rectangle矩形

使用该函数画一个矩形,可以用当前的画笔画矩形轮廓,用当前画刷进行填充。

1
BOOL Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

hdc:设备环境句柄。

nLeftRect:指定矩形左上角的逻辑X坐标。

nTopRect:指定矩形左上角的逻辑Y坐标。

nRightRect:指定矩形右下角的逻辑X坐标。

nBottomRect:指定矩形右下角的逻辑Y坐标。

返回值:如果函数调用成功,返回值非零,否则返回值为0。

Windows NT:若想获得更多错误信息,请调用GetLastError函数。

备注:此函数不使用和改变当前位置。

1
Rectangle(hdc,200,50,600,400);

Ellipse画圆或椭圆

1
2
3
4
5
6
BOOL Ellipse(HDC hdc,
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect
);

hdc:设备环境句柄。

nLeftRect:指定限定矩形左上角的X坐标。

nTopRect:指定限定矩形左上角的Y坐标。

nRightRect:指定限定矩形右下角的X坐标。

nBottomRect:指定限定矩形右下角的Y坐标。

如果函数调用成功,返回值非零;如果函数调用失败,返回值是0。

Windows NT:若想获得更多错误信息,请调用GetLastError函数。

1
Ellipse(hdc, 200, 50, 600, 400);

RoundRect带圆角的矩形

该函数画一个带圆角的矩形,此矩形由当前画笔画轮廓,由当前画刷填充。

1
BOOL RoundRect(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);

参数:

hdc:设备环境句柄。

nLeftRect:指定矩形左上角的X坐标。

nTopRect:指定矩形左上角的Y坐标。

nRightRect:指定矩形右下角的X坐标。

nbottomRect:指定矩形右下角的Y坐标。

nWidth:指定用来画圆角的椭圆的宽。

nHeight:指定用来画圆角的椭圆的高。

返回值:如果函数调用成功,则返回值非空,否则返回值是0。

Windows NT:若想获得更多的错误信息,请调用GetLastError函数。

1
RoundRect(hdc, 700, 50, 1100, 400,100,100);

ARC画圆弧

1
2
3
4
5
6
7
8
9
10
BOOL Arc(
HDC hdc,
int xLeft,
int yTop,
int xRight,
int yBottom,
int XStart,
int YStart,
int XEnd,
int YEnd);

hdc 绘画的窗口句柄

xLeft和nyTopt指定外接矩形左上角坐标

xRight和yBottom指定外接矩形右下角坐标

xStart和yStart指定圆弧开始坐标

xEnd和nyEnd指定圆弧结束坐标

1
Arc(hdc, 850, 500, 1000, 650, 20, 600, 1200, 400);

Chord画弦

1
2
3
4
BOOL Chord(
DC: HDC; {设备环境句柄}
int X1, Y1, X2, Y2, X3, Y3, X4, Y4 ; {四个坐标点}
);

参数表

X1,Y1 ---------- Long,指定围绕椭圆的一个矩形的左上角位置

X2,Y2 ---------- Long,指定围绕椭圆的一个矩形的右下角位置

X3,Y3 ---------- Long,指定与椭圆相交的一条线的一个点

X4,Y4 ---------- Long,指定与椭圆相交的一条线的另一个点

返回值

Long,非零表示成功,零表示失败。会设置GetLastError

1
Chord(hdc, 450,500,800,850, 20, 600, 1200, 400);

Pie画圆饼

1
BOOL Pie(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2);

hdc:设备环境句柄。

nLeftRect:指定限定矩形左上角的X坐标。

nTopRect:指定限定矩形左上角的Y坐标。

nRigthRect:指定限定矩形右下角的X坐标。

nBottomRect:指定限定矩形右下角的Y坐标。

nXRadial1:指定第一条半径的端点的X坐标。

nYRadial1:指定第一条半径的端点的Y坐标。

nXRadial2:指定第二条半径的端点的X坐标。

nYRadial2:指定第二条半径的端点的Y坐标。

返回值:如果函数调用成功,返回值非零;如果函数调用失败,返回值是0。

Windows:要得到更多的错误信息,调用GetLastError。

1
Pie(hdc,50,500,400,850,500,0,50,650);

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 边框绘制函数.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "边框绘制函数.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: 在此放置代码。

// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));

MSG msg;

// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MY);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassExW(&wcex);
}

//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW ,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), nullptr, nullptr, hInstance, nullptr);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
//画矩形
Rectangle(hdc,200,50,600,400);
//画椭圆或正圆
Ellipse(hdc, 200, 50, 600, 400);
//画带圆角的矩形
RoundRect(hdc, 700, 50, 1100, 400,100,100);

//画弦
Chord(hdc, 450,500,800,850, 20, 600, 1200, 400);
//画弧
Arc(hdc, 850, 500, 1000, 650, 20, 600, 1200, 400);
//画圆饼
Pie(hdc,50,500,400,850,500,0,50,650);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}