WPF 应用程序在每个系统规模上都具有相同的大小(独立于规模)
WPF Application same size at every system scale (scale independent)
有没有办法让 WPF 应用程序在每个系统规模上都获得相同的大小?
当我将 更改文本、应用程序和其他项目的大小 windows 系统设置从 125%(推荐) 到全高清屏幕中的 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 安装程序
- Telegram 桌面版
终于找到答案了。首先使用以下选项之一获取系统 DPI 比例:
- 从位于
Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
中的注册表 AppliedDPI
双字读取。然后除以 96。
或使用此代码段:
double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
returns 介于 1.0 和 2.5 之间的值
然后创建一个包含应用程序设置的配置文件,并将 dpiFactor 设置为默认比例。如果用户更喜欢自定义比例,请在 window 启动时调用此函数:
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;
}
有没有办法让 WPF 应用程序在每个系统规模上都获得相同的大小?
当我将 更改文本、应用程序和其他项目的大小 windows 系统设置从 125%(推荐) 到全高清屏幕中的 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 安装程序
- Telegram 桌面版
终于找到答案了。首先使用以下选项之一获取系统 DPI 比例:
- 从位于
Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
中的注册表AppliedDPI
双字读取。然后除以 96。 或使用此代码段:
double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
returns 介于 1.0 和 2.5 之间的值
然后创建一个包含应用程序设置的配置文件,并将 dpiFactor 设置为默认比例。如果用户更喜欢自定义比例,请在 window 启动时调用此函数:
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;
}