有没有办法让 WPF 应用程序在每个系统规模上获得相同的大小?
当我改变时更改文本、应用程序和其他项目的大小在windows系统设置中125%(推荐) to 100%在全高清屏幕中,我的 WPF 应用程序变得太小。为了实现独立的系统缩放应用程序,我编写了一个类似这样的函数,将应用程序的缩放比例更改回 125%:
private void ScaleTo125Percents()
{
// Change scale of window content
MainContainer.LayoutTransform = new ScaleTransform(1.25, 1.25, 0, 0);
Width *= 1.25;
Height *= 1.25;
// Bring window center screen
var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
Top = ( screenHeight - Height ) / 2;
Left = ( screenWidth - Width ) / 2;
}
但调用这个函数是有条件的。首先屏幕必须是全高清(有 API 可以检查这一点),并且系统缩放比例必须为 100%(没有 .NET API 可以获取系统缩放比例)。
我能做些什么?我是否正在采用标准方法来使我的应用程序系统规模独立?
我见过的与规模无关的应用程序示例:
-
Visual Studio 2017 安装程序
- 电报桌面版
终于找到答案了。首先使用以下选项之一获取系统 DPI 比例:
然后创建一个保存应用程序设置的配置文件并将 dpiFactor 设置为默认比例。如果用户喜欢自定义比例,请在窗口启动时调用此函数:
private void UserInterfaceCustomScale(double customScale)
{
// Change scale of window content
MainContainer.LayoutTransform = new ScaleTransform(customScale, customScale, 0, 0);
Width *= customScale;
Height *= customScale;
// Bring window center screen
var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
Top = ( screenHeight - Height ) / 2;
Left = ( screenWidth - Width ) / 2;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)