GDI 边框绘制函数(8)

绘制矩形

调用 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 坐标
);

这三个函数使用起点和终点来控制绘图,这样程序员就可以无需自行计算精确的坐标,就能完成绘制工作。

LINEDEMO 示例程序

#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;
}
原文地址:https://www.cnblogs.com/yenyuloong/p/9121896.html