• VS 2022 设计 WinForm 高DPI兼容程序


    前言‍

     

    本文主要解决两个问题

    • C# Winform高DPI字体模糊

    • 高DPI下(缩放>100%), UI设计器一直提示缩放到100%, 如果不重启到100%,设计的控件会乱飞

    建立测试程序

    1、新建.NET Windows窗体应用 (Winform)工程

    2、选择.NET 6.0

    3、将窗体尺寸定为 1000 x 1000 , 用于后面检测缩放是否正确

    4、添加一个按钮 , 尺寸定为 150 x 50

    5、添加一个图片框 , 尺寸定为 300 x 300 , 右键导入一张图片

    6、添加测试代码

    1. namespace WinFormsApp1
    2. {
    3.     public partial class Form1 : Form
    4.     {
    5.         public Form1()
    6.         {
    7.             InitializeComponent();
    8.         }
    9.         private void Form1_Load(object sender, EventArgs e)
    10.         {
    11.             Text = this.Width + "x" + this.Height + " pic "+ pictureBox1.Width + "x" + pictureBox1.Height + 启动环境(); 
    12.         }
    13.         public static string 启动环境()
    14.         {
    15. #if NET461
    16.             return (".NET Framework 4.6.1");
    17. #elif NET6_0
    18.             return (".NET6");
    19. #endif  
    20.         }
    21.     }
    22. }

    7、运行看看效果: .NET 6 下运行,尺寸都是对的

    正式开始

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

    取消这段的注释,打开感知 DPI

    1.  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    2.   <windowsSettings>
    3.    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    4.    <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    5.   </windowsSettings>
    6.  </application>

    2、双击工程名称, 编辑配置文件,

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

    完整文件如下

    1. <Project Sdk="Microsoft.NET.Sdk">
    2.  <PropertyGroup>
    3.   <OutputType>WinExe</OutputType>
    4.   <TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
    5.   <UseWindowsForms>true</UseWindowsForms>
    6.   <ApplicationManifest>app.manifest</ApplicationManifest>
    7.   <ApplicationVisualStyles>true</ApplicationVisualStyles>
    8.   <ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
    9.   <ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
    10.  </PropertyGroup>
    11.  <ItemGroup>
    12.   <Compile Update="Properties\Resources.Designer.cs">
    13.    <DesignTime>True</DesignTime>
    14.    <AutoGen>True</AutoGen>
    15.    <DependentUpon>Resources.resx</DependentUpon>
    16.   </Compile>
    17.  </ItemGroup>
    18.  <ItemGroup>
    19.   <EmbeddedResource Update="Properties\Resources.resx">
    20.    <Generator>ResXFileCodeGenerator</Generator>
    21.    <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    22.   </EmbeddedResource>
    23.  </ItemGroup>
    24. </Project>

    3、如果提示找不到控件, 在 Form1.Designer.cs 和 Form1.cs 添加

    1. using System;
    2. using System.Windows.Forms;

    4、Program.cs注释掉 ApplicationConfiguration.Initialize();

    5、运行选择 net461

    备注:我的屏幕是 2800 x 1800 ,缩放 175%

    果然, 显示尺寸不对

    6、Form1.cs 添加 'AutoScaleMode = AutoScaleMode.Dpi;'

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

    再次运行

    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

  • 相关阅读:
    让Python遇上Office--从编程入门到自动化办公实践
    [计算机毕业设计]基于SM9的密钥交换方案的实现与应用
    【无标题】BOOT SERVICES函数实现原型:
    C++项目实战-UDP服务器
    基于高股息高分红优化的量化选股模型
    Java面试ZooKeeper面试题汇总及答案
    LeetCode 26. 删除有序数组中的重复项
    ROS工程实践1—创建工作空间和功能包
    Linux常用命令
    项目实战——推箱子小游戏(引入数据库)
  • 原文地址:https://blog.csdn.net/biyusr/article/details/125575844