有什么方法可以使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;
}
Run Code Online (Sandbox Code Playgroud)
但是有条件可以调用此函数。屏幕的第一个必须为Full-HD(有API可以检查此内容),并且系统比例必须为100%(没有.NET API可以获取系统比例)。
我能做什么?我是否正在使用标准方法来使我的应用程序系统独立于规模?
我见过与规模无关的应用程序的示例:
终于找到了答案。首先使用以下选项之一获取系统 DPI 比例:
AppliedDPI
位于Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
. 然后除以 96。或者使用这个片段:
double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
Run Code Online (Sandbox Code Playgroud)
返回 1.0 到 2.5 之间的值
然后创建一个包含应用程序设置的配置文件,并将 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;
}
Run Code Online (Sandbox Code Playgroud)