views:

245

answers:

3

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
}
+4  A: 

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.

Reed Copsey
To find out whether a window is maximized you can call IsZoomed.
aquinas
Yeah - nice addition.
Reed Copsey
+1  A: 

Instead of enumerating Processes, you should use the native EnumWindows() function and then call IsIconic.

jeffamaphone
Why? Btw, EnumWindows was my original version, but I changed it to managed code.
AngryHacker
@AngryHacker: You can still P/Invoke into EnumWindows. That's potentially better than trying to use process, unless you're only interested in the main window. You still need P/Invoke for IsIconic (or GetWindowPlacement).
Reed Copsey
I am still unclear as to why EnumWindows is preferred?
AngryHacker
I don't think it matters... I didn't know you could say p.MainWindowHandle, and it was an easy way to get the handles. Whatever works for you.
jeffamaphone
+1  A: 
[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;
     }
    }     
}
aquinas