WPF窗口在屏幕右边缘的中间打开

时间:2017-06-02 18:31:01

标签: c# wpf xaml window

我有以下代码。我正试着让主窗口右侧的窗口打开,位于屏幕一侧的中间位置。它根本没有移动窗口的起始位置。谢谢!

int screenWidth = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
int screenHeight = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
cd.Top = (screenHeight / 2) - (cd.Height / 2);
cd.Left = screenWidth - (cd.Width + 4);

1 个答案:

答案 0 :(得分:0)

您应该将代码放在窗口的 加载 事件中,此代码也可以更加可重复使用,并按照您的意愿工作,我检查了它。< / p>

private void Window_Loaded(object sender, RoutedEventArgs e)
{
        var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
        this.Left = desktopWorkingArea.Right - this.Width;
        this.Top = desktopWorkingArea.Top + this.Height;
}