从最小化恢复WindowState

时间:2008-12-09 22:16:12

标签: winforms

是否有一种简单的方法可以将最小化的表单恢复到以前的状态,正常还是最大化?我期待与单击任务栏相同的功能(或右键单击并选择还原)。

到目前为止,我有这个,但如果表格之前已经最大化,它仍然会作为普通窗口返回。

if (docView.WindowState == FormWindowState.Minimized)
    docView.WindowState = FormWindowState.Normal;

我是否必须处理表单中的状态更改以记住以前的状态?

9 个答案:

答案 0 :(得分:39)

我使用以下扩展方法:

using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
    public static class Extensions
    {
        [DllImport( "user32.dll" )]
        private static extern int ShowWindow( IntPtr hWnd, uint Msg );

        private const uint SW_RESTORE = 0x09;

        public static void Restore( this Form form )
        {
            if (form.WindowState == FormWindowState.Minimized)
            {
                ShowWindow(form.Handle, SW_RESTORE);
            }
        }
    }
}

然后在我的代码中调用form.Restore()

答案 1 :(得分:13)

将表单恢复到正常状态的最简单方法是:

if (MyForm.WindowState == FormWindowState.Minimized)
{
    MyForm.WindowState = FormWindowState.Normal;
}

答案 2 :(得分:4)

您可以模拟单击任务栏按钮,如下所示:

SendMessage(docView.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);

答案 3 :(得分:4)

对我来说,上面的代码不起作用。

但最后我发现了工作代码。这是:

CxImports.ManagedWindowPlacement placement = new CxImports.ManagedWindowPlacement();
CxImports.GetWindowPlacement(Convert.ToUInt32(Handle.ToInt64()), placement);

if (placement.flags == CxImports.WPF_RESTORETOMAXIMIZED)
    WindowState = FormWindowState.Maximized;
else
    WindowState = FormWindowState.Normal;

我想,你可以通过简单的谷歌搜索找到所有需要的“导入”功能。

答案 4 :(得分:1)

如果有人想知道如何在其他应用程序窗口中执行此操作,则此代码对我有用:

    public void UnMinimize(IntPtr handle)
    {
        WINDOWPLACEMENT WinPlacement = new WINDOWPLACEMENT();
        GetWindowPlacement(handle, out WinPlacement);
        if(WinPlacement.flags.HasFlag(WINDOWPLACEMENT.Flags.WPF_RESTORETOMAXIMIZED))
        {
            ShowWindow(handle, (int)SW_MAXIMIZE);
        }
        else
        {
            ShowWindow(handle, (int)SW_RESTORE);
        }
    }

东西在这里:

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public Int32 Left;
    public Int32 Top;
    public Int32 Right;
    public Int32 Bottom;
}

public struct POINT
{
    public int x;
    public int y;
}

public struct WINDOWPLACEMENT
{

    [Flags]
    public enum Flags : uint
    {
        WPF_ASYNCWINDOWPLACEMENT = 0x0004,
        WPF_RESTORETOMAXIMIZED = 0x0002,
        WPF_SETMINPOSITION = 0x0001
    }


    /// <summary>
    /// The length of the structure, in bytes. Before calling the GetWindowPlacement or SetWindowPlacement functions, set this member to sizeof(WINDOWPLACEMENT).
    /// </summary>
    public uint length;
    /// <summary>
    /// The flags that control the position of the minimized window and the method by which the window is restored. This member can be one or more of the following values.
    /// </summary>
    /// 
    public Flags flags;//uint flags;
                       /// <summary>
                       /// The current show state of the window. This member can be one of the following values.
                       /// </summary>
    public uint showCmd;
    /// <summary>
    /// The coordinates of the window's upper-left corner when the window is minimized.
    /// </summary>
    public POINT ptMinPosition;
    /// <summary>
    /// The coordinates of the window's upper-left corner when the window is maximized.
    /// </summary>
    public POINT ptMaxPosition;
    /// <summary>
    /// The window's coordinates when the window is in the restored position.
    /// </summary>
    public RECT rcNormalPosition;
}

public class UnMinimizeClass
{
    [DllImport("user32.dll")]
    public static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);

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

    const int SW_MAXIMIZE = 3;
    const int SW_RESTORE = 9;

    public static void UnMinimize(IntPtr handle)
    {
        WINDOWPLACEMENT WinPlacement = new WINDOWPLACEMENT();
        GetWindowPlacement(handle, out WinPlacement);
        if (WinPlacement.flags.HasFlag(WINDOWPLACEMENT.Flags.WPF_RESTORETOMAXIMIZED))
        {
            ShowWindow(handle, SW_MAXIMIZE);
        }
        else
        {
            ShowWindow(handle, (int)SW_RESTORE);
        }
    }
}

答案 5 :(得分:0)

    [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, ref wndRect lpRect);
    [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
    [DllImport("user32.dll")] public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);//用来遍历所有窗口 
    [DllImport("user32.dll")] public static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);//获取窗口Text 
    [DllImport("user32.dll")] public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);//获取窗口类名 

    public static List<wndInfo> GetAllDesktopWindows(bool? isVisitable_)
    {
        //用来保存窗口对象
        List<wndInfo> wndList = new List<wndInfo>();

        //enum all desktop windows 
        EnumWindows(delegate (IntPtr hWnd, int lParam)
        {
            wndInfo wnd = new wndInfo();
            StringBuilder sb = new StringBuilder(256);
            //get hwnd 
            wnd.hWnd = hWnd;
            if (isVisitable_ == null || IsWindowVisible(wnd.hWnd) == isVisitable_)
            {
                //get window name  
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.szWindowName = sb.ToString();

                //get window class 
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.szClassName = sb.ToString();

                wndList.Add(wnd);
            }
            return true;

        }, 0);

        return wndList;
    }

    private void Btn_Test5_Click(object sender, RoutedEventArgs e)
    {
        var ws = WSys.GetAllDesktopWindows(true);
        foreach (var w in ws)
        {
            if (w.szWindowName == "计算器")
            {
                WSys.ShowWindow(w.hWnd, 5);
                WSys.ShowWindow(w.hWnd, 9);
                Log.WriteLine(w.szWindowName);
            }
        }
    }

答案 6 :(得分:0)

仅使用Could not create config directory: mkdir /home/sbx_user1051: permission denied.

下一种方法适用于我的WPF应用程序

MainWindow.WindowState = WindowState.Normal;

答案 7 :(得分:0)

我又添加了一件,以扩大@Mesmo提供的解决方案。如果尚未创建该实例,则它将创建该实例;如果已从应用程序中的任何位置创建了该实例,它将还原表单。我的要求是,我不想为应用程序中的某些功能打开多个表单。

实用程序类:

public static class Utilities
{
  [DllImport("user32.dll")]
  private static extern int ShowWindow(IntPtr hWnd, uint Msg);

  private const uint SW_RESTORE = 0x09;

  public static void Restore(this Form form)
  {
    if (form.WindowState == FormWindowState.Minimized)
    {
      ShowWindow(form.Handle, SW_RESTORE);
    }
  }

  public static void CreateOrRestoreForm<T>() where T: Form
  {
    Form form = Application.OpenForms.OfType<T>().FirstOrDefault();

    if (form == null)
    {
      form = Activator.CreateInstance<T>();
      form.Show();
    }
    else
    {
      form.Restore();
      form.Focus();
    }
  }
}

用法:

Utilities.CreateOrRestoreForm<AboutForm>();

答案 8 :(得分:-1)

上述代码在所有情况下都不适合我

检查标志后,我还必须检查showcmd = 3,如果是,则最大化其他恢复