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:
- Enable MFC support for Direct2D. A good place for doing it is in WM_CREATE message handler.
int CDirectWriteDemoView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; EnableD2DSupport(); // Enable D2D support for this window return 0; }
- 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.
void CDirectWriteDemoView::OnDraw(CDC* /*pDC*/) { // only override the pure virtual method from base class; // the rendering is done in AFX_WM_DRAW2D registered message handler. }
- Map a handler function for AFX_WM_DRAW2D MFC registered message.
class CDirectWriteDemoView : public CView { // ... afx_msg LRESULT OnDraw2D(WPARAM wParam, LPARAM lParam); };
// ... BEGIN_MESSAGE_MAP(CDirectWriteDemoView, CView) // ... ON_REGISTERED_MESSAGE(AFX_WM_DRAW2D, &CDirectWriteDemoView::OnDraw2D) END_MESSAGE_MAP() // ...
- 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.
LRESULT CDirectWriteDemoView::OnDraw2D(WPARAM wParam, LPARAM lParam) { CHwndRenderTarget* pRenderTarget = (CHwndRenderTarget*)lParam; ASSERT_VALID(pRenderTarget); // clear the drawing area pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); // other drawing (e.g. an image) may come here... // ... // construct a CD2DTextFormat object which describes the font properties used to format text CD2DTextFormat textFormat(pRenderTarget, // pointer to the render target strFontFamilyName, // font family name fFontSize, // font size in DIP (1/96 inch) fontWeight, // font weight (normal, bold, etc.) fontStyle, // font style (normal, oblique or italic) fontStretch); // font stretch // construct a CD2DTextLayout object which represents a block of formatted text CD2DTextLayout textLayout(pRenderTarget, // pointer to the render target strText, // text to be drawn textFormat, // text format sizeTarget); // size of the layout box // finally, draw the text pRenderTarget->DrawTextLayout(CD2DPointF(5, 5), // top-left corner of the text &textLayout, // text layout object &CD2DSolidColorBrush // brush used for text (pRenderTarget, D2D1::ColorF(D2D1::ColorF::DarkBlue))); return 0; }
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 (17)
References and related articles
- MSDN: CD2DTextFormat Class
- MSDN: CD2DTextLayout Class
- MSDN: DirectWrite
- MSDN: Adding a D2D Object to an MFC Project
- MSDN: MFC Classes Added for Visual Studio 2010 SP1
- Codexpert: MFC Support for Direct2D