In my WPF application I have to get the resolution including scaling factor for all connected screens.
(在我的WPF应用程序中,我必须获得所有连接屏幕的分辨率,包括缩放比例。)
I found out that Windows Forms includes this functionality ( Screen class), but WPF not. (我发现Windows窗体包含此功能( Screen类),但WPF不包含。)
To prevent mixing up WinForms and WPF code, I outsourced this functionality to a .NET Framework DLL. (为了避免混淆WinForms和WPF代码,我将此功能外包给了.NET Framework DLL。)
Here is a code sample for getting the scaled resolution of the first screen (inside the DLL): (这是一个代码示例,用于获取第一个屏幕(在DLL内部)的缩放分辨率:)
using System.Windows.Forms;
namespace Resolution
{
public class FirstScreen
{
public string GetResolution()
{
return Screen.AllScreens[0].Bounds.Width.ToString() + "x" + Screen.AllScreens[0].Bounds.Height.ToString();
}
}
}
The result (using a full HD screen with 125% scaling):
(结果(使用具有125%缩放比例的全高清屏幕):)
Why do I get different results using the same DLL?
(为什么使用相同的DLL会得到不同的结果?)
And how can I get the scaled size of all screens in WPF, if this is the wrong way? (如果这是错误的方法,如何在WPF中获得所有屏幕的缩放尺寸?)
EDIT:
(编辑:)
Here is the code of my simplified WPF App (for reconstructing the problem):
(这是我简化的WPF应用程序的代码(用于重建问题):)
using System.Windows;
using Resolution;
namespace ScreenResProblem
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FirstScreen fs = new FirstScreen();
tb.Text = fs.GetResolution();
}
}
}
And here the code of the console Application:
(这是控制台应用程序的代码:)
using System;
using Resolution;
namespace ConsoleRes
{
class Program
{
static void Main(string[] args)
{
FirstScreen fs = new FirstScreen();
Console.WriteLine(fs.GetResolution());
Console.ReadKey();
}
}
}
ask by User95 translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…