在 WPF 中恢复窗口位置和大小时发生漂移

发布于 2024-07-09 06:40:41 字数 1822 浏览 4 评论 0原文

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

每次执行此代码时,我都会观察到向上漂移 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));
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-07-16 06:40:41

试试这个:

1)从普通的窗口派生你的类,而不是 RibbonWindow - 如果解决了这个问题,那就是 RibbonWindow 问题。

2) 使用硬编码值在 Loaded 处理程序中设置测量值 - 如果问题得以解决,则问题与设置有关。

通过这两个更改,它对我来说效果很好。 窗口每次都出现在它应该出现的地方。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文