Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether another app is minimized or not?

Tags:

c#

How can I check whether another application is minimized or not? For instance in a loop like this:

foreach(Process p in processes)
{
  // Does a process have a window?
  // If so, is it minimized, normal, or maximized
}
like image 907
AngryHacker Avatar asked Jun 16 '09 18:06

AngryHacker


People also ask

How do you know when an app is minimized?

How to detect when an Android app goes to the background? onPause() or onUserLeaveHint() works but are also called when the orientation is changed or another activity is presented.


4 Answers

[DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

    private struct WINDOWPLACEMENT {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }

if (p.MainWindowHandle != IntPtr.Zero) {
    if (p.MainWindowTitle.Contains("Notepad")) {
        WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
        GetWindowPlacement(p.MainWindowHandle, ref placement);
        switch (placement.showCmd) {
           case 1:
             Console.WriteLine("Normal");
             break;
           case 2:
             Console.WriteLine("Minimized");
             break;
           case 3:
             Console.WriteLine("Maximized");
             break;
        }
    }                   
}
like image 102
aquinas Avatar answered Nov 05 '22 19:11

aquinas


There is no such thing as a minimized "application." The best alternative would be to check whether the application's Main Window is Iconic (minimized).

IsIconic can be used to check for the iconic state of a window. It will return 1 if a window is minimized. You can call this with process.MainWindowHandle.

like image 40
Reed Copsey Avatar answered Nov 05 '22 20:11

Reed Copsey


If a window is minimized (in Windows Forms at least) both Location.X and Location.Y values are -32000

like image 34
CMarsden Avatar answered Nov 05 '22 21:11

CMarsden


you can use isZoomed for maximized and isIconic for minimized by injecting user32 dll

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsZoomed(IntPtr hWnd);
like image 28
Shahin Shemshian Avatar answered Nov 05 '22 19:11

Shahin Shemshian