Question

I am using the code below to save and restore the window position and size upon restart.

I am observing an upward drift of 28 pixels everytime I execute this code!

Am I reading the wrong values, or am I restoring them incorrectly? Where is the number 28 (size of the chrome?) coming from (and how would I account for it programmatically, rather than a fixed number in code)?

Here is my code:

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));
    }
}
Was it helpful?

Solution

Try this:

1) Derive your class from the normal Window, not the RibbonWindow - if that fixes it, it's a RibbonWindow issue.

2) Use hard-coded values to set the measurments in the Loaded handler - if that fixes it, the problem's got something to do with the settings.

With these two changes, it worked fine for me. The window appeared where it should every time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top