开发者

Minimized window position in WPF

开发者 https://www.devze.com 2022-12-29 06:57 出处:网络
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 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. It all works fine except for when the dialog is minimized. If the dialog is minimized and开发者_C百科 the user right clicks the dialogs representation on the taskbar (windows 7) they can click "close this window". Strangely, the number -32030 gets saved in the registry as the X and Y positions but the width and height get saved correctly. 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. 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. 2) Check to see if the window is minimized before saving the X and Y positions.

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

精彩评论

暂无评论...
验证码 换一张
取 消