问题描述
有没有办法使WPF应用程序在每个系统规模上都具有相同的大小?
Is there any way to make WPF application get same size at every system scale?
当我更改更改文本,应用程序和其他项目的大小在Windows系统设置中从全高清屏幕的 125%(推荐)到 100%,我的WPF应用程序变得太小。为了实现独立的系统规模的应用程序,我编写了这样的函数将应用程序的缩放比例更改回125%:
When I change Change size of text, apps and other items in windows system setting from 125% (Recommended) to 100% in a Full-HD screen, My WPF application gets too small. To implement independent system scale application I've wrote a function like this to change scaling of my app back to 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;
}
但是有一些调用此函数的条件。屏幕的第一个必须为Full-HD(有API可以检查此内容),并且系统比例必须为100%(没有.NET API可以获取系统比例)。
But there are conditions to call this function. First of the screen must be Full-HD (There are APIs to check this) and also system scale must be 100% (There is no .NET API to get system scale).
我该怎么办?我是否正在执行使我的应用程序系统独立于规模的标准方法?
What can I do? Am I doing standard way to make my application system scale independent?
我见过的与规模无关的应用程序示例:
Example of scale independent applications I've seen:
- Visual Studio 2017安装程序
- 电报桌面
- Visual Studio 2017 Installer
- Telegram Desktop
推荐答案
终于找到了答案。首先使用以下选项之一获取系统DPI规模:
Finally found an answer. First get system DPI scale using one of the options below:
- 从注册表中读取
AppliedDPI
位于计算机\HKEY_CURRENT_USER\控制面板\桌面\WindowMetrics
中的dword。然后将其除以96。 -
或使用以下代码段:
- Read from registry
AppliedDPI
dword located inComputer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
. Then divide it by 96. Or use this snippet:
double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
返回1.0到2.5之间的值
that returns a value between 1.0 to 2.5
然后创建一个保存应用程序设置的配置文件,并将dpiFactor设置为默认比例。如果用户喜欢自定义比例,请在窗口启动时调用此函数:
Then create a config file that holds application settings and set dpiFactor as default scale. If user preferred a custom scale, call this function on window startup:
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应用程序在每个系统规模上的大小相同(与规模无关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!