簡體   English   中英

C#-如何以編程方式最小化最上面的窗口?

[英]C# - How do I programatically minimize the topmost window?

我已經進行了一些研究,但是我發現的大多數/所有答案都將最小化所有窗口或具有已知窗口名稱/進程名稱的窗口 (鏈接涵蓋了兩者)。

但是,如果我只想最小化最上面的窗口怎么辦? 我將如何去做? 我不確定從哪里開始。

您可以嘗試這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM