如何在当前屏幕的左上角放置一个对话框 window?

How to position a dialog window on top left corner of current screen?

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

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

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

window.Left = 0;
window.Top = 0-window.Height;

window.ShowDialog();

事实证明,我只是没有看到必要的属性:每个 Screen 对象在其工作区内都有自己的顶部和左侧 属性。

这对我有用:

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;