建立測試程序
1、新建.NET Windows窗體應用 (Winform)工程

2、選擇.NET 6.0

3、將窗體尺寸定為 1000 x 1000, 用于后面檢測縮放是否正確

4、添加一個按鈕, 尺寸定為 150 x 50

5、添加一個圖片框, 尺寸定為 300 x 300, 右鍵導入一張圖片


6、添加測試代碼
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??
????????}
????}
}
7、運行看看效果: .NET 6 下運行,尺寸都是對的

正式開始
1、右鍵工程,添加應用程序清單?app.manifest
, 文件名用默認,修改

取消這段的注釋,打開感知 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>
2、雙擊工程名稱, 編輯配置文件,
TargetFrameworks
?改為雙目標框架?<TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
?, 保存后提示重載工程, 最好是關閉vs再打開一次.
完整文件如下
<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>
3、如果提示找不到控件, 在 Form1.Designer.cs 和 Form1.cs 添加
using?System;
using?System.Windows.Forms;
4、Program.cs
注釋掉?ApplicationConfiguration.Initialize();
5、運行選擇?net461
備注:我的屏幕是 2800 x 1800,縮放 175%

果然, 顯示尺寸不對

6、Form1.cs 添加 'AutoScaleMode = AutoScaleMode.Dpi;'
public?Form1()
{
????AutoScaleMode?=?AutoScaleMode.Dpi;?//添加這句,要在'InitializeComponent();'上方
????InitializeComponent();
}
再次運行

7、雙擊編輯窗體,沒有提示100%縮放, 添加標準菜單和DataGridView測試

完美!雙倍的快樂!
總結
- 新建.Net Windows窗體應用 (Winform)工程 [.Net6.0]
- 添加應用程序清單?
app.manifest
, 打開感知 DPI TargetFrameworks
?改為雙目標框架?<TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
Program.cs
注釋掉?ApplicationConfiguration.Initialize();
AutoScaleMode = AutoScaleMode.Dpi;
?//添加這句,要在'InitializeComponent();'上方
老工程也可以通過編輯projet文件升級到這種新工程格式,支持本文說的功能
配套DEMO
https://github.com/densen2014/WinformHighDPICompatibleProgram
https://gitee.com/alexchow/WinformHighDPICompatibleProgram