2017-06-02 84 views
0

我有以下代码。我试图让窗口在屏幕右侧的中间位置在屏幕的中间位置打开。它根本没有移动窗口的起始位置。谢谢!WPF窗口在屏幕的右边缘中间打开

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); 
+0

据我所知“cd”是你的Window实例吗? 如果您将代码放置在MainWindow类中,只需使用 this.Top和this.Left,而不是创建实例 – pablocity

回答

0

你应该把你的代码中负载事件窗口,也是这个代码更readeable一点,可以作为你想,我检查。

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; 
}