开发者

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 :)


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 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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜