在更高 DPI 设置下将 Screen.PrimaryScreen.WorkingArea 转换为 WPF 尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24441443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:01:00  来源:igfitidea点击:

Transform Screen.PrimaryScreen.WorkingArea to WPF dimensions at higher DPI settings

c#.netwpfdpi

提问by JoeMjr2

I have the following function in my WPF application that I use to resize a window to the primary screen's working area (the whole screen minus the taskbar):

我的 WPF 应用程序中有以下功能,用于将窗口大小调整为主屏幕的工作区域(整个屏幕减去任务栏):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    int theHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
    int theWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;

    this.MaxHeight = theHeight;
    this.MinHeight = theHeight;
    this.MaxWidth = theWidth;
    this.MinWidth = theWidth;

    this.Height = theHeight;
    this.Width = theWidth;

    this.Top = 0;
    this.Left = 0;
}

This works very well, as long as the machine's DPI is set at 100%. However, if they have the DPI set higher, then this doesn't work, and the window spills off the screen. I realize that this is because WPF pixels aren't the same as "real" screen pixels, and because I'm using a WinForms property to get the screen dimensions.

只要机器的 DPI 设置为 100%,这就会非常有效。但是,如果他们将 DPI 设置得更高,则这不起作用,并且窗口会溢出屏幕。我意识到这是因为 WPF 像素与“真实”屏幕像素不同,并且因为我使用 WinForms 属性来获取屏幕尺寸。

I don't know of an WPF equivalent to Screen.PrimaryScreen.WorkingArea. Is there something I can use for this that would work regardless of DPI setting?

我不知道等效于 Screen.PrimaryScreen.WorkingArea 的 WPF。无论 DPI 设置如何,我都可以为此使用什么东西吗?

If not, then I guess I need to some sort of scaling, but I'm not sure how to determine how much to scale by.

如果没有,那么我想我需要进行某种缩放,但我不确定如何确定要缩放的比例。

How can I modify my function to account for different DPI settings?

如何修改我的函数以适应不同的 DPI 设置?

By the way, in case you're wondering why I need to use this function instead of just maximizing the window, it's because it's a borderless window (WindowStyle="None"), and if you maximize this type of window, it covers the taskbar.

顺便说一句,如果你想知道为什么我需要使用这个函数而不是仅仅最大化窗口,那是因为它是一个无边界窗口(WindowStyle="None"),如果你最大化这种类型的窗口,它会覆盖任务栏。

回答by Clemens

You get the transformed work area size from the SystemParameters.WorkAreaproperty:

您可以从SystemParameters.WorkArea属性中获取转换后的工作区大小:

Top = 0;
Left = 0;
Width = System.Windows.SystemParameters.WorkArea.Width;
Height = System.Windows.SystemParameters.WorkArea.Height;

回答by Sheridan

In WPF, you can use the SystemParameters.PrimaryScreenWidthand SystemParameters.PrimaryScreenHeightproperties to find out the primary screen dimensions:

在 WPF 中,您可以使用SystemParameters.PrimaryScreenWidthSystemParameters.PrimaryScreenHeight属性来找出主屏幕尺寸:

double width = SystemParameters.PrimaryScreenWidth;
double height = SystemParameters.PrimaryScreenHeight;

回答by A.Holz

If you wanna get the dimensions of both Screens you simply can use:

如果您想获得两个屏幕的尺寸,您只需使用:

var primaryScreen = 
   System.Windows.Forms
      .Screen
      .AllScreens
      .Where(s => s.Primary)
      .FirstOrDefault();

var secondaryScreen = 
   System.Windows.Forms
      .Screen
      .AllScreens
      .Where(s => !s.Primary)
      .FirstOrDefault();


After this you can reach Width, Height etc. by using

在此之后,您可以通过使用达到宽度、高度等

primaryScreen.Bounds.Width

So Long ;)

太长 ;)