我知道如果计算机没有使用默认的DPI设置,那么WPF坐标与“真实”屏幕坐标(像素坐标)不同。在我的程序中,我想(1)找出哪个监视器打开了一个WPF窗口,并(2)打开同一个监视器左下角的另一个窗口。我听说有没有Screen为WPF,所以我使用的WinForms版本相当于如下,这在默认的96 DPI工作正常:WPF:在屏幕坐标和WPF坐标之间转换
public void ChooseInitialPosition(Window w) // w is some other window
{
var scr = System.Windows.Forms.Screen.FromRectangle(
new System.Drawing.Rectangle((int)w.Left, (int)w.Top, (int)w.Width, (int)w.Height))
.WorkingArea;
this.Left = scr.Right - Width;
this.Top = scr.Bottom - Height;
}
但在其他的DPI,这两个步骤正确工作,并且可以把窗口完全脱离屏幕。
到目前为止,它看起来像我可以使用Visual.PointToScreen对于第一部分:
var p1 = w.PointToScreen(new Point(0,0));
var p2 = w.PointToScreen(new Point(w.Width,w.Height));
var scr = System.Windows.Forms.Screen.FromRectangle(
new System.Drawing.Rectangle((int)p1.X, (int)p1.Y, (int)(p2.X - p1.X), (int)(p2.Y - p1.Y))).WorkingArea;
我不知道这是否是完全正确的,因为它可能无法解释的边界正确。但第二部分更重要。如何将屏幕矩形“scr”转换为WPF空间,以便正确设置左侧和顶部?
不确定有多个显示器支持,但你可以正确定位你的窗口o主屏幕仅使用WPF(即DPI可识别),而不使用SystemParameters类引用Windows窗体(不支持DPI)。相关属性会自动调整为不同的DPI设置。例如:window.Top = SystemParameters.FullPrimaryScreenHeight - (window.ActualHeight - SystemParameters.WindowCaptionHeight); //适用于Vista和Windows 7,不知道XP。 –