简体   繁体   English

在WPF中将窗口位置最小化

[英]Minimized window position in WPF

I am trying to save the position of a custom dialog to the users registry, so that when they reload the same dialog, it appears in the same place they moved or resized it to previously. 我试图将自定义对话框的位置保存到用户注册表,以便当他们重新加载同一对话框时,该对话框出现在他们将其移动或调整为先前大小的位置。 I am saving the windows X position Y position Width and Height. 我正在保存窗口X位置Y位置的宽度和高度。 It all works fine except for when the dialog is minimized. 除了最小化对话框外,其他所有方法都工作正常。 If the dialog is minimized and the user right clicks the dialogs representation on the taskbar (windows 7) they can click "close this window". 如果对话框最小化,并且用户右键单击任务栏上的对话框表示(窗口7),则可以单击“关闭此窗口”。 Strangely, the number -32030 gets saved in the registry as the X and Y positions but the width and height get saved correctly. 奇怪的是,数字-32030被保存在注册表中作为X和Y位置,但是宽度和高度却被正确保存。 Any idea where this number comes from and what to do in this situation thx 任何知道这个数字来自哪里以及在这种情况下该怎么做的想法

You want something like this when you save the window position: 保存窗口位置时,您需要这样的内容:

if (this.WindowState == WindowState.Normal)
{
    Properties.Settings.Default.Top = Top;
    Properties.Settings.Default.Left = Left;
    Properties.Settings.Default.Height = Height;
    Properties.Settings.Default.Width = Width;
}
else
{
    Properties.Settings.Default.Top = RestoreBounds.Top;
    Properties.Settings.Default.Left = RestoreBounds.Left;
    Properties.Settings.Default.Height = RestoreBounds.Height;
    Properties.Settings.Default.Width = RestoreBounds.Width;
    // Check for WindowState.Maximized or WindowState.Minimized if you
    // need to do something different for each case (e.g. store if application
    // was Maximized
}

The important bit is the RestoreBounds which you need when the window is maximised or minimised. 重要的一点是在最大化或最小化窗口时所需的RestoreBounds The code can probably be refactored to make it more efficient, but you get the idea. 可以对代码进行重构以使其更高效,但是您明白了。

I guess you are updating the window position when the window is closed? 我想您是在关闭窗口时更新窗口位置吗? There are a couple of solutions if that is the case. 如果是这样的话,有两种解决方案。

1) Save the window position on a different event, like when the window is resized or moved. 1)将窗口位置保存在其他事件上,例如调整窗口大小或移动窗口时。 2) Check to see if the window is minimized before saving the X and Y positions. 2)在保存X和Y位置之前,请检查窗口是否已最小化。

Example: 例:

switch (this.WindowState)
{
    case WindowState.Maximized:
        // don't update the X,Y
        break;
    case WindowState.Minimized:
        // don't update the X,Y
        break;
    case WindowState.Normal:
        // DO update the X,Y
        break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM