How to restore a minimized Window in code-behind?

后端 未结 7 925
无人共我
无人共我 2020-12-08 06:24

This is somewhat of a mundane question but it seems to me there is no in-built method for it in WPF. There only seems to be the WindowState property which being

7条回答
  •  余生分开走
    2020-12-08 06:54

    Hmmm, the accepted answer did not work for me. The "maximized" window, when recalled from the task bar would end up centering itself (displaying in its Normal size, even though its state is Maximized) on the screen and things like dragging the window by its title bar ended up not working. Eventually (pretty much by trial-and-error), I figured out how to do it. Thanks to @H.B. and @Eric Liprandi for guiding me to the answer! Code follows:

    private bool windowIsMinimized = false;
    private WindowState lastNonMinimizedState = WindowState.Normal;
    
    private void Window_StateChanged(object sender, EventArgs e)
    {
        if (this.windowIsMinimized)
        {
            this.windowIsMinimized = false;
            this.WindowState = WindowState.Normal;
            this.WindowState = this.lastNonMinimizedState;
        }
        else if (this.WindowState == WindowState.Minimized)
        {
            this.windowIsMinimized = true;
        }
    }
    
    private void Window_MinimizeButtonClicked(object sender, MouseButtonEventArgs e)
    {
        this.lastNonMinimizedState = this.WindowState;
        this.WindowState = WindowState.Minimized;
        this.windowIsMinimized = true;
    }
    
    private void Window_MaximizeRestoreButtonClicked(object sender, MouseButtonEventArgs e)
    {
        if (this.WindowState == WindowState.Normal)
        {
            this.WindowState = WindowState.Maximized;
        }
        else
        {
            this.WindowState = WindowState.Normal;
        }
    
        this.lastNonMinimizedState = this.WindowState;
    }
    

提交回复
热议问题