Article From:https://www.cnblogs.com/yenyuloong/p/9121896.html

draw rectangle

Invoking the Rectangle function can draw a rectangle (it will fill the rectangle):

BOOL Rectangle(
    HDC hdc,         // Device environment handle
    int nLeftRect,   // The position of the left line
    int nTopRect,    // The position of the upper line
    int nRightRect,  // The position of the right line
    int nBottomRect  // The position of the lower line
);

Draw the ellipse

Calling the Ellipse function can draw an ellipse, which is the same as the rectangle parameter.

BOOL Ellipse(
    HDC hdc,         // Device environment handle
    int nLeftRect,   // The position of the left line
    int nTopRect,    // The position of the upper line
    int nRightRect,  // The position of the right line
    int nBottomRect  // The position of the lower line
);

Draw a rounded rectangle

The RoundRect function can draw a rounded rectangle with the same border as the first two, and it needs two parameters.

BOOL RoundRect(  
    HDC hdc,         // Device environment handle
    int nLeftRect,   // The position of the left line
    int nTopRect,    // The position of the upper line
    int nRightRect,  // The position of the right line
    int nBottomRect, // The position of the lower line
    int nWidth,      // The width of a small ellipse on a round angle
    int nHeight      // The height of a small ellipse on a round angle
);

Draw a curve, a bow, a fan

The functions of Arc, Chord and Pie are respectively called, and arc, bow and fan can be drawn. These three functions have the same parameters.

BOOL Arc(
    HDC hdc,         // Device environment handle
    int nLeftRect,   // The position of the left line
    int nTopRect,    // The position of the upper line
    int nRightRect,  // The position of the right line
    int nBottomRect, // The position of the lower line
    int nXStartArc,  // Starting point x coordinates
    int nYStartArc,  // Starting point y coordinates
    int nXEndArc,    // Terminal x coordinates
    int nYEndArc     // Terminal y coordinates
);
BOOL Chord(
    HDC hdc,         // Device environment handle
    int nLeftRect,   // The position of the upper line
    int nTopRect,    // The position of the upper line
    int nRightRect,  // The position of the right line
    int nBottomRect, // The position of the lower line
    int nXRadial1,   // Starting point x coordinates
    int nYRadial1,   // Starting point y coordinates
    int nXRadial2,   // Terminal x coordinates
    int nYRadial2    // Terminal y coordinates
);
BOOL Pie(
    HDC hdc,         // Device environment handle
    int nLeftRect,   // The position of the left line
    int nTopRect,    // The position of the upper line
    int nRightRect,  // The position of the right line
    int nBottomRect, // The position of the lower line
    int nXRadial1,   // Starting point x coordinates
    int nYRadial1,   // Starting point y coordinates
    int nXRadial2,   // Terminal x coordinates
    int nYRadial2    // Terminal y coordinates
);

These three functions use the starting point and the end point to control the drawing, so that the programmer can complete the drawing without calculating the precise coordinates.

LINEDEMO Sample program

#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;
}

Similar Posts:

Link of this Article: GDI border drawing function (8)

Leave a Reply

Your email address will not be published. Required fields are marked *