Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

c# - 在DLL中使用Screen.Bounds-输出不一致(Using Screen.Bounds in DLL - Inconsistent output)

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%缩放比例的全高清屏幕):)

  • If I embed the DLL in my WPF App, I always get the unscaled screen size (1920x1080).

    (如果将DLL嵌入到WPF应用程序中,则始终会得到未缩放的屏幕尺寸(1920x1080)。)

  • If I embed the DLL in a C# console application, I always get the scaled size (1536x864).

    (如果我将DLL嵌入C#控制台应用程序中,则始终可以得到缩放后的大小(1536x864)。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...