MFC Support for DirectWrite – About Trimming Again

In a previous article, I showed how to trim a text which overflows the layout box. In the example presented there, the ellipsis is added at the end of truncated text. But if, let’s say, we have to show a long path and file name, it is not meaningful if the file name is not displayed. No problem, this can be easily resolved if have a look at DWRITE_TRIMMING structure which passed to IDWriteTextFormat::SetTrimming. The second parameter is a character code used as delimiter and the third one is the delimiter count. The text is preserved from the end until is found the delimiter which coresponds to delimiter count. For example, if the delimiter is backslash and the delimiter count is 2, a text like “e:\Articles\MFC Support for DirectWrite\DirectWrite Trimming Demo\D2DStaticCtrl.cpp” may be displayed as follows “e:\Articles…\DirectWrite Trimming Demo\D2DStaticCtrl.cpp”.

An example of trimming by using a delimiter

LRESULT CD2DStaticCtrl::OnAfxDraw2D(WPARAM wParam, LPARAM lParam)
    // ...

    // Set text trimming
    CComPtr<IDWriteInlineObject> spInlineObject;
    IDWriteFactory* pDirectWriteFactory = AfxGetD2DState()->GetWriteFactory();
    hr = pDirectWriteFactory->CreateEllipsisTrimmingSign(textFormat.Get(), 
    if (FAILED(hr))
        return FALSE;


    hr = textFormat.Get()->SetTrimming(&trimming, spInlineObject);
    if (FAILED(hr))
        return FALSE;

    // ...

    return TRUE;

Demo project

Download: DirectWrite Trimming (1349 downloads)
The demo project is a simple MFC application which demonstrate how to trim text rendered with DirectWrite. You can choose the granularity, the delimiter and the delimiter count and see what is displayed.

DirectWrite Trimming Demo
DirectWrite Trimming Demo

Resources and related articles

Leave a Comment