Overview
The Graphics Device Interface (GDI) provides a set of functions for drawing graphics and formatted text on Windows-based displays and printers. GDI enables device‑independent representation of graphical objects, allowing applications to create consistent output across a wide variety of hardware.
Commonly Used Functions
| Function | Purpose |
|---|---|
CreateCompatibleDC | Create a memory device context compatible with a specified device. |
BitBlt | Perform a bit block transfer of color data between device contexts. |
SelectObject | Select a GDI object (pen, brush, bitmap, etc.) into a device context. |
SetPixelV | Set the color of a single pixel. |
GetDeviceCaps | Retrieve device-specific information. |
Sample Code
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HDC hdc = GetDC(NULL);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP bmp = CreateCompatibleBitmap(hdc, 200, 200);
SelectObject(memDC, bmp);
// Fill background
HBRUSH brush = CreateSolidBrush(RGB(255,255,255));
FillRect(memDC, &(RECT){0,0,200,200}, brush);
DeleteObject(brush);
// Draw a red ellipse
HPEN pen = CreatePen(PS_SOLID, 2, RGB(255,0,0));
SelectObject(memDC, pen);
Ellipse(memDC, 20,20,180,180);
DeleteObject(pen);
// Blit to screen
BitBlt(hdc, 100, 100, 200, 200, memDC, 0,0, SRCCOPY);
ReleaseDC(NULL, hdc);
DeleteDC(memDC);
DeleteObject(bmp);
return 0;
}
For full documentation, see the individual pages linked in the sidebar.