The WinForms Notepad Project: High-DPI Support (Premium)

Before (left) and after (right, with high-DPI support)

When Windows Forms first appeared with the initial release of the .NET Framework in early 2002, Windows XP was still brand new and high DPI displays were several years away from being the norm in the PC market. For this reason, Windows Forms applications can look fuzzy when run on high DPI displays, and this is something we’ve seen with our Notepad control.

Fortunately, Microsoft added support for high DPI displays to Windows Forms via updates to the .NET Framework, and it has improved that support over the years as Windows 8 and then Windows 10 arrived. And as of .NET Framework 4.7, high DPI support in Windows Forms is pretty much complete and supports the latest OS features related to this functionality.

The issue is that it’s not enabled by default. So let’s enable it.

We need to make two changes, one each to two different application configuration files.

Application manifest

First, we need to add an entry to our application’s manifest file. As described by Microsoft, the “application manifest is an XML file that describes and identifies the shared and private side-by-side assemblies that an application should bind to at run time … Application manifests may also describe metadata for files that are private to the application.” That may or may not mean anything to you, but one thing .NET-based applications have used an application manifest file for over the years is to support the new visual styles for controls and other UI elements that arrived with certain Windows versions. So what we’re using it for here is somewhat similar to that.

To access our application’s manifest, right-click the project name in the Solution Explorer and then select Add > New Item from the context menu that appears. The Add New Item window appears.

Select “Application Manifest File” and then click Add. (Don’t change the name.) A file named app.manifest now appears under the project in Solution Explorer, and it opens in the code editor.

This file includes numerous declarations that are commented out using the “<!–” start comment tag and the “–>” end comment tag. We need to remove the comments around two of them.

The first reads like so:

<!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

So remove the “<!–” and “–>” and text from the second of those two lines. It should now read like so, and explicitly add support for Windows 10 (and previous Windows versions) to our app.

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

Below that, you’ll see another commented out block related to DPI-awareness. It currently looks like so:

<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
</application>
-->

Again, remove the beginning and end comment tags so that it now reads like so:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
</application>

Now, save and close that file.

Application configuration

Second, we need to open App.config (which is found in Solution Explorer). It currently looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>

We need to add a block of XML code that will allow the high-DPI support to support per-display DPI settings in multi-display scenarios. That block of code looks like so:

<System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>

When you’re done, App.config should look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <System.Windows.Forms.ApplicationConfigurationSection>
        <add key="DpiAwareness" value="PerMonitorV2" />
    </System.Windows.Forms.ApplicationConfigurationSection>
</configuration>

Save and close App.config.

Now, run your application. You’ll see that all the text in the menus, the dialogs, and in the text box look clean and crisp now. (The status bar looks too small, but that’s because we don’t have any text in it yet.)

This is exactly the look we were going for.

More soon.

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Thurrott