2008-12-06 91 views
1

我使用下面的代码在重新启动时保存和恢复窗口位置和大小。在WPF中恢复窗口位置和大小时漂移

我正在观察向上漂移28个像素,每次执行此代码

我读的是错误的数值,还是我错误地恢复它们?数28(铬的大小?)来自哪里(以及如何以编程方式说明它,而不是代码中的固定数字)?

这里是我的代码:

public partial class MainStudioWindowControl : RibbonWindow 
{ 
    public MainStudioWindowControl() 
    { 
     App.MainWindowOwner = this; 
     this.Loaded += new System.Windows.RoutedEventHandler(MainStudioWindowControl_Loaded); 
    } 

    void MainStudioWindowControl_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     System.Windows.Window mainWindow = System.Windows.Application.Current.MainWindow; 
     mainWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual; 
     if (Studio.Properties.Settings.Default.Width > 0) 
     { 
      mainWindow.Left = Studio.Properties.Settings.Default.Left; 
      mainWindow.Top = Studio.Properties.Settings.Default.Top; 
      mainWindow.Width = Studio.Properties.Settings.Default.Width; 
      mainWindow.Height = Studio.Properties.Settings.Default.Height; 
     } 
     Debug.WriteLine(string.Format("Loading: Top = {0}", this.Top)); 
    } 

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) 
    { 
     base.OnClosing(e); 
     System.Windows.Window mainWindow = System.Windows.Application.Current.MainWindow; 
     Studio.Properties.Settings.Default.Left = mainWindow.Left; 
     Studio.Properties.Settings.Default.Top = mainWindow.Top; 
     Studio.Properties.Settings.Default.Width = mainWindow.Width; 
     Studio.Properties.Settings.Default.Height = mainWindow.Height; 
     Studio.Properties.Settings.Default.Save(); 
     Debug.WriteLine(string.Format("Saving: Settings.Top = {0}", Studio.Properties.Settings.Default.Top)); 
    } 
} 

回答

3

试试这个:

1)从正常窗口派生类,而不是RibbonWindow - 能否解决,这是一个RibbonWindow问题。

2)使用硬编码的值来设置加载处理程序中的度量值 - 如果修复了这个问题,问题就与设置有关。

有了这两个改变,它对我来说工作得很好。窗户出现在每次它应该在的地方。

+0

是的,我敢打赌,当你设置Top属性时,RibbonWindow没有考虑到Windows标题栏,但是当你得到它时会这样做。 – 2008-12-06 16:46:17