如何配置应用的机器上具有高DPI设置(如150%)正常运行? [英] How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?

查看:4135
本文介绍了如何配置应用的机器上具有高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.

您必须明确地让它知道,你可以通过添加&LT处理更高的DPI设置; dpiAware> 元素的体现。 MSDN的网页。是这里,但它不是T完成,因为它省略了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()方法的PInvoke 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更新1或更高。增加的清单已经有相关的指令,只是删除评论。


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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆