获取任务栏中的应用程序数量

发布于 2024-08-27 01:35:28 字数 110 浏览 8 评论 0原文

多年来我一直想知道如何做到这一点。我正在创建一个小应用程序,我需要弄清楚任务栏中显示了多少个应用程序或窗口。

我还没有找到任何有关此的信息,我将不胜感激任何帮助。

谢谢 :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杀手六號 2024-09-03 01:35:29

这里是一篇文章,介绍了如何获取窗口,当您使用 ALT+TAB 组合键时显示。

基本上,您将获得与任务栏中显示的相同窗口(除非它是未显示的工具窗口),但话又说回来,您始终可以检查 WS_EX_TOOLWINDOW (未显示)和 <强>WS_EX_APPWINDOW(如图所示)。

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

丑丑阿 2024-09-03 01:35:29

正如其他人所说,您需要使用 Win32 EnumWindows 函数枚举窗口,并以这种方式获得计数。

您还可以使用 Process.GetProcesses(); 枚举进程,但是像资源管理器窗口这样不是单独进程的窗口不会显示在该列表中。

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 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);
深爱不及久伴 2024-09-03 01:35:29

您可以查看我之前的答案 此处;这里的主要区别是您只需计算符合给定要求的窗口即可。

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.

后来的我们 2024-09-03 01:35:29

据我所知,没有管理方法可以访问任务栏。这里有一个链接,描述了如何通过 Windows API 访问任务栏。但是,我快速扫描没有显示任何“项目数量”或类似的内容。尽管如此,它可能会为您指明正确的方向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文