MFC Static Control Using Direct2D and DirectWrite (updated)

I have update the MFC static control presented in the previous articles by adding methods for setting text range color, typography and inline images.

Code samples

Setting text range color

void CDemoDlg::_DoDemo_Effects()

    const CString strTagRed = _T("RED");
    const CString strTagGreen = _T("GREEN");
    const CString strTagBlue = _T("BLUE");

    CString strText;
    strText.Format(_T("IDWriteTextLayout::SetDrawingEffect can change the color")
        _T(" of a text range as for example: \n\t%s\n\t%s\n\t%s"),
        strTagRed, strTagGreen, strTagBlue);


    // set text range colors
    m_staticDemo.SetTextColor(strText.Find(strTagRed), strTagRed.GetLength(), RGB(255,0,0));
    m_staticDemo.SetTextColor(strText.Find(strTagGreen), strTagGreen.GetLength(), RGB(0, 255, 0));
    m_staticDemo.SetTextColor(strText.Find(strTagBlue), strTagBlue.GetLength(), RGB(0, 0, 255));

Setting text range typography

void CDemoDlg::_DoDemo_Typography()

    const CString strTagFontTypography = _T("Fancy Typography Rendering");

    CString strText;
    strText.Format(_T("Some OpenType fonts (e.g. Microsoft’s Gabriola) support %s"), 

    const UINT32 nStartPos = strText.Find(strTagFontTypography);
    const UINT32 nLength = strTagFontTypography.GetLength();

    // set an OpenType font which supports typography features
    m_staticDemo.SetFontFamilyName(nStartPos, nLength, _T("Gabriola"));
    // set feature
    m_staticDemo.SetFontTypography(nStartPos, nLength, DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7);

Setting inline images

void CDemoDlg::_DoDemo_InlineImages()

    const CString strTagImageRings = _T("[IMAGE:RINGS]");
    const CString strTagImageRose = _T("[IMAGE:ROSE]");
    const CString strTagImageDog = _T("[IMAGE:DOG]");

    CString strText;
    strText.Format(_T("Here are two rings %s a beautiful rose %s  and a little cute dog %s"),
        strTagImageRings, strTagImageRose, strTagImageDog);


    // set inline images
    m_staticDemo.SetInlineImage(strText.Find(strTagImageRings), strTagImageRings.GetLength(), 
        _T("PNG"), IDB_PNG_RINGS);
    m_staticDemo.SetInlineImage(strText.Find(strTagImageRose), strTagImageRose.GetLength(), 
        _T("PNG"), IDB_PNG_ROSE);
    m_staticDemo.SetInlineImage(strText.Find(strTagImageDog), strTagImageDog.GetLength(), 
        _T("PNG"), IDB_PNG_DOG);

Demo application

Download: DirectWrite Static Control (updated).zip (1360 downloads)

MFC DirectWrite Static Control
MFC DirectWrite Static Control


See also

3 thoughts on “MFC Static Control Using Direct2D and DirectWrite (updated)”

  1. Is AFX_WM_RECREATED2DRESOURCES handler really neccesary in any circumstances? It wasn’t mentioned in your MFC Direct2d tutorial.


Leave a Comment