ID2D1Factory::CreateDCRenderTarget method (d2d1.h)

Creates a render target that draws to a Windows Graphics Device Interface (GDI) device context.

Syntax

HRESULT CreateDCRenderTarget(
  [in]  const D2D1_RENDER_TARGET_PROPERTIES *renderTargetProperties,
  [out] ID2D1DCRenderTarget                 **dcRenderTarget
);

Parameters

[in] renderTargetProperties

Type: const D2D1_RENDER_TARGET_PROPERTIES*

The rendering mode, pixel format, remoting options, DPI information, and the minimum DirectX support required for hardware rendering. To enable the device context (DC) render target to work with GDI, set the DXGI format to DXGI_FORMAT_B8G8R8A8_UNORM and the alpha mode to D2D1_ALPHA_MODE_PREMULTIPLIED or D2D1_ALPHA_MODE_IGNORE. For more information about pixel formats, see Supported Pixel Formats and Alpha Modes.

[out] dcRenderTarget

Type: ID2D1DCRenderTarget**

When this method returns, dcRenderTarget contains the address of the pointer to the ID2D1DCRenderTarget created by the method.

Return value

Type: HRESULT

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

Remarks

Before you can render with a DC render target, you must use the render target's BindDC method to associate it with a GDI DC. Do this for each different DC and whenever there is a change in the size of the area you want to draw to.

To enable the DC render target to work with GDI, set the render target's DXGI format to DXGI_FORMAT_B8G8R8A8_UNORM and alpha mode to D2D1_ALPHA_MODE_PREMULTIPLIED or D2D1_ALPHA_MODE_IGNORE.

Your application should create render targets once and hold on to them for the life of the application or until the render target's EndDraw method returns the D2DERR_RECREATE_TARGET error. When you receive this error, recreate the render target (and any resources it created).

Examples

The following code creates a DC render target.

// Create a DC render target.
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
    D2D1_RENDER_TARGET_TYPE_DEFAULT,
    D2D1::PixelFormat(
        DXGI_FORMAT_B8G8R8A8_UNORM,
        D2D1_ALPHA_MODE_IGNORE),
    0,
    0,
    D2D1_RENDER_TARGET_USAGE_NONE,
    D2D1_FEATURE_LEVEL_DEFAULT
    );

hr = m_pD2DFactory->CreateDCRenderTarget(&props, &m_pDCRT);

In the preceding code, m_pD2DFactory is a pointer to an ID2D1Factory, and m_pDCRT is a pointer to an ID2D1DCRenderTarget.

The next code example binds a DC to the ID2D1DCRenderTarget.

HRESULT DemoApp::OnRender(const PAINTSTRUCT &ps)
{

// Get the dimensions of the client drawing area.
GetClientRect(m_hwnd, &rc);

// Bind the DC to the DC render target.
hr = m_pDCRT->BindDC(ps.hdc, &rc);

Requirements

Requirement Value
Minimum supported client Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2008 R2, Windows Server 2008 with SP2 and Platform Update for Windows Server 2008 [desktop apps | UWP apps]
Target Platform Windows
Header d2d1.h
Library D2d1.lib
DLL D2d1.dll

See also

Direct2D and GDI Interoperation Overview

ID2D1Factory

Supported Pixel Formats and Alpha Modes