问题描述
我在我的WPF应用程序下面的功能,我使用的窗口大小调整到主屏幕上的工作区域(整个屏幕减去任务栏):
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):
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的属性来获取屏幕尺寸。
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.
我不知道WPF相当于一个以Screen.PrimaryScreen.WorkingArea的。有什么我可以用这个,将工作的DPI设置,无论?
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?
如果没有的话,我想我需要某种形式的缩放,但我不知道如何确定有多少比例通过。
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.
如何修改我的功能,以适应不同的DPI设置?
How can I modify my function to account for different DPI settings?
顺便说一句,如果你想知道为什么我需要使用此功能,而不仅仅是最大化窗口,那是因为它是一个没有国界的窗口(WindowStyle =无),如果您最大化这种类型的窗口,它涵盖了任务栏。
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.
推荐答案
您得到的的属性:
You get the transformed work area size from the SystemParameters.WorkArea
property:
Top = 0;
Left = 0;
Width = System.Windows.SystemParameters.WorkArea.Width;
Height = System.Windows.SystemParameters.WorkArea.Height;
这篇关于变换Screen.PrimaryScreen.WorkingArea到WPF尺寸在更高的DPI设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!