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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 (1204)
Resources
- MSDN: MFC Classes
- MSDN: DirectWrite
Is AFX_WM_RECREATED2DRESOURCES handler really neccesary in any circumstances? It wasn’t mentioned in your MFC Direct2d tutorial.
Usually there is no much to do. See this article:
http://codexpert.ro/blog/2019/06/03/mfc-support-for-direct2d-part-8-lost-render-target/
Thank you so much for the explanation about the issue.