【沧海拾昧】C# .NET8 WinForms程序在主显示器125%/150%缩放下尺寸显示异常的解决办法

#C0106


沧海茫茫千钟粟,且拾吾昧一微尘

------《沧海拾昧集》@CuPhoenix


【阅前敬告】 沧海拾昧集仅做个人学习笔记之用,所述内容不专业不严谨不成体系 【如有问题必是本集记录有谬,切勿深究】


问题

使用 C#(.NET8)进行 WinForms 程序开发时,如果主显示器的缩放比例不为100%,会出现窗体和控件显示尺寸异常的问题,网上通常的方案是在 Program.cs 中加入 SetProcessDPIAware(); 函数,但该函数可能对 .NET8 的 WinForms 程序无效。

c 复制代码
[STAThread]
    static void Main() {
        if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());             // Edit as needed
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool SetProcessDPIAware();

解决

通过参考 .NET8 的官方文档和 Git 手册,在 项目名.csproj 文件中(可以搜索 <Project Sdk = "Microsoft.NET.Sdk"> 找到该文件),将 <PropertyGrop></Property>中的内容修改为:

html 复制代码
<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>net8.0-windows</TargetFramework>
  <Nullable>enable</Nullable>
  <UseWindowsForms>true</UseWindowsForms>
  <ImplicitUsings>enable</ImplicitUsings>
  <ApplicationHighDpiMode>DpiUnawareGdiScaled</ApplicationHighDpiMode>
  <ForceDesignerDpiUnaware>true</ForceDesignerDpiUnaware>
</PropertyGroup>

说明

资料1WinForms GitHub 手册 - Designer HighDpi mode 中提到:

Issues

The Winforms designer, when operating in SystemAware/PermonitorV2 mode, serializes layout metrics into the > > source file based on the Dpi setting of the current display device on which it is being rendered. This may lead to two kinds of problems.

WinForms applications developed on a machine with DPI settings different from the machine where they are> > > executed may encounter layout inconsistencies.
Solution

html 复制代码
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   <OutputType>WinExe</OutputType>
   <TargetFramework>net7.0-windows</TargetFramework>
   <Nullable>enable</Nullable>
   <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
    <ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
    <ForceDesignerDpiUnaware>true</ForceDesignerDpiUnaware>
  </PropertyGroup>

但该解决方案中设置的 <ApplicationHighDpiMode> 属性值是错误的,SystemAware 将查询主监视器的 DPI 一次,并将此值用于所有监视器上的应用程序,因此不能解决问题。

资料2.NET8 Learn - ApplicationHighDpiMode,通过微软官方的学习手册,查阅该属性值的取值。ApplicationHighDpiMode 可设置为 HighDpiMode 枚举值之一:

说明
DpiUnaware 应用程序窗口不会缩放 DPI 更改,并且始终假定比例系数为 100%。
DpiUnawareGdiScaled 类似于 DpiUnaware,但提高了基于 GDI/GDI+ 的内容的质量。
PerMonitor 此窗口会在创建 DPI 时对其进行检查,并在 DPI 更改时调整缩放比例。
PerMonitorV2 类似于 PerMonitor,但启用了子窗口 DPI 更改通知、comctl32 控件的改进缩放和对话框缩放。
SystemAware 如果未指定,则为默认值。窗口查询主监视器的 DPI 一次,并将此值用于所有监视器上的应用程序。

选择合适的取值(如 DpiUnawareGdiScaled)即可。

敬谢诸君。


相关推荐
码观天工11 分钟前
C#线程池核心技术:从原理到高效调优的实用指南
性能优化·c#·.net·线程·线程池·多线程·异步
ghost1432 小时前
C#学习第24天:程序集和部署
开发语言·学习·c#
自由的风.2 小时前
回溯法求解N皇后问题
算法·c#·剪枝·迭代加深
Kookoos2 小时前
基于 ABP vNext + CQRS + MediatR 构建高可用与高性能微服务系统:从架构设计到落地实战
微服务·云原生·架构·.net·mediatr·abp vnext
WineMonk3 小时前
ArcGIS Pro 3.4 二次开发 - Arcade
arcgis·c#·.net
hez201010 小时前
.NET 的全新低延时高吞吐自适应 GC - Satori GC
c#·.net·.net core·clr
姜行运19 小时前
数据结构【AVL树】
android·数据结构·c#
chenyuhao20241 天前
链表面试题9之环形链表进阶
数据结构·算法·链表·面试·c#
飞人博尔特的摄影师1 天前
WPF技巧-常用的Converter集合(更新ing)
c#·wpf·xaml·maui·uwp·技巧·valueconverter
炯哈哈1 天前
【上位机——WPF】命名空间
开发语言·windows·c#·wpf·上位机