GDI Documentation

Rectangle Function (GDI)

The Rectangle function draws a rectangle using the current pen and brush. It can be used to render simple shapes or as the basis for more complex drawings.

Syntax

C++
BOOL Rectangle(
    HDC   hdc,
    int   left,
    int   top,
    int   right,
    int   bottom
);

Parameters

Return Value

If the function succeeds, the return value is non‑zero. If it fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Example

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow)
{
    HWND hwnd = CreateWindowA("STATIC","Rectangle Demo",
        WS_OVERLAPPEDWINDOW|WS_VISIBLE,
        100,100,400,300,
        NULL,NULL,hInst,NULL);

    HDC hdc = GetDC(hwnd);
    SelectObject(hdc, GetStockObject(BLACK_PEN));
    SelectObject(hdc, GetStockObject(LTGRAY_BRUSH));
    Rectangle(hdc, 50, 50, 250, 150);
    ReleaseDC(hwnd, hdc);
    MSG msg;
    while (GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

For more examples, see the GDI Samples page.