如何在代码后恢复最小化窗口?

时间:2022-04-25 07:16:30

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 an enum does not help since i cannot tell whether the Window was in the Normal or Maximized state before being minimized.

这是一个很普通的问题,但在我看来,在WPF中没有内置的方法。因为我无法判断窗口是否处于正常状态,或者在最小化之前,我无法判断窗口是否处于正常状态,所以只有窗口状态属性才会有帮助。

When clicking the taskbar icon the window is being restored just as expected, assuming its prior state, but i cannot seem to find any defined method which does that.

当单击任务栏图标时,窗口将按预期恢复,假设它是先前状态,但我似乎找不到任何已定义的方法来实现这一点。

So i have been wondering if i am just missing something or if i need to use some custom interaction logic.

所以我一直在想,我是不是漏掉了什么东西,或者我是否需要使用一些自定义的交互逻辑。

(I'll post my current solution as answer)

(我将把我当前的解决方案作为答案)

7 个解决方案

#1


72  

Not sure this will work for everybody, but I ran into this today and someone on the team suggested "have you tried Normal"?

我不确定这对每个人都适用,但我今天遇到了这个问题,团队里有人建议“你试过正常吗?”

Turns out he was right. The following seems to nicely restore your window:

结果证明他是对的。以下似乎很好地恢复了您的窗口:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

That works just fine, restoring the window to Maximized if needed. It seems critical to check for the minimized state first as calling WindowState.Normal a second time will "restore" your window to its non-maximized state.

这很好,如果需要的话,可以将窗口恢复到最大。在调用WindowState时,检查最小化状态似乎很重要。正常的第二次将“恢复”您的窗口到它的非最大化状态。

Hope this helps.

希望这个有帮助。

#2


14  

WPF's point of view is that this is an OS feature. If you want to mess around with OS features you might have to get your hands dirty. Luckily they have provided us with the tools to do so. Here is a UN-minimize method that takes a WPF window and uses WIN32 to accomplish the effect without recording any state:

WPF的观点是,这是一个OS特性。如果你想搞乱操作系统的功能,你可能要弄脏你的手。幸运的是,他们为我们提供了这样做的工具。这里有一个unminimum方法,它使用WPF窗口并使用WIN32来完成效果,而不记录任何状态:

public static class Win32
{
    public static void Unminimize(Window window)
    {
        var hwnd = (HwndSource.FromVisual(window) as HwndSource).Handle;
        ShowWindow(hwnd, ShowWindowCommands.Restore);
    }

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

    private enum ShowWindowCommands : int
    {
        /// <summary>
        /// Activates and displays the window. If the window is minimized or 
        /// maximized, the system restores it to its original size and position. 
        /// An application should specify this flag when restoring a minimized window.
        /// </summary>
        Restore = 9,
    }
}

#3


9  

SystemCommands class has a static method called RestoreWindow that restores the window to previous state.

systemcommand类有一个名为RestoreWindow的静态方法,可以将窗口恢复到以前的状态。

SystemCommands.RestoreWindow(this); // this being the current window

[Note : SystemCommands class is part of .NET 4.5+ (MSDN Ref) for projects that target to earlier versions of Framework can use the WPF Shell extension (MSDN Ref)]

[注:SystemCommands类是.NET 4.5+ (MSDN Ref)的一部分,用于针对早期版本的框架的项目可以使用WPF外壳扩展(MSDN Ref)]

#4


3  

Here is how i get it to restore right now: I handle the StateChanged event to keep track of the last state that was not Minimized

下面是我如何让它现在恢复:我处理状态更改事件以跟踪未最小化的最后一个状态。

WindowState _lastNonMinimizedState = WindowState.Maximized;
private void Window_StateChanged(object sender, EventArgs e)
{
    if (this.WindowState != System.Windows.WindowState.Minimized)
    {
        _lastNonMinimizedState = WindowState;
    }
}

To restore i then have to set that WindowState respectively:

为了恢复,我必须分别设置窗口状态:

this.WindowState = _lastNonMinimizedState;

#5


1  

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:

嗯,公认的答案对我不起作用。“最大化”窗口,当从任务栏中调用时,最终会以自身为中心(以正常大小显示,即使它的状态是最大化的)在屏幕上显示,而像通过标题栏拖动窗口这样的事情最终会失败。最终(几乎是通过反复试验),我找到了如何去做。由于@H.B。还有@Eric Liprandi,感谢他给我的答案!代码如下:

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

#6


1  

For some reason,

出于某种原因,

WindowState = WindowState.Normal;

didn't work for me. So I used following code & it worked..

没有为我工作。所以我用了下面的代码。

 Show();
 WindowState = WindowState.Normal;

#7


0  

In native Windows you can restore your window to a previous state with ShowWindow(SW_RESTORE):

在本机窗口中,您可以使用ShowWindow(SW_RESTORE)将窗口恢复到以前的状态:

Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.

激活并显示窗口。如果窗口最小化或最大化,系统将它恢复到原来的大小和位置。应用程序应该在还原最小化窗口时指定此标志。

There's surely .Net counterpart to that.

肯定有。net对应的。

#1


72  

Not sure this will work for everybody, but I ran into this today and someone on the team suggested "have you tried Normal"?

我不确定这对每个人都适用,但我今天遇到了这个问题,团队里有人建议“你试过正常吗?”

Turns out he was right. The following seems to nicely restore your window:

结果证明他是对的。以下似乎很好地恢复了您的窗口:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

That works just fine, restoring the window to Maximized if needed. It seems critical to check for the minimized state first as calling WindowState.Normal a second time will "restore" your window to its non-maximized state.

这很好,如果需要的话,可以将窗口恢复到最大。在调用WindowState时,检查最小化状态似乎很重要。正常的第二次将“恢复”您的窗口到它的非最大化状态。

Hope this helps.

希望这个有帮助。

#2


14  

WPF's point of view is that this is an OS feature. If you want to mess around with OS features you might have to get your hands dirty. Luckily they have provided us with the tools to do so. Here is a UN-minimize method that takes a WPF window and uses WIN32 to accomplish the effect without recording any state:

WPF的观点是,这是一个OS特性。如果你想搞乱操作系统的功能,你可能要弄脏你的手。幸运的是,他们为我们提供了这样做的工具。这里有一个unminimum方法,它使用WPF窗口并使用WIN32来完成效果,而不记录任何状态:

public static class Win32
{
    public static void Unminimize(Window window)
    {
        var hwnd = (HwndSource.FromVisual(window) as HwndSource).Handle;
        ShowWindow(hwnd, ShowWindowCommands.Restore);
    }

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

    private enum ShowWindowCommands : int
    {
        /// <summary>
        /// Activates and displays the window. If the window is minimized or 
        /// maximized, the system restores it to its original size and position. 
        /// An application should specify this flag when restoring a minimized window.
        /// </summary>
        Restore = 9,
    }
}

#3


9  

SystemCommands class has a static method called RestoreWindow that restores the window to previous state.

systemcommand类有一个名为RestoreWindow的静态方法,可以将窗口恢复到以前的状态。

SystemCommands.RestoreWindow(this); // this being the current window

[Note : SystemCommands class is part of .NET 4.5+ (MSDN Ref) for projects that target to earlier versions of Framework can use the WPF Shell extension (MSDN Ref)]

[注:SystemCommands类是.NET 4.5+ (MSDN Ref)的一部分,用于针对早期版本的框架的项目可以使用WPF外壳扩展(MSDN Ref)]

#4


3  

Here is how i get it to restore right now: I handle the StateChanged event to keep track of the last state that was not Minimized

下面是我如何让它现在恢复:我处理状态更改事件以跟踪未最小化的最后一个状态。

WindowState _lastNonMinimizedState = WindowState.Maximized;
private void Window_StateChanged(object sender, EventArgs e)
{
    if (this.WindowState != System.Windows.WindowState.Minimized)
    {
        _lastNonMinimizedState = WindowState;
    }
}

To restore i then have to set that WindowState respectively:

为了恢复,我必须分别设置窗口状态:

this.WindowState = _lastNonMinimizedState;

#5


1  

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:

嗯,公认的答案对我不起作用。“最大化”窗口,当从任务栏中调用时,最终会以自身为中心(以正常大小显示,即使它的状态是最大化的)在屏幕上显示,而像通过标题栏拖动窗口这样的事情最终会失败。最终(几乎是通过反复试验),我找到了如何去做。由于@H.B。还有@Eric Liprandi,感谢他给我的答案!代码如下:

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

#6


1  

For some reason,

出于某种原因,

WindowState = WindowState.Normal;

didn't work for me. So I used following code & it worked..

没有为我工作。所以我用了下面的代码。

 Show();
 WindowState = WindowState.Normal;

#7


0  

In native Windows you can restore your window to a previous state with ShowWindow(SW_RESTORE):

在本机窗口中,您可以使用ShowWindow(SW_RESTORE)将窗口恢复到以前的状态:

Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.

激活并显示窗口。如果窗口最小化或最大化,系统将它恢复到原来的大小和位置。应用程序应该在还原最小化窗口时指定此标志。

There's surely .Net counterpart to that.

肯定有。net对应的。