Minimized window position in WPF

后端 未结 2 1323
长情又很酷
长情又很酷 2021-01-24 09:45

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 pre

2条回答
  •  情深已故
    2021-01-24 10:13

    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.

提交回复
热议问题