# WinformHighDPICompatibleProgram **Repository Path**: gitmy/WinformHighDPICompatibleProgram ## Basic Information - **Project Name**: WinformHighDPICompatibleProgram - **Description**: 手把手教会 VS2022 设计 Winform 高DPI兼容程序 (net461 net6.0 双出) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2025-09-15 - **Last Updated**: 2025-09-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 本文主要解决两个问题 * C# Winform高DPI字体模糊. * 高DPI下(缩放>100%), UI设计器一直提示缩放到100%, 如果不重启到100%,设计的控件会乱飞. ![QQ截图20220413234355](https://user-images.githubusercontent.com/8428709/163275524-9bc8e8a8-68bc-44d2-adb8-15cdcdda00bb.png) ![QQ截图20220413234045](https://user-images.githubusercontent.com/8428709/163275119-f763b05e-8e3b-41b9-b8e6-ef4423e75be6.png) ## 建立测试程序 1. 新建.Net Windows窗体应用 (Winform)工程 ![1](https://user-images.githubusercontent.com/8428709/163266315-dbff89a8-7ae1-4309-8d0f-d6901597a703.png) 2. 选择.Net6.0 ![2](https://user-images.githubusercontent.com/8428709/163266610-d118dd9a-19d4-4521-94e3-b78fa9d584a7.png) 3. 将窗体尺寸定为 1000 x 1000 , 用于后面检测缩放是否正确 ![3](https://user-images.githubusercontent.com/8428709/163266621-0ebe0f55-a6b6-431f-a720-f456f59ff5c4.png) 4. 添加一个按钮 , 尺寸定为 150 x 50 ![4](https://user-images.githubusercontent.com/8428709/163266653-de510ced-eb5d-4b71-81f7-56c8de189645.png) 5. 添加一个图片框 , 尺寸定为 300 x 300 , 右键导入一张图片 ![5](https://user-images.githubusercontent.com/8428709/163266652-befe30dc-9af2-4755-8396-22ef043b6ec1.png) ![6](https://user-images.githubusercontent.com/8428709/163266647-b7fda41f-32ab-40e4-95e7-88801d145306.png) 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. 运行看看效果: net6下运行,尺寸都是对的 ![7](https://user-images.githubusercontent.com/8428709/163266645-90c8ca51-0a2d-4fe9-ac4f-40101042f181.png) ## 正式开始 1. 右键工程,添加应用程序清单 `app.manifest`, 文件名用默认,修改 ![QQ截图20220413234934](https://user-images.githubusercontent.com/8428709/163276209-58e8968d-28ac-4405-9d3d-13bdc6915f2b.png) 取消这段的注释,打开感知 DPI ``` true true ``` 2. 双击工程名称, 编辑配置文件, `TargetFrameworks` 改为双目标框架 `net6.0-windows;net461;` , 保存后提示重载工程 , 最好是关闭vs再打开一次. 完整文件如下 ``` WinExe net6.0-windows;net461; true app.manifest true false SystemAware True True Resources.resx ResXFileCodeGenerator Resources.Designer.cs ``` 3. 如果提示找不到控件, 在 Form1.Designer.cs 和 Form1.cs 添加 ``` using System; using System.Windows.Forms; ``` 4. `Program.cs`注释掉 `ApplicationConfiguration.Initialize();` 5. 运行选择 `net461` _备注:我的屏幕是 2800 x 1800 ,缩放 175%_ ![QQ截图20220414002554](https://user-images.githubusercontent.com/8428709/163280230-fa0c85ca-8684-498c-b456-ee1fc36c0a90.png) 果然, 显示尺寸不对 ![QQ截图20220414002450](https://user-images.githubusercontent.com/8428709/163280144-76d3c1bb-4010-4c58-b3c8-a7d24fe603b2.png) 6. Form1.cs 添加 'AutoScaleMode = AutoScaleMode.Dpi;' ``` public Form1() { AutoScaleMode = AutoScaleMode.Dpi; //添加这句,要在'InitializeComponent();'上方 InitializeComponent(); } ``` 再次运行 ![QQ截图20220414002857](https://user-images.githubusercontent.com/8428709/163280527-4ecc6a95-e12e-4a75-97e7-e1cd2faa7c90.png) **完美!** 7. 双击编辑窗体,没有提示100%缩放, 添加标准菜单和DataGridView测试 ![QQ截图20220414003217](https://user-images.githubusercontent.com/8428709/163280909-0494bf3b-e5a7-4503-b42c-d8187320a7a9.png) **完美!双倍的快乐!** ## 总结 * 新建.Net Windows窗体应用 (Winform)工程 [.Net6.0] * 添加应用程序清单 `app.manifest`, 打开感知 DPI * `TargetFrameworks` 改为双目标框架 `net6.0-windows;net461;` * `Program.cs`注释掉 `ApplicationConfiguration.Initialize();` * `AutoScaleMode = AutoScaleMode.Dpi;` //添加这句,要在'InitializeComponent();'上方 ## 老工程也可以通过编辑projet文件升级到这种新工程格式,支持本文说的功能, 需要继续出教程的请在评论区留言, 这几天都在度假中, 今天就写到这里了. 下回见!