繁体   English   中英

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

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

我在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;
}

只要将机器的DPI设置为100%,此效果就很好。 但是,如果将DPI设置为较高,则此操作将无效,并且窗口会从屏幕上溢出。 我意识到这是因为WPF像素与“真实”屏幕像素不同,并且因为我正在使用WinForms属性来获取屏幕尺寸。

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

如果不是,那么我想我需要某种缩放比例,但是我不确定如何确定缩放比例。

如何修改功能以说明不同的DPI设置?

顺便说一句,如果您想知道为什么我需要使用此功能而不是最大化窗口,那是因为它是无边界窗口(WindowStyle =“ None”),并且如果您最大化这种类型的窗口,它将涵盖任务栏。

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

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

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

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

如果您想获得两个屏幕的尺寸,则可以使用:

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

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

之后,您可以使用来达到Width,Height等

primaryScreen.Bounds.Width

太长 ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM