Graphics Device Interface (GDI)

The Graphics Device Interface (GDI) provides a device-independent way to represent graphical output. It allows applications to draw lines, curves, text, and graphics primitives on various output devices, such as screens and printers, without needing to know the specific details of the underlying hardware.

Key Concepts

Understanding GDI involves familiarizing yourself with several core concepts:

Core Functions

Below is a list of commonly used GDI functions. Click on a function name for detailed documentation.

CreatePen

HPEN WINAPI CreatePen(int fnPenStyle, int nWidth, COLORREF crColor);

Creates a new pen with the specified style, width, and color.

Parameters:
  • fnPenStyle: The pen style. Can be PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT, PS_NULL, or PS_GEOMETRIC.
  • nWidth: The width of the pen in logical units.
  • crColor: The RGB color of the pen.
Return Value:

If the function succeeds, the return value is a handle to the newly created pen object. If the function fails, the return value is NULL.

Rectangle

BOOL WINAPI Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

Draws a rectangle using the current pen and fills it using the current brush.

Parameters:
  • hdc: Handle to the device context.
  • nLeftRect: The x-coordinate of the upper-left corner of the rectangle.
  • nTopRect: The y-coordinate of the upper-left corner of the rectangle.
  • nRightRect: The x-coordinate of the lower-right corner of the rectangle.
  • nBottomRect: The y-coordinate 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.

Remarks:

The rectangle is drawn with the current pen and filled with the current brush. If the pen is a cosmetic pen, the rectangle outline is drawn using the current color. If the pen is a geometric pen, the rectangle outline is drawn using the pen's attributes. If the brush is NULL, the rectangle is not filled.

See Also:

TextOut

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

Writes a string of text at the specified location.

Parameters:
  • hdc: Handle to the device context.
  • nXStart: The x-coordinate of the starting position of the string.
  • nYStart: The y-coordinate of the starting position of the string.
  • lpString: Pointer to the string to be written.
  • nCount: The number of characters in the string.
Return Value:

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Note: GDI functions often require a valid Device Context (HDC). You can obtain an HDC for a window using GetDC or for the entire screen using GetDC(NULL). Remember to release the DC when you are finished using ReleaseDC.

Related Topics