views:

2299

answers:

5
+4  A: 

From an API (Win32) perspective there is no such thing as Tasks (at least not the one that Windows Task Manager/Alt-Tab shows).

Those "Tasks" are actually top level windows.

So in order to get a list of those, you need to enumerate the windows (here is the PInvoke for it).

Then look at the style of the windows to determine if they are actually top level windows.

Asher
By enumerating the windows can I restore windows from being minimized?
Joel
A: 

I haven't tried it, but I suspect something like this:

using System.Diagnostics;
static void MyFunc()
{
    Process[] processes = Process.GetProcesses();
    foreach(Process p in processes)
    {
       if (p.MainWindowHandle != 0)
       { // This is a GUI process
       }
       else
       { // this is a non-GUI / invisible process
       }
    }
}

The point is to check each process for a WindowHandle.

abelenky
"If (p.MainWindowHandle != 0)" errors, but "If (p.MainWindowTitle != '')" works, even if it is a bit of a hack.
James Sulak
A: 

@abelenky17

I suspect this will not cover all cases, for example there are processes who have several top level windows (that appear in the task manager). consider for example: FireFox, Windows Explorer, IE, etc... those application can have multiple windows on the desktop/ also, it will not handle Terminal Sessions scenarios properly (because you enumerate all the processes running in the system)

@Dan C.

doing something like that:

p.ProcessName != "explorer"

seems ok to you ? it smells, bad.

Asher
Smelling as it is, I'd like to see the alternatives :)
Dan C.
Agreed. The purpose of answering a question is not to cover all cases, or make code that smells "rosy", but to answer the question: that is, provide enough of the missing information that the original questioner can flush out the details.
abelenky
Correct. Coming back to the question, I've checked and you're right about firefox not spawning a new process for new instances. So if each separate window of the same application is needed, then the only option is to enumerate windows.
Dan C.
+4  A: 

This article should pretty much tell you exactly what to do, it shows how to build your own task switch and includes the code needed to enumerate all windows and determine if they are "tasks" and it shows you how to use PrintWindow api to get the previews on XP.

http://msdn.microsoft.com/en-us/library/ms997649.aspx

Also, here is a blog post that talks about the algorithm used to determine what shows up in the Alt+Tab view. Basically you need to check the WS_EX_APPWINDOW and WS_EX_TOOLWINDOW along with if the window has an owner.

Mo Flanagan
Raymond Chen rulez!!!
Asher