Winform 在高分变率显示器中窗体变模糊配置方式

   我们知道  Winform 前身与 XP 系统 同一时代出生 , 那时候显示器还是LCD 和 大头机 ,显示器普遍  96 DPI  。 随着显示器质量改善,2K 屏, 4K屏普及,DPI 达  192 甚至更高。  原来winform 程序在 96 dpi 环境开发编译 程序 , 运行完好,在  192  dpi  下,布局可能会错乱 , 或者字看起来小很多 , 字体与边框线条缩放规则不协调等,因此微软提供配置 支持高低 分辨率布局兼容 方式如下:

 

  1.  通过代码实现:

   

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

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (Environment.OSVersion.Version.Major >= 6)
                SetProcessDPIAware();

            Application.Run(new Form4());
        }
    }

 

2. 通过配置文件实现:

    添加应用程序清单文件:

   

 

 

   同时 启用如下配置:

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>

 

app.config 中增加配置项:

  <appSettings>
    <add key="EnableWindowsFormsHighDpiAutoResizing" value= "true" />
  </appSettings>

 

 设置窗体模式:

 

 

这样让操作系统自动处理软件缩放,支持高清显示。

 

   

posted on 2020-06-15 13:53  千浔  阅读(529)  评论(0编辑  收藏  举报