Author Archives: Ovidiu Cucu

About Ovidiu Cucu

Co-founder CODEXPERT Follow me on Twitter: StumbleUpon list:

MFC Support for DirectWrite – Part 8: Trimming

When using “classic” GDI functions i.e. CDC::DrawText, it’s no sweat to trim with ellipsis a single-line text when that text does not fit in the drawing rectangle width, by specifying DT_WORDBREAK flag. We cannot find a similar flag for CRenderTarget::DrawText or CRenderTarget::DrawTextLayout. However, trimming a text is also possible with DirectDraw. All we have to do is the following:

  1. call IDWriteFactory::CreateEllipsisTrimmingSign to create an inline object for trimming, using ellipsis as the omission sign;
  2. pass the created inline object to IDWriteTextFormat::SetTrimming.

Here is a simple code example in an MFC-based application:

Direct2D text trimming code sample

Demo project

Download: MFC Support for DirectWrite Demo (Part 8).zip (746)

The demo project contains sample code for all my DirectWrite-related articles. To demonstrate this one, in Trimming granularity combo, select “Character” or “Word”. Also select “No wrap” in Word wrapping combo then have fun.

MFC DirectWrite Demo Project

MFC DirectWrite Demo Project


Resources and related articles

Codexpert – 2016 Articles Summary

C++ Programming Language

Microsoft Libraries

Visual C++

See also

How to Get Visual C++ Sample Projects (2)

In a previous article I showed how to get Visual C++ sample projects either by using Visual Studio IDE or by downloading from MSDN – Developer Code Samples site. Recently, I found that we can also download an archive containing a very large number of sample projects from GitHub. Pretty cool!
Here is the magic link: Microsoft – Windows Classic Samples.

Easy PNG Resource Loading with MFC – Part 2

A previous article demonstrates how easy is to load images from PNG resources, by using Direct2D MFC support. I have found even an easier way: CPngImage class which extends CBitmap with methods that allow loading images from PNG format files and resources.
Here is a brief example:

Using CPngImage class for loading PNG resources

Demo project

It is a simple dialog-based MFC application that loads a bitmap from PNG resource then use the bitmap for setting the image in a static picture control.
Download: PNG Resource Loading (VS 2015).zip (1345)

PNG Resource Loading - Demo Application

PNG Resource Loading – Demo Application


  • of course, we can follow a similar way to set images in other controls, like for example CMFCButton;
  • CPngImage was designed for internal use in the MFC framework, but so far I didn’t see any problem in using it for our own purposes.

Resources and related articles

Read IFD and EXIF Metadata with WIC and MFC

A graphic file can store additional information abaout image (camera model, camera manufacturer, date and time, etc). Windows Imaging Component (WIC) provides interfaces which allow dealing with image metadata. Basically, for reading metadata we need an IWICMetadataQueryReader instance to call its GetMetadataByName method. The first argument of GetMetadataByName is based in Metadata Query Language and is composed by a path in metadata tree and an item name/identifier or a block name/identifier (a block contains itself items and/or other blocks). For more details, please see Metadata Query Language Overview in MSDN library. The second argument is a PROPVARIANT structure that gets the value. If returned value is of type VT_UNKNOWN then we’ve got a block, otherwise we’ve got an item value.

Among other metadata blocks defined in TIFF standard, which are are common to other graphic file formats (e.g. JPEG and raw image file formats like NEF) there are IFD (Image File Directory) and EXIF metadata blocks. I’ll not provide more details here (they can be found in MSDN or in other documents and articles). Just to note that EXIF is nested into IFD block and, while IFD block is placed in the metadata root for TIFF, it is nested/embedded into APP1 block for JPEG file format. For example, to get the ISOSpeed EXIF value, we can call IWICMetadataQueryReader::GetMetadataByName passing “/ifd/exif/{ushort=34867}” in case of TIFF or “/app1/ifd/exif/{ushort=34867}” for JPEG format.

Reading IFD and EXIF metadata from a JPEG file

Here is a brief example:

As can be seen, this example assumes that we have a JPEG file and uses the root metadata query reader by passing the item full path.
Let’s improve it in order to read metadata also from TIFF, NEF and other formats. Let’s also get and use the embedded metadata readers both for IFD and EXIF blocks.

Reading IFD and EXIF metadata from an image file

First, we need a functions which finds out if we are dealing with JPEG or other format.

Next, we can write a function that gets the IFD query reader.

Also we need a function to get the embedded EXIF query reader. Here we can write a generic one, which takes the parent query reader and the block name.

Let’a also write a function that gets the root query reader.

Now, the function which reads the image IFD and EXIF metadata may look like this:

It’s a little bit better than the previous example but still needs to be completed and a lot of code refactoring. I’ll do that in a future article to present a “full featured” IFD/EXIF metadata reader.

References and related articles

Getting Direct2D, DirectWrite and WIC Factories in MFC

MFC library offers a series of wrapper classes over Direct2D and DirectWrite interfaces (see CRenderTarget and CD2D classes). That’s pretty cool because allows to easily load and render images and draw texts, without care too much of direct dealing with COM interfaces.
However, let’s say we have to implemented something different so we need to start creating Direct2D or DirectWrite factories. Of course, we can call D2D1CreateFactory or DWriteCreateFactory functions, but a little bit handier is to use the factories instances created and kept by the MFC framework.

Getting Direct2D, DirectWrite and WIC factories in Visual Studio 2010

In MFC framework shipped with Visual Studio 2010, the instances of ID2D1Factory and IDWriteFactory are created and kept in a global AFX_GLOBAL_DATA structure named afxGlobalData. So, we have to do something like this:

Getting Direct2D, DirectWrite and WIC factories in Visual Studio 2012 – 2015

In Visual Studio 2012, these factory instances have been moved in a D2D-specific structure of type _AFX_D2D_STATE that can be accessed by calling AfxGetD2DState.

Now, we can note that MFC framework keeps also an instance of IWICImagingFactory which is internally used by CD2D classes. That’s also pretty cool; we’ll use it in the following example.

An example of getting and using IWICImagingFactory

Here is a brief example of getting IWICImagingFactory to further read metadata stored in JPEG, TIFF and other image formats.

References and related articles

Visual Studio 2015: How to Step into MFC Framework Code

Let’s say we have installed Visual Studio 2015 and  have started an MFC application in DEBUG mode. Now, attempting to step into an MFC Framework function, e.g. CWinApp::InitInstance, the debugger simply steps over.

Step into MFC Framework failed

Step into MFC Framework failed

Having a look into Output window, we may discover a message like this: “…’C:\Windows\System32\mfc140ud.dll’. Cannot find or open the PDB file”. That’s clear, the necessary PDB file is missing. What can we do? We can buy one from Microsoft or get a free copy from Torrents site. 🙂 Well, don’t need to do that, I was just joking. We can easily get it from Microsoft Symbol Servers, using Visual Studio. Here are the steps:

Changing Debugging/Symbols options in Visual Studio

  1. Open the Options dialog (choose Tools/Options… menu item).
  2. Expand the tree from the left pane to Debugging/Symbols.

    Default debugging symbols options

    Default debugging symbols options

  3. Under Symbol file (.pdb) locations, check Microsoft Symbol Servers. In this moment a message box appears; it suggest that we can choose to get only the debugging symbols for modules which we want, e.g. mfc140ud.dll

    Debug performance message

    Debug performance message

  4. …so let’s check Only specified modules, then click on Specify modules link and add mfc140ud.dll to the list.

    Symbols to load automatically

    Symbols to load automatically

  5. Type or browse for a folder for caching the symbols, e.g. C:\Symbols . Here is how the debugging symbols options finally looks.

    Debugging symbols options

    Debugging symbols options

  6. Hit OK to close the Options dialog.


  • The same steps can be applied for Visual Studio 2013, except that it needs the symbols for mfc120ud.dll.
  • First time the PDB file is needed it may take a pretty long time for downloading. However, next times it is taken from the cache folder, so symbol loading time becomes irrelevant.
  • If have Visual Studio 2015 with at least Update 1, still may not step into the MFC code, even the symbols has been successfully loaded. For this issue see the next topic.

Changing Linker/Debugging project properties

The Update 1 for Visual Studio 2015 comes with /DEBUG:FASTLINK liker option. That’s pretty cool for improving link times but unfortunately, if it’s set, it makes not possible stepping into the MFC Framework code, although the necessary symbols has been loaded. So let’s change it, following these steps:

  1. Open the project’s Property Pages.
  2. Choose Debug configuration and All Platforms.
  3. Expand the tree from left pane to Configuration Properties/Linker/Debugging.
  4. Change Generate Debug Info option from Optimize for faster linking, (/DEBUG:FASTLINK) to Optimize for debugging (/DEBUG).

    Optimize for debugging

    Optimize for debugging

  5. Hit OK to close project’s Property Pages.


  • The above option can also be changed for all projects in the Property Manager window.

Resources and related articles