如何将对话窗口定位在当前屏幕的左上角?



我正在尝试将 WPF 对话框窗口放置在当前屏幕的左上角。但是,我不知道如何获取除主屏幕以外的任何其他屏幕的坐标。 对于主屏幕,顶部和左侧将为 0。但是,对于任何其他屏幕,我需要知道偏移量。 我可以有第二个甚至第三个屏幕。更复杂的是:其他屏幕(理论上)可以放置在主屏幕的左侧、顶部或下方。

我做了一些研究,但找不到解决方案。有人可以指出我正确的方向吗?

尝试将值设置为 Window.Left 和 Window.Top:

window.Left = 0;
window.Top = 0-window.Height;
window.ShowDialog();

事实证明,我只是没有看到必要的属性:每个屏幕对象在其工作区中都有自己的 Top 和 Left 属性。

这对我有用:

var topLeftCornerOfMainWindow = new System.Drawing.Point((int)System.Windows.Application.Current.MainWindow.Left, (int)System.Windows.Application.Current.MainWindow.Top);
var currentScreen = Screen.FromPoint(topLeftCornerOfMainWindow);
this.Top = currentScreen.WorkingArea.Top;
this.Left = currentScreen.WorkingArea.Left;
this.Width = currentScreen.WorkingArea.Width;
this.Height = currentScreen.WorkingArea.Height;

最新更新