How could I list all running applications which have a GUI in C#? I am trying to make some sort of a taskbar, so I have to know what processes are running. I can't list all processes of course, because that would be a total disaster. My idea is to only list processes with a gui, because that seems like what's happening in the "stock" taskbar.
Asked
Active
Viewed 1,909 times
0
-
Would a solution that simply lists all the windows on the desktop be ok? – Emond Aug 06 '17 at 18:45
-
If it lists the minimized ones as well, then it would be perfect. If only the ones that are "visible", then it's not really the answer, but it would be also appreciated because I will need that later. – Máté Varga Aug 06 '17 at 18:53
-
See https://stackoverflow.com/questions/7268302/get-the-titles-of-all-open-windows and https://stackoverflow.com/questions/1032933/enumerate-all-window-handles-on-desktop – Emond Aug 06 '17 at 18:57
-
See my answer, it also lists minimized windows. – IS4 Aug 06 '17 at 18:58
2 Answers
0
You can list all processes that have a main window:
static IEnumerable<Process> WindowProcesses()
{
foreach(var proc in Process.GetProcesses())
{
if(proc.MainWindowHandle != IntPtr.Zero)
{
yield return proc;
}
}
}
This will enumerate all processes that have a main window, but may not necessarily list all processes that have any visible windows (e.g. services). For that, you need to P/Invoke EnumWindows and GetWindowThreadProcessId.

IS4
- 11,945
- 2
- 47
- 86
-1
This code will fill a dropdown and set get it on foreground by selection, you can do it like this
[DllImport("User32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private void ListTask()
{
Process[] processes = Process.GetProcesses();
foreach (Process process in Process.GetProcesses().
Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).ToList())
listBox1.Items.Add(process.ProcessName);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Process p = new Process();
string name = listBox1.SelectedItem.ToString();
p = Process.GetProcessesByName(name)[0];
const uint SWP_SHOWWINDOW = 0x0001;
ShowWindow(p.MainWindowHandle, SWP_SHOWWINDOW);
SetForegroundWindow(p.MainWindowHandle);
p.Dispose();
}

Ghulam Mohayudin
- 1,093
- 10
- 18
-
1) Enumerates all processes, while the questioner only wanted those that have a GUI. 2) Creates a rendundant instance at `new Process()`. 3) `GetProcessesByName` may be ambiguous and return an incorrect process. – IS4 Aug 06 '17 at 18:52
-