Winform 支持高DPI的几种方式

7bb5dcb9a65ce27367dfed8e13b784d9.png

前言

00845bcf0bc1940075b0769628ef6543.png

28ab476b2a9947c7b4a5a1a38e7107a8.png

    就基于目前的电脑而言,开发Winform界面真的是一键很头疼的事,由于现在基本上都是高DPI,渲染出来的控件就会根据DPI进行缩放,最后导致页面布局混乱。

虽然在.Net Core Winform上,已经可以直接通过设置Application.SetHighDpiMode来解决,但是在.Net Framework下,还是得通过其他方式。

以下方式可以按照官方文档描述选择性的采用或者全部使用。

70b0a8b2e7e8f879d7be8a4f7702b6ef.png

8f69cbd78fcc7079d7bb31a1eb408b7c.gif

034540f333cb609530e13272677d06f6.jpeg

开发环境:.NET Framework版本:4.8

开发工具:Visual Studio 2022

c335d2b4291168e7dad0047e7f53dd97.png

实现步骤

e15d9a73d35f6a175cd6a9931055f59e.png

  1. 第一种方式就是添加程序清单文件,官方文档上也有介绍过此方法,右键项目->添加->新建项->应用程序清单文件(app.manifest)。

242d5251e74254e426140f8e7ad0060b.png

  1. 然后就可以看到文件中已经写好了注释,其中有这样一句话

指示该应用程序可感知 DPI 且 Windows 在 DPI 较高时将不会对其进行自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需选择加入。选择加入此设置的 Windows 窗体应用程序(面向 .NET Framework 4.6)还应在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"

  1. 然后参考官网高DPI支持:

    https://learn.microsoft.com/zh-cn/dotnet/desktop/winforms/high-dpi-support-in-windows-forms?view=netframeworkdesktop-4.8,可以将Win10的相关属性也去掉,其他根据需求自行抉择

  2. 所以,最终app.manifest文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC 清单选项
        如果想要更改 Windows 用户帐户控制级别,请使用
        以下节点之一替换 requestedExecutionLevel 节点。


        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />


        指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
        如果你的应用程序需要此虚拟化来实现向后兼容性,则移除此
        元素。
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>


  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- 设计此应用程序与其一起工作且已针对此应用程序进行测试的
      Windows 版本的列表。取消评论适当的元素,
      Windows 将自动选择最兼容的环境。-->


      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->


      <!-- Windows 7 -->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->


      <!-- Windows 8 -->
      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->


      <!-- Windows 8.1 -->
      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->


      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />


    </application>
  </compatibility>


  <!-- 指示该应用程序可感知 DPI 且 Windows 在 DPI 较高时将不会对其进行
  自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
  选择加入。选择加入此设置的 Windows 窗体应用程序(面向 .NET Framework 4.6)还应
  在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。


  将应用程序设为感知长路径。请参阅 https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
  -->
  <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>


  <!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
  <!--
  <dependency>
  <dependentAssembly>
  <assemblyIdentity
  type="win32"
  name="Microsoft.Windows.Common-Controls"
  version="6.0.0.0"
  processorArchitecture="*"
  publicKeyToken="6595b64144ccf1df"
  language="*"
  />
</dependentAssembly>
</dependency>
  -->


</assembly>
  1. 由于以上文档涉及到了App.config文件的修改,所以应在文件中添加以下内容

<System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
  </System.Windows.Forms.ApplicationConfigurationSection>
  <appSettings>
    <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
  </appSettings>
  1. 第二种方式是采用Windows Api的方式,使用SetProcessDPIAware函数,此函数可以参考Windows Api函数文档(此文档已上传,请点击文末小程序下载),描述如下:

将进程默认 DPI 感知设置为系统 DPI 感知。这等效于调用DPI_AWARENESS_CONTEXT值

为 DPI_AWARENESS_CONTEXT_SYSTEM_AWARE 的 SetProcessDpiAwarenessContext

  1. 同时文档给出这样一句话“SetProcessDPIAware 可能已更改或不可用。请改用 SetProcessDPIAwareness。”,但是其实不妨碍我们使用

  2. 将以上函数在Main函数中调用

internal static class Program
    {


        [DllImport("user32.dll")]
        public static extern bool SetProcessDPIAware();


        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            SetProcessDPIAware();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
  1. 结束

ba41c299b4aad55ec64ebe9e2b586753.png

实现效果

631be019b360c040c1f99b523b1002ce.png

☛☛☛点击此处下载源码☚☚☚

8b37b999979448571a4321e5848e0c4b.gif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值