0

I've done some research, but most/all of the answers I found would minimize either ALL windows or a window with a known window name/process name (link covers both).

But what if I only want to minimize the topmost window? How would I go about doing that? I'm not sure where to start.

Community
  • 1
  • 1
Aloha
  • 864
  • 18
  • 40
  • 1
    Go one more step back and do research on finding the `window name/process name` of the top most window .. then u know whats next – Mohit S Dec 02 '16 at 08:55
  • There is a question here regarding getting the top-most window: http://stackoverflow.com/questions/1000847/how-to-get-the-handle-of-the-topmost-form-in-a-winform-app – LordWilmore Dec 02 '16 at 08:58

1 Answers1

0

You can try something like this.

  [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);
        }
     }
Davy
  • 29
  • 5