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()
{
    m_staticDemo.Clear();

    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);

    m_staticDemo.SetText(strText);

    // 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()
{
    m_staticDemo.Clear();

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

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

    m_staticDemo.SetText(strText);
    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()
{
    m_staticDemo.Clear();

    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);

    m_staticDemo.SetText(strText);

    // 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 (1338 downloads)

MFC DirectWrite Static Control
MFC DirectWrite Static Control

Resources

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.

    Reply

Leave a Comment