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 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
}
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.
Instead of enumerating Processes, you should use the native EnumWindows() function and then call IsIconic.
[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;
}
}
}