Direct2D 1.1 API Reference - D2D1

This section provides detailed reference information for the Direct2D 1.1 API. Direct2D is a hardware-accelerated, immediate-mode 2D graphics API that provides high performance for 2D rendering and processing of graphics primitives and bitmaps.

ID2D1Factory

Represents a factory for creating Direct2D objects.

Methods

CreateRender tảrget

virtual HRESULT CreateRenderTarget(
  _In_ D2D1_RENDER_TARGET_TYPE type,
  _In_ const D2D1_RENDER_TARGET_PROPERTIES &renderTargetProperties,
  _Outptr_ ID2D1RenderTarget **renderTarget
) = 0;

Parameters

  • type: A value that specifies how the render target is created.
  • renderTargetProperties: A structure that contains the render target's properties.
  • renderTarget: A pointer to a location that receives a pointer to the new render target.

Return Value

If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

This method creates a Direct2D render target that you can use to draw primitives, text, and bitmaps.

Example

ID2D1Factory* pFactory = NULL;
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);

D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties();
ID2D1HwndRenderTarget* pHwndRenderTarget = NULL;
pFactory->CreateHwndRenderTarget(
    props,
    D2D1::HwndRenderTargetProperties(hWnd, D2D1::SizeU(width, height)),
    &pHwndRenderTarget
);

// ... use pHwndRenderTarget to draw ...

if (pHwndRenderTarget) pHwndRenderTarget->Release();
if (pFactory) pFactory->Release();

CreatePathGeometry

virtual HRESULT CreatePathGeometry(
  _Outptr_ ID2D1PathGeometry **pathGeometry
) = 0;

Parameters

  • pathGeometry: A pointer to a location that receives a pointer to the new path geometry.

Return Value

If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

Creates an empty path geometry object that can be populated by using its sink interface.

ID2D1Geometry

The base interface for all geometry objects in Direct2D.

Methods

GetBounds

virtual HRESULT GetBounds(
  _In_ const D2D1_MATRIX_3X2_F &worldTransform,
  _Out_ D2D1_RECT_F *rect
) const = 0;

Parameters

  • worldTransform: The world transform to apply to the geometry.
  • rect: A pointer to a RECT_F structure that receives the geometry's bounds.

Return Value

If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

Retrieves the bounds of the geometry after applying the specified world transform.

ID2D1PathGeometry

Represents a complex shape made up of lines, curves, and arcs.

Methods

Open

virtual HRESULT Open(
  _Outptr_ ID2D1GeometrySink **geometrySink
) = 0;

Parameters

  • geometrySink: A pointer to a location that receives a pointer to the geometry sink.

Return Value

If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

Obtains a geometry sink that can be used to define the geometry's shape.

ID2D1GeometrySink

An interface used to define a path geometry.

Methods

BeginFigure

virtual void BeginFigure(
  _In_ const D2D1_POINT_2F &startPoint,
  _In_ D2D1_FIGURE_BEGIN figureBegin = D2D1_FIGURE_BEGIN_REQUIRE
) = 0;

Parameters

  • startPoint: The starting point of the figure.
  • figureBegin: A value that specifies whether the figure is open or closed.

Remarks

Begins a new figure in the path geometry.

AddLines

virtual void AddLines(
  _In_reads_(pointsCount) const D2D1_POINT_2F *points,
  _In_ UINT32 pointsCount
) = 0;

Parameters

  • points: An array of points that define the lines to add.
  • pointsCount: The number of points in the array.

Remarks

Adds a series of straight line segments to the geometry sink.

ID2D1RenderTarget

Represents the drawing surface for Direct2D.

Methods

DrawGeometry

void DrawGeometry(
  _In_ ID2D1Geometry *geometry,
  _In_ ID2D1Brush *brush,
  _In_ FLOAT strokeWidth = 1.0f,
  _In_opt_ ID2D1StrokeStyle *strokeStyle = NULL
);

Parameters

  • geometry: The geometry to draw.
  • brush: The brush to use for drawing the geometry's outline.
  • strokeWidth: The width of the geometry's outline.
  • strokeStyle: An optional stroke style to apply to the geometry's outline.

Remarks

Draws the outline of a geometry.

FillGeometry

void FillGeometry(
  _In_ ID2D1Geometry *geometry,
  _In_ ID2D1Brush *brush
);

Parameters

  • geometry: The geometry to fill.
  • brush: The brush to use for filling the geometry.

Remarks

Fills the interior of a geometry.

ID2D1Brush

An abstract interface for objects that paint areas in Direct2D.

Derived Classes

ID2D1SolidColorBrush

A brush that paints with a single, solid color.

Methods

SetColor

void SetColor(
  _In_ const D2D1_COLOR_F &color
);

Parameters

  • color: The color to set for the brush.

Remarks

Sets the color of the solid color brush.