调用 Rectangle 函数可以绘制一个矩形(它将填充这个矩形):
BOOL Rectangle(
HDC hdc, // 设备环境句柄
int nLeftRect, // 左边线的位置
int nTopRect, // 上边线的位置
int nRightRect, // 右边线的位置
int nBottomRect // 下边线的位置
);
调用 Ellipse 函数可以绘制一个椭圆,它和绘制矩形的参数相同:
BOOL Ellipse(
HDC hdc, // 设备环境句柄
int nLeftRect, // 左边线的位置
int nTopRect, // 上边线的位置
int nRightRect, // 右边线的位置
int nBottomRect // 下边线的位置
);
调用 RoundRect 函数可以绘制一个圆角矩形,它的边框与前面两个相同,并且还需要两个参数:
BOOL RoundRect(
HDC hdc, // 设备环境句柄
int nLeftRect, // 左边线的位置
int nTopRect, // 上边线的位置
int nRightRect, // 右边线的位置
int nBottomRect, // 下边线的位置
int nWidth, // 圆角上的小椭圆的宽度
int nHeight // 圆角上的小椭圆的高度
);
分别调用 Arc、Chord、Pie 函数,可以绘制弧线、弓形和扇形,这三个函数参数相同:
BOOL Arc(
HDC hdc, // 设备环境句柄
int nLeftRect, // 左边线的位置
int nTopRect, // 上边线的位置
int nRightRect, // 右边线的位置
int nBottomRect, // 下边线的位置
int nXStartArc, // 起始点 x 坐标
int nYStartArc, // 起始点 y 坐标
int nXEndArc, // 终点 x 坐标
int nYEndArc // 终点 y 坐标
);
BOOL Chord(
HDC hdc, // 设备环境句柄
int nLeftRect, // 上边线的位置
int nTopRect, // 上边线的位置
int nRightRect, // 右边线的位置
int nBottomRect, // 下边线的位置
int nXRadial1, // 起始点 x 坐标
int nYRadial1, // 起始点 y 坐标
int nXRadial2, // 终点 x 坐标
int nYRadial2 // 终点 y 坐标
);
BOOL Pie(
HDC hdc, // 设备环境句柄
int nLeftRect, // 左边线的位置
int nTopRect, // 上边线的位置
int nRightRect, // 右边线的位置
int nBottomRect, // 下边线的位置
int nXRadial1, // 起始点 x 坐标
int nYRadial1, // 起始点 y 坐标
int nXRadial2, // 终点 x 坐标
int nYRadial2 // 终点 y 坐标
);
这三个函数使用起点和终点来控制绘图,这样程序员就可以无需自行计算精确的坐标,就能完成绘制工作。
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
static int cxClient, cyClient;
switch (message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
Rectangle(hdc, cxClient / 8, cyClient / 8, cxClient * 7 / 8, cyClient * 7 / 8);
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, cxClient, cyClient);
MoveToEx(hdc, 0, cyClient, NULL);
LineTo(hdc, cxClient, 0);
Ellipse(hdc, cxClient / 8, cyClient / 8, cxClient * 7 / 8, cyClient * 7 / 8);
RoundRect(hdc, cxClient / 4, cyClient / 4, cxClient * 3 / 4, cyClient * 3 / 4, cxClient / 4, cyClient / 4);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
LPCTSTR lpszClassName = TEXT("LineDemo");
LPCTSTR lpszWindowName = TEXT("LineDemo Program");
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = lpszClassName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("This program requires Windows NT!"), lpszWindowName, MB_ICONERROR);
return 0;
}
HWND hwnd = CreateWindow(
lpszClassName,
lpszWindowName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
🖊️ 本文由 Alone Café 创作,如果您觉得本文让您有所收获,请随意赞赏 🥺
⚖️ 本文以 CC BY-NC-SA 4.0,即《署名-非商业性使用-相同方式共享 4.0 国际许可协议》进行许可
👨⚖️ 本站所发表的文章除注明转载或出处外,均为本站作者原创或翻译,转载前请务必署名并遵守上述协议
🔗 原文链接:https://alone.cafe/2018/06/gdibian-kuang-hui-zhi-han-shu
📅 最后更新:2018年06月01日 Friday 07:32
Update your browser to view this website correctly. Update my browser now