Direct2D API Reference

This section provides detailed information on the Direct2D interfaces, functions, structures, and enumerations used for hardware-accelerated 2D graphics rendering on Windows.

Interfaces

Direct2D uses COM interfaces to expose its functionality. Key interfaces include:

HRESULT D2D1CreateFactory( D2D1_FACTORY_TYPE factoryType, REFIID riid, CONST D2D1_FACTORY_OPTIONS *pFactoryOptions, void **ppvFactory );

D2D1CreateFactory

Creates an instance of the Direct2D factory. The factory is used to create other Direct2D objects, such as render targets, geometries, and brushes.

Parameters

  • factoryType: A D2D1_FACTORY_TYPE value that specifies the threading model of the factory.
  • riid: A reference to the GUID of the desired factory interface.
  • pFactoryOptions: A pointer to a D2D1_FACTORY_OPTIONS structure that contains optional features for the factory. Can be NULL.
  • ppvFactory: A pointer to a variable that receives a pointer to the new Direct2D factory.

Return Value

  • An HRESULT indicating whether the factory was created successfully.
void DrawLine( D2D1_POINT_2F point1, D2D1_POINT_2F point2, CONST D2D1_COLOR_F &color, FLOAT strokeWidth, CONST ID2D1StrokeStyle *strokeStyle );

ID2D1DeviceContext::DrawLine

Draws a line using the specified color and stroke style.

Parameters

  • point1: The starting point of the line.
  • point2: The ending point of the line.
  • color: The color of the line.
  • strokeWidth: The width of the line.
  • strokeStyle: An optional pointer to a stroke style object that defines the appearance of the line's stroke.
HRESULT CreateBitmap( D2D1_SIZE_U size, CONST void *pSourceData, UINT32 pitch, CONST D2D1_BITMAP_PROPERTIES &bitmapProperties, ID2D1Bitmap **ppBitmap );

ID2D1DeviceContext::CreateBitmap

Creates a Direct2D bitmap from source data.

Parameters

  • size: The dimensions of the bitmap.
  • pSourceData: Pointer to the bitmap data.
  • pitch: The byte pitch of the bitmap data.
  • bitmapProperties: Properties of the bitmap, including pixel format and DPI.
  • ppBitmap: Pointer to the created bitmap object.

Return Value

  • An HRESULT indicating success or failure.
void SetColor( CONST D2D1_COLOR_F &color );

ID2D1SolidColorBrush::SetColor

Sets the color of the solid color brush.

Parameters

  • color: The new color for the brush.

Functions

HRESULT D2D1CreateFactory( D2D1_FACTORY_TYPE factoryType, REFIID riid, CONST D2D1_FACTORY_OPTIONS *pFactoryOptions, void **ppvFactory );

D2D1CreateFactory

Creates an instance of the Direct2D factory. This is the entry point for using Direct2D.

Parameters

  • factoryType: A D2D1_FACTORY_TYPE value that specifies the threading model of the factory.
  • riid: A reference to the GUID of the desired factory interface (e.g., IID_ID2D1Factory).
  • pFactoryOptions: A pointer to a D2D1_FACTORY_OPTIONS structure that contains optional features for the factory. Can be NULL.
  • ppvFactory: A pointer to a variable that receives a pointer to the new Direct2D factory.

Return Value

  • An HRESULT indicating whether the factory was created successfully.

Structures

Commonly used structures include:

Enumerations

Key enumerations for configuring Direct2D objects: