VS 2022 Designing WinForms High-DPI Compatible Application

VS 2022 Designing WinForms High-DPI Compatible Application

Under high DPI (scaling >100%), the UI designer constantly prompts to scale to 100%. If you do not restart to 100%, the designed controls will scatter.

Last updated 5/14/2022 12:02 PM
AlexChow
4 min read
Category
WinForms
Tags
.NET WinForms UI Design Visual Studio

This article mainly solves two problems

  • C# Winform high DPI font blur.
  • Under high DPI (scaling > 100%), the UI designer continuously prompts to scale to 100%. If you don't restart at 100%, the designed controls will fly around.

Create a test program

  1. Create a new .NET Windows Forms App (WinForms) project

  1. Select .NET 6.0

  1. Set the form size to 1000 x 1000 for later verification of correct scaling

  1. Add a button with size 150 x 50

  1. Add a PictureBox with size 300 x 300, right-click to import an image

  1. Add test code
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Text = this.Width + "x" + this.Height + " pic "+ pictureBox1.Width + "x" + pictureBox1.Height + 启动环境();

        }

        public static string 启动环境()
        {
#if NET461
            return (".NET Framework 4.6.1");
#elif NET6_0
            return (".NET6");
#endif
        }

    }
}
  1. Run and see the effect: under .NET 6, the dimensions are all correct

Let's officially begin

  1. Right-click the project, add an application manifest app.manifest, keep the filename default, then modify it

Uncomment this section to enable DPI awareness

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
        <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
</application>
  1. Double-click the project name to edit the configuration file

Change TargetFrameworks to a dual-target framework: <TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>. After saving, you'll be prompted to reload the project. It's best to close and reopen Visual Studio.

The complete file is as follows

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>WinExe</OutputType>
		<TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
		<UseWindowsForms>true</UseWindowsForms>
		<ApplicationManifest>app.manifest</ApplicationManifest>
		<ApplicationVisualStyles>true</ApplicationVisualStyles>
		<ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
		<ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
	</PropertyGroup>

	<ItemGroup>
		<Compile Update="Properties\Resources.Designer.cs">
			<DesignTime>True</DesignTime>
			<AutoGen>True</AutoGen>
			<DependentUpon>Resources.resx</DependentUpon>
		</Compile>
	</ItemGroup>

	<ItemGroup>
		<EmbeddedResource Update="Properties\Resources.resx">
			<Generator>ResXFileCodeGenerator</Generator>
			<LastGenOutput>Resources.Designer.cs</LastGenOutput>
		</EmbeddedResource>
	</ItemGroup>
</Project>
  1. If you are prompted that controls cannot be found, add the following lines to Form1.Designer.cs and Form1.cs
using System;
using System.Windows.Forms;
  1. In Program.cs, comment out ApplicationConfiguration.Initialize();

  2. Run and select net461

Note: My screen is 2800 x 1800 with 175% scaling

As expected, the displayed dimensions are incorrect

  1. Add AutoScaleMode = AutoScaleMode.Dpi; to Form1.cs
public Form1()
{
    AutoScaleMode = AutoScaleMode.Dpi; // Add this line, above 'InitializeComponent();'
    InitializeComponent();
}

Run again

Perfect!

  1. Double-click the form editor – no prompt about 100% scaling. Add a standard menu and DataGridView for testing

Perfect! Double the pleasure!

Summary

  • Create a new .NET Windows Forms App (WinForms) project [.NET 6.0]
  • Add an application manifest app.manifest and enable DPI awareness
  • Change TargetFrameworks to dual-target: <TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
  • In Program.cs, comment out ApplicationConfiguration.Initialize();
  • Add AutoScaleMode = AutoScaleMode.Dpi; above InitializeComponent();

For older projects, you can also upgrade to this new project format by editing the project file, which supports the features described in this article. If you need further tutorials, please leave a comment. I've been on vacation for the past few days, so I'll stop here for today. See you next time!

Accompanying DEMO

Keep Exploring

Related Reading

More Articles
Same tag 2/21/2025

.NET Project Automation Secrets: Full Guide to One-Click Version Update and Publish Scripts

This article details how to use PowerShell scripts and batch files to automate version updates and one-click publishing in .NET Avalonia UI projects. First, it explains the setup and modification of PowerShell execution policies to ensure scripts can run properly. Then, it introduces methods for adding scripts in Visual Studio pre-build events to automatically update version numbers, as well as using batch files to publish applications on multiple platforms. Finally, a PowerShell script example is provided that can automatically update program version information based on Git tags. These methods can improve the development efficiency and convenience of the publish process for .NET projects.

Continue Reading
Same tag 2/21/2025

(1)From Nurse to C# Developer -- Can a Nurse Successfully Switch to Learning .NET Development?

The article tells the story of a nurse who resigned due to work pressure and family responsibilities. After facing difficulties in job hunting, she decided to learn programming, especially the C# language. She describes in detail what she learned on the first day, including .NET, .NET Framework, C# language concepts, types of C# development software, different UI frameworks, interaction modes, and the use of Visual Studio. She expresses her confidence and determination to learn programming and hopes to receive guidance and help from more people.

Continue Reading