【问题标题】:C# - How do I programatically minimize the topmost window?C# - 如何以编程方式最小化最顶层的窗口?
【发布时间】:2017-04-17 02:53:41
【问题描述】:

我做了一些研究,但我发现的大多数/所有答案都会最小化所有窗口或带有known window name/process name 的窗口(链接涵盖两者)。

但是如果我只想最小化最上面的窗口呢?我该怎么做呢?我不知道从哪里开始。

【问题讨论】:

  • 再退一步,研究寻找最顶部窗口的window name/process name .. 然后你就知道接下来会发生什么了
  • 这里有一个关于获取最顶层窗口的问题:stackoverflow.com/questions/1000847/…

标签: c# windows user-interface window


【解决方案1】:

你可以试试这样的。

  [DllImport("user32.dll")]
  private static extern IntPtr GetForegroundWindow();

  [DllImport("user32.dll")]
  internal static extern short GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

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

  public struct RECT
  {
    public RECT(int l, int t, int r, int b)
    {
      Left = l;
      Top = t;
      Right = r;
      Bottom = b;
    }

    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
  }

  public struct POINT
  {
    public POINT(int x, int y)
    {
      X = x;
      Y = y;
    }

    public int X;
    public int Y;

  }



  public struct WINDOWPLACEMENT
      {
        public int length;
        public int flags;
        public ShowWindowEnum showCmd;
        public POINT ptMinPosition;
        public POINT ptMaxPosition;
        public RECT rcNormalPosition;
      }

     public enum ShowWindowEnum : uint
     {
        /// <summary>
        /// Activates the window and displays it as a minimized window.
        /// </summary>
        SW_SHOWMINIMIZED = 2,
     }

     private void MinimizeForegroundWindow()
     {
        WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
        GetWindowPlacement(_handle, ref placement);
        if (placement.showCmd != ShowWindowEnum.SW_SHOWMINIMIZED)
        {
            ShowWindow(_handle, ShowWindowEnum.SW_SHOWMINIMIZED);
        }
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多