让 WPF 应用程序忽略 DPI 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18449760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:30:14  来源:igfitidea点击:

Have a WPF Application ignore DPI settings

c#wpfdpi

提问by Herrozerro

I have a wpf application that is designed for a 1900x1200 resolution but some of our users have used the windows display settings to resize everything by 125%-150%, is there a way to have an application ignore those settings and still display normally?

我有一个专为 1900x1200 分辨率设计的 wpf 应用程序,但我们的一些用户使用 Windows 显示设置将所有内容调整为 125%-150%,有没有办法让应用程序忽略这些设置并仍然正常显示?

I have seen something called SetProcessDPIAware here, but havent had any luck with it.

我在这里看到了一个叫做 SetProcessDPIAware 的东西,但没有任何运气。

Here is a screen shot of the application with the display scaling turned up to 150% Application with Scaling

这是应用程序的屏幕截图,显示比例调至 150% 缩放应用

As you can see the application is way too small and there is no way to get it to be correctly sized.

如您所见,该应用程序太小了,无法使其大小正确。

采纳答案by Der_Meister

Dpi awareness will not help in this case. It will only change CompositionTarget.TransformToDevice matrix, but controls sizes will not change.

在这种情况下,Dpi 意识将无济于事。它只会改变 CompositionTarget.TransformToDevice 矩阵,但控件大小不会改变。

I use view box to scale all content. All sizes in my app are designed for Surface Pro 3 portrait:

我使用视图框来缩放所有内容。我的应用程序中的所有尺寸都是为 Surface Pro 3 纵向设计的:

<Window>
    <Viewbox Stretch="Uniform">
        <Grid Width="1440" Height="2160">
            <!-- ... -->
        </Grid>
    </Viewbox>
</Window>

You can calculate view box size basing on screen DPI. See this answer: https://stackoverflow.com/a/12414433/991267

您可以根据屏幕 DPI 计算视图框大小。看到这个答案:https: //stackoverflow.com/a/12414433/991267