Get number of apps in TaskBar

I've been wondering how to do this for ages. I'm creating a little app, and I need to figure out how many apps or windows are displayed in the TaskBar.

I've yet to find any info on this at all, I'd appreciate any help at all.

Thank you :)

Answers


As other's have said you need to enumerate through the windows using the Win32 EnumWindows function, and get your count that way.

You can also enumerate through processes using Process.GetProcesses(); However windows like explorer windows which are not a separate process will not show up in that list.

int appCount = 0;

public bool EnumerateWindows(IntPtr hwnd, IntPtr lParam)
{
    if (IsWindowVisible(hwnd))
    {
        StringBuilder sb = new StringBuilder();
        string text = "";

        GetWindowText(hwnd, sb, 1024);
        text = sb.ToString();

        if (text != string.Empty && text != "Program Manager")
        {
            appCount++;
        }
    }

    return true;
}

private int GetAppCount()
{
    appCount = 0;
    EnumWindows(EnumerateWindows, new IntPtr(0));

    return appCount;
}

internal delegate bool EnumThreadWindowsCallback(IntPtr hwnd, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern bool IsWindowVisible(IntPtr hwnd);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);

Here is an article that shows how to get the windows, that are shown when you are using the ALT+TAB key combination.

Basically, you will get the same windows that are shown in the taskbar (unless it is a tool window that is not displayed), but then again, you can always check against WS_EX_TOOLWINDOW (not shown) and WS_EX_APPWINDOW (shown).


You may have a look at my previous answer here; the main difference here is that you just have to count the windows that match the given requirements.


As far as I know there is no managed way of accessing the taskbar. Here is a link that describes how to access the taskbar by the Windows API. However, I a quick scan did not show any "number of items" or something similar. Still it might point you in the right direction.


Need Your Help

Can the Eclipse Javascript formatter be set up for JSLint compliance?

javascript eclipse coding-style jslint

I can't seem to configure the Eclipse Javascript formatting options so that it generates markup JSLint doesn't complain about - primarily whitespace settings when the "tolerate sloppy whitespace" o...