Direct2D API Reference

Welcome to the Direct2D API Reference. Direct2D is a 2D graphics API that provides high performance, hardware-accelerated, and easy-to-use functionality for 2D graphics and text rendering on Windows.

Concepts

Explore the fundamental concepts that underpin Direct2D rendering.

ID2D1Factory

HRESULT CreateFactory(
  D2D1_FACTORY_TYPE factoryType,
  REFIID riid,
  const D2D1_FACTORY_OPTIONS *pFactoryOptions,
  void **ppIFactory
);

Creates a Direct2D factory object, which is the starting point for using Direct2D.

factoryType
A value that specifies the threading mode of the factory. It can be D2D1_FACTORY_TYPE_SINGLE_THREADED or D2D1_FACTORY_TYPE_MULTI_THREADED.
riid
The GUID of the interface to be returned.
pFactoryOptions
Optional. Debug-level information about the factory.
ppIFactory
When this method returns, contains a pointer to the new factory object. This parameter is passed uninitialized.
Return Value
If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Example:


HRESULT hr = D2D1CreateFactory(
    D2D1_FACTORY_TYPE_SINGLE_THREADED,
    &hr);

if (SUCCEEDED(hr)) {
    // Factory created successfully
}
                        

ID2D1HwndRenderTarget

Represents a render target that is associated with a window (HWND).

Key methods include:

  • BeginDraw(): Prepares the render target for drawing operations.
  • EndDraw(): Completes the drawing operations and presents the results.
  • Clear(): Clears the entire render target.
  • DrawLine(), DrawRectangle(), DrawEllipse(): Methods for drawing basic geometric shapes.
  • DrawBitmap(): Draws a bitmap.
  • DrawText(): Draws formatted text.

Example: Drawing a rectangle


// Assuming pRenderTarget is a valid ID2D1HwndRenderTarget*
D2D1_RECT_F rect = D2D1::RectF(50.0f, 50.0f, 150.0f, 100.0f);
const ID2D1SolidColorBrush* pBrush;
pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Blue), &pBrush);

pRenderTarget->BeginDraw();
pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::LightGray));
pRenderTarget->DrawRectangle(rect, pBrush);
hr = pRenderTarget->EndDraw();
if (SUCCEEDED(hr)) {
    // Drawing complete
}
pBrush->Release();
                        

ID2D1SolidColorBrush

A brush that fills with a solid color.

Used for drawing lines, outlines, and filled shapes.

HRESULT CreateSolidColorBrush(
  D2D1_COLOR_F color,
  const D2D1_BRUSH_PROPERTIES *pDefaultBrushProperties,
  ID2D1SolidColorBrush **ppBrush
);

Creates a solid color brush.

ID2D1PathGeometry

Represents a complex geometric shape that can be composed of lines, curves, and arcs.

Use ID2D1Factory::CreatePathGeometry to create instances.

Interfaces

A list of key Direct2D interfaces and their purposes.

Structures

Data structures used by Direct2D APIs.

Enumerations

Enumerations used to specify options and states in Direct2D.

Functions

Key functions for initializing and managing Direct2D resources.