Direct2D Documentation

Welcome to the official Microsoft Developer Network (MSDN) documentation for Direct2D. Direct2D is a 2D graphics API that provides high performance, hardware-accelerated, and general-purpose 2D rendering that integrates with Windows. It's designed for 2D graphics, including UI elements, documents, and media.

Key Concepts

Direct2D provides a rich set of features for rendering 2D graphics. Here are some fundamental concepts you'll encounter:

  • ID2D1Factory: The starting point for all Direct2D operations. You use it to create other Direct2D resources.
  • ID2D1HwndRenderTarget: A render target that draws directly to a window.
  • Geometric Primitives: Drawing shapes like lines, rectangles, ellipses, and paths.
  • Bitmaps and Textures: Rendering images and using textures for advanced effects.
  • Brushes: Tools for filling shapes and text with solid colors, gradients, or bitmaps.
  • Text Rendering: High-quality text rendering with DirectWrite integration.
  • Transformations: Applying transformations like translation, scaling, and rotation to graphics.

Getting Started

To begin using Direct2D, you'll typically need to:

  1. Initialize Direct2D: Create an instance of ID2D1Factory.
  2. Create a Render Target: Typically an ID2D1HwndRenderTarget for drawing to a window.
  3. Define Resources: Create brushes, geometries, and other resources.
  4. Begin Drawing: Call BeginDraw on the render target.
  5. Draw Graphics: Use the factory and render target to draw primitives, text, and images.
  6. End Drawing: Call EndDraw to present the rendered content.

Core APIs

Here's a quick reference to some of the most frequently used Direct2D interfaces and their primary functions:

Interface Description
ID2D1Factory Creates Direct2D objects like render targets, brushes, and geometries.
ID2D1RenderTarget Base interface for drawing surfaces.
ID2D1HwndRenderTarget Render target associated with a window.
ID2D1Brush Base interface for brushes used to fill shapes and text.
ID2D1SolidColorBrush A brush that fills with a single color.
ID2D1Geometry Represents geometric shapes.
ID2D1PathGeometry Represents complex paths.

Code Example: Drawing a Rectangle

Here's a simple C++ snippet demonstrating how to draw a filled rectangle using Direct2D:


#include <d2d1.h>

// Assume pRenderTarget is a valid ID2D1HwndRenderTarget*
// Assume pBlackBrush is a valid ID2D1SolidColorBrush* with D2D1::ColorF(D2D1::ColorF::Black)

D2D1_RECT_F rect = D2D1::RectF(50.0f, 50.0f, 150.0f, 100.0f);
pRenderTarget->FillRectangle(rect, pBlackBrush);
                

Further Resources