2014-01-20 52 views
1

我有主窗口,它被设置为最大化负载。但是,当我Un-Maximize窗口或双击窗口的标题栏。它随机被放置在屏幕上,但不是ScreenCenter在最大化状态下的CenterScreen窗口,在StateChanged上

有什么办法,我可以在ScreenCenter放置窗口时未最大化

我试图在希望下面的代码,但没有工作

private void InfoWindow_OnStateChanged(object sender, EventArgs e) 
{ 
    var window = sender as Window; 
    if(window != null && window.WindowState == WindowState.Normal) 
     WindowStartupLocation = WindowStartupLocation.CenterScreen; 
} 

回答

1

由于WPF不提供直接的这个工作方法,你需要做这样的事情:

var workingArea = System.Windows.SystemParameters.WorkArea; 
this.Left = (workingArea.Width - this.Width)/2 + workingArea.Left; 
this.Top = (workingArea.Height - this.Height)/2 + workingArea.Top; 
+0

这是winforms –

+0

@ K.B,感谢您的支持。让我编辑。 –

+1

谢谢它完成了这项工作:) – ADi

1

您可以使用此方法来设置窗口的位置,你的屏幕的中心。

private void CenterWindowOnScreen() 
    { 
     double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth; 
     double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight; 
     double windowWidth = this.Width; 
     double windowHeight = this.Height; 
     this.Left = (screenWidth/2) - (windowWidth/2); 
     this.Top = (screenHeight/2) - (windowHeight/2); 
    } 

private void InfoWindow_OnStateChanged(object sender, EventArgs e) 
{ 
    var window = sender as Window; 
    if(window != null && window.WindowState == WindowState.Normal) 
     CenterWindowOnScreen(); 
} 
+0

为你的时间,你的解决方案也工作 – ADi

相关问题