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
}
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.
[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;
}
}
}
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.
If a window is minimized (in Windows Forms at least) both Location.X and Location.Y values are -32000
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With