如何配置应用程序以在具有高

如何配置应用程序以在具有高

本文介绍了如何配置应用程序以在具有高 DPI 设置(例如 150%)的机器上正确运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 C# 创建了一个简单的 Winforms 应用程序.当我在具有高 DPI 设置(例如 150%)的机器上运行应用程序时,应用程序会被放大.到现在为止还挺好!但不是用更大的字体大小来渲染字体,所有的文本也只是按比例放大.这当然会导致文本非常模糊(在按钮等所有控件上).

I've created a simple Winforms application in C#. When I run the application on a machine with high DPI settings (e.g. 150%), the application gets scaled up. So far so good!But instead of rendering the fonts with a higher font size, all texts are just scaled up, too. That of course leads to very blurry text (on all controls like buttons etc.).

窗口不应该负责正确渲染文本吗?例如,我的应用程序的标题栏呈现清晰 &清楚.

Shouldn't windows take care of rendering the texts correctly? For example my application's title bar is rendered crisp & clear.

推荐答案

一旦超过 100%(或 125%,勾选XP 风格的 DPI 缩放"复选框),Windows 默认会接管 UI 的缩放.它通过让您的应用程序将其输出呈现为位图并将该位图绘制到屏幕来实现.该位图的重新缩放使文本不可避免地看起来模糊.一项名为DPI 虚拟化"的功能,可让旧程序在高分辨率显示器上可用.

Once you go past 100% (or 125% with the "XP-style DPI scaling" checkbox ticked), Windows by default takes over the scaling of your UI. It does so by having your app render its output to a bitmap and drawing that bitmap to the screen. The rescaling of that bitmap makes the text inevitably look fuzzy. A feature called "DPI virtualization", it keeps old programs usable on high resolution monitors.

您必须明确让它知道您可以通过将 <dpiAware> 元素添加到您的清单来处理更高的 DPI 设置.MSDN 页面在这里 但它不完整,因为它省略了 UAC 设置.项目 + 添加新项目,选择应用程序清单文件".编辑清单文本或复制/粘贴此内容:

You have to explicitly let it know that you can handle higher DPI settings by adding the <dpiAware> element to your manifest. The MSDN page is here but it isn't complete since it is omitting the UAC settings. Project + Add New Item, pick "Application Manifest File". Edit the manifest text or copy/paste this:

<?xml version="1.0" encoding="utf-8"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
    <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">
                <requestedExecutionLevel level="asInvoker" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
    <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
            <dpiAware>true</dpiAware>
        </asmv3:windowsSettings>
    </asmv3:application>
</assembly>

您还可以在 Main() 方法中调用 SetProcessDPIAware(),例如,如果您使用 ClickOnce 进行部署,则是必需的:

You can also pinvoke SetProcessDPIAware() in your Main() method, necessary for example if you deploy with ClickOnce:

    [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();

更新,如果您使用 VS2015 Update 1 或更高版本,这个常见的需求最终会更容易一些.添加的manifest已经有相关指令,去掉注释即可.


UPDATE, this common need is finally a bit easier if you use VS2015 Update 1 or higher. The added manifest already has the relevant directive, just remove the comments.

搜索关键字,以便我可以找到这篇文章:dpiAware

Keyword for search so I can find this post back: dpiAware

这篇关于如何配置应用程序以在具有高 DPI 设置(例如 150%)的机器上正确运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:24