MFC Support for DirectWrite – Part 1: The Basics

A previous article shows how easy is to make an image viewer with MFC by enabling Direct2D support and using MFC wrappers over Direct2D interfaces. Now let’s discover MFC wrapper classes for DirectWrite.

A simple example of drawing texts by using MFC DirectWrite wrapper classes

Basically, must follow these steps:

  1. Enable MFC support for Direct2D. A good place for doing it is in WM_CREATE message handler.
  2. If the view class is derived from CView, override CView::OnDraw pure virtual method. Do nothing inside it, because all drawing will be done in AFX_WM_DRAW2D message handler.
  3. Map a handler function for AFX_WM_DRAW2D MFC registered message.

  4. In AFX_WM_DRAW2D message handler implementation, get the CHwndRenderTarget pointer which comes in lParam then use CD2DTextFormat and CD2DTextLayout to format and draw the text.

Notes

  • More details can be found in the demo application attached to this article.
  • DirectWrite has more capabilities like for example formatting a particular range in text and applying special efects; I will present them in a future article.
  • Minimum required Visual Studio version is 2010 SP1.
  • Minimum required operating systems are:
    • Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista;
    • Windows Server 2008 R2, Windows Server 2008 with SP2 and Platform Update for Windows Server 2008.

Demo application

Download: MFC Support for DirectWrite Demo.zip (828)

MFC Support for DirectWrite - Demo Application.jpg

MFC Support for DirectWrite – Demo Application.jpg

References and related articles

Leave a Reply