Graphics Device Interface (GDI)

The Graphics Device Interface (GDI) provides a rich set of functions for drawing graphical elements on Windows devices. This section covers the core GDI functions for creating and manipulating graphics objects.

Core GDI Functions

CreateSolidBrush

HBRUSH CreateSolidBrush(COLORREF color);

Creates a logical brush with the specified solid color. A brush is a drawing object used to fill areas, such as rectangles and ellipses, and to draw lines.

Return Value:
  • If the function succeeds, the return value is a handle to the newly created brush.
  • If the function fails, the return value is NULL.

Rectangle

BOOL Rectangle(HDC hdc, int nX1, int nY1, int nX2, int nY2);

Draws a rectangle using the current pen and fills it using the current brush. The rectangle's coordinates are defined by two opposing corners.

Parameters:
  • hdc: Handle to a device context.
  • nX1, nY1: Logical coordinates of the upper-left corner of the rectangle.
  • nX2, nY2: Logical coordinates of the lower-right corner of the rectangle.
Return Value:
  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero.

TextOut

BOOL TextOut(HDC hdc, int nXStart, int nYStart, LPCTSTR lpString, int cchString);

Writes a string of characters at the specified location in the device context. The characters are written using the currently selected font, background color, and text color.

Parameters:
  • hdc: Handle to a device context.
  • nXStart, nYStart: Logical coordinates of the starting position of the string.
  • lpString: Pointer to the string to be written.
  • cchString: Number of characters to write.
Return Value:
  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero.

Device Contexts

Device contexts (DCs) are essential for GDI operations. They represent a drawing surface and store information about the drawing attributes, such as the pen, brush, font, and drawing mode.

Drawing Primitives

GDI provides functions for drawing basic shapes like lines, rectangles, ellipses, and polygons.

Bitmaps and Image Manipulation

Work with bitmap images, including loading, displaying, and manipulating them.

Fonts and Text Rendering

Control font selection, text alignment, and rendering properties for displaying text.

Path Objects

Create and manipulate complex graphical paths for advanced drawing operations.