当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-10 18:35:42 C#

C#学习教程:在较高DPI设置下将Screen.PrimaryScreen.WorkingArea转换为WPF大小主屏幕区域(整个屏幕减去任务栏):inttheWidth=System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;this.MaxHeight=theHeight;this.MinHeight=theHeight;this.MaxWidth=theWidth;this.MinWidth=theWidth;=宽度;这个.Top=0;这个。左=0;只要机器的DPI设置为100%,这就可以正常工作。但是,如果他们将DPI设置得更高,那么这将不起作用并且窗口会溢出屏幕。我意识到这是因为WPF像素与“真实”屏幕像素不同,并且因为我使用WinForms属性来获取屏幕尺寸。我不知道与Screen.PrimaryScreen.WorkingArea等效的WPF。无论DPI设置如何,我都可以使用哪些东西?如果没有,那么我想我需要某种缩放,但我不知道如何确定要缩放多少。如何修改我的函数以适应不同的DPI设置?顺便说一句,如果你想知道为什么我需要使用这个函数而不是仅仅最大化窗口,那是因为它是一个无边框窗口(WindowStyle="None"),如果你最大化一个这种类型的窗口,它覆盖了任务栏。您可以从SystemParameters.WorkArea属性中获取转换后的工作区大小:Top=0;左=0;宽度=System.Windows.SystemParameters.WorkArea.Width;Height=System.Windows.SystemParameters.WorkArea.Height;在WPF中,您可以使用SystemParameters.PrimaryScreenWidth和SystemParameters.PrimaryScreenHeight属性来查找主屏幕尺寸:doublewidth=SystemParameters.PrimaryScreenWidth;双高度=SystemParameters.PrimaryScreenHeight;如果你想获得两种屏幕尺寸,你可以使用:varprimaryScreen=System.Windows.Forms.Screen.AllScreens.Where(s=>s.Primary).FirstOrDefault();varsecondaryScreen=System.Windows.Forms.Screen.AllScreens.Where(s=>!s.Primary).FirstOrDefault();在此之后,您可以使用它来实现宽度、高度等。primaryScreen.Bounds.Width太长了;)以上就是关于C#学习教程:ConvertingScreen.PrimaryScreen.WorkingAreatoWPFDimensionsatHigherDPISettings的全部内容,如果它对您有用你和你需要了解更多的C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: