VS 2022 Designs Winform High DPI Compatible Program

VS 2022 Designs Winform High DPI Compatible Program

Under high DPI (zoom>100%), the UI designer keeps prompting to zoom to 100%. If you don't restart it to 100%, the designed controls will fly around.

最后更新 5/14/2022 12:02 PM
AlexChow
预计阅读 4 分钟
分类
Winform
标签
.NET Winform UI design Visual Studio

This paper mainly solves two problems

  • C#Winform high DPI fonts are blurred.
  • Under high DPI (zoom>100%), the UI designer keeps prompting to zoom to 100%. If you don't restart it to 100%, the designed controls will fly around.

Establish test procedures

  1. New. Net Windows Forms Application (Winform) Project

  1. Select. Net6.0

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

  1. Add a button, size 150 x 50

  1. Add a picture box with the size set to 300 x 300 , and right-click to import a picture

  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 it and see the effect: run it under net6, the size is correct

officially began

  1. 右键工程,添加应用程序清单 app.manifest, 文件名用默认,修改

Uncomment this paragraph and turn on sensing DPI

<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

TargetFrameworks 改为双目标框架 <TargetFrameworks>net6.0-windows;net461;</TargetFrameworks> , 保存后提示重载工程 , 最好是关闭 vs 再打开一次.

The complete document 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 prompted that the control cannot be found, add it in Form1.Designer.cs and Form1.cs
using System;
using System.Windows.Forms;
  1. Program.cs注释掉 ApplicationConfiguration.Initialize();

  2. 运行选择 net461

Note: My screen is 2800 x 1800 , zoom 175%

Sure enough, the display size is wrong

  1. Form1.cs Add 'AutoScaleMode = AutoScaleMode.Dpi;'
public Form1()
{
    AutoScaleMode = AutoScaleMode.Dpi; //添加这句,要在'InitializeComponent();'上方
    InitializeComponent();
}

run again

Perfect!

  1. Double-click the edit form, 100% zoom without prompting, add standard menus and DataGridView tests

** Perfect! Double the happiness!**

summary

  • New. Net Windows Forms Application (Winform) Project [.Net6.0]
  • 添加应用程序清单 app.manifest, 打开感知 DPI
  • TargetFrameworks 改为双目标框架 <TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
  • Program.cs注释掉 ApplicationConfiguration.Initialize();
  • AutoScaleMode = AutoScaleMode.Dpi; //添加这句,要在'InitializeComponent();'上方

** Old projects can also be upgraded to this new project format by editing projet files, which supports the functions described in this article. If you need to continue to issue tutorials, please leave a message in the comments area. You have been on vacation these days, so I'm done here today. See you next time!**

** Supporting DEMO**

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 2/29/2024

Data display can also be done in Winform

In the process of developing winform, data display functions are often needed. I have been using the gridcontrol before. Today, I want to use an example to introduce to you how to use the table component in ant design blazor hybrid to display data.

继续阅读
同分类 / 同标签 2/29/2024

Can Winform's interface become better?

A few days ago, I introduced to you the use of blazor hybrid in winform, and I also said that with blazor's ui, our winform program design can be more beautiful. Next, I want to use an example of drawing in winform blazor hybrid to illustrate it, hoping to be helpful to you.

继续阅读