16 msdn magazine Windows with C++
Applications can embed or distribute a manifest file—along
with their binaries—that contains hints for the shell or Windows
loader to use in determining how to prepare the application pro-
cess before any code begins to run. Th e Windows shell uses this
for a variety of purposes, including security features, assembly
dependency information and, yes, DPI-awareness claims.
Th ese manifest fi les are just text fi les. Either they’re embedded
within the executable as a traditional Win32 resource or they’re
simply shipped alongside the executable. You can, for example,
take a peek at the manifest fi le for Word by using the manifest tool
included with the Windows SDK using the following command:
mt -inputresource:"C:\ ... \WINWORD.EXE" -out:manifest.xml
I’ve elided the full path to conserve space. If all goes well, you’ll
be rewarded with an XML document in the current directory. You
can see what this looks like in Figure 7. In the middle of some
otherwise unrelated information about the Word executable is the
dpiAware XML element and its value of true.
If an application doesn’t have a manifest file and doesn’t use
the programmatic approach of setting its awareness level, then it’s
assumed to be DPI-unaware. If an application has a manifest fi le
but doesn’t contain the dpiAware element, then it’s again assumed
to be DPI-unaware. On Windows
7, the presence of the dpiAware
element is enough for the system
to assume it’s DPI-aware. It doesn’t
matter what value this element con-
tains or whether it even has a value.
Window 8 is a little more specifi c. It
expects a value starting with T, on
the assumption that it says “True.”
Case doesn’t matter.
So that deals with DPI-unaware
and system DPI-aware applications.
What about per-monitor DPI-
aware applications? Well, when this
feature was fi rst developed, a new
value was chosen for the dpiAware
element: Per Monitor. Th e desire to
let application developers produce
a single binary capable of targeting
both Windows 7 and Windows 8 with support for system DPI
awareness, as well as per-monitor DPI awareness on Windows 8, led
to a new value to be chosen: True/PM. Th is enables Windows 7 and
Windows 8 to accept the binary as system DPI-aware, and Windows
8.1 accepts it as per-monitor DPI-aware. You can, of course, con-
tinue to use Per Monitor if you only need to support Windows 8.1.
Unfortunately, Visual C++ 2013 doesn’t yet support this new
value, but there’s a way to make it happen. Th e Manifest Tool that’s
part of the Visual C++ project build has an option to merge in
additional manifest fi les. All you need to do is create a text fi le with
the correct dpiAware element and value and merge it into your
build. Th e following code shows the XML you need:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns=
"http://schemas.microsoft.com/SMI/2005/WindowsSettings">
True/PM</dpiAware>
</windowsSettings>
</application>
</assembly>
Once you’ve created the manifest fi le, you can simply update your proj-
ect’s settings, as illustrated in Figure 8. Th e manifest is set as an
additional manifest fi le and the DPI awareness is set to None.
When it comes to rendering your application’s window,
you need to make sure you have the right DPI scaling factor.
If you’re looking aft er a legacy application with deep roots in
GDI graphics and USER controls, then you’re likely going
to experience a lot of challenges in trying to make it per-
monitor DPI-aware. Th ese are the same challenges faced by
the Windows and Offi ce teams at Microsoft. But if you’ve been
following my column, you know that Direct2D is the way
to go. Direct2D takes care of all your DPI scaling needs and
presents you with a logical coordinate system independent
of the physical pixels and the DPI scaling factor. Of course,
for Direct2D to be able to do this eff ectively, you need to
tell it what DPI values to use for a particular render target.
Figure 7 The Microsoft Word Manifest File
Figure 8 Manifest Tool Options