You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Windows 10任务栏上显示的应用图标列表?

获取Windows任务栏图标数量的实现方案

想要实现任务栏图标居中,核心第一步是获取任务栏上的应用图标数量/列表,以下是不同技术栈的实现方案:

Python 实现(兼容Windows 10/11)

使用pywinauto库通过UI自动化获取,兼容性更强:

  1. 安装依赖
pip install pywinauto
  1. 代码示例
from pywinauto import Desktop

def get_taskbar_icon_list():
    desktop = Desktop(backend="uia")
    # 定位任务栏
    taskbar = desktop.window(class_name="Shell_TrayWnd")
    # 找到任务栏的图标按钮区域(Windows 11对应类名)
    button_area = taskbar.child_window(class_name="Taskbar.TaskbarButtonArea", control_type="ToolBar")
    # 获取所有图标按钮
    icon_buttons = button_area.children(control_type="Button")
    # 返回数量和图标列表(可获取图标对应的窗口标题)
    return len(icon_buttons), [btn.window_text() for btn in icon_buttons]

if __name__ == "__main__":
    icon_count, icon_list = get_taskbar_icon_list()
    print(f"任务栏图标数量:{icon_count}")
    print("图标对应应用:", icon_list)

Python 原生API实现(适合Windows 10及更早)

通过pywin32调用Windows原生API:

  1. 安装依赖
pip install pywin32
  1. 代码示例
import win32gui
import win32con

def get_taskbar_icon_count():
    # 获取任务栏句柄
    taskbar_handle = win32gui.FindWindow("Shell_TrayWnd", None)
    if not taskbar_handle:
        return 0
    # 定位任务栏的工具栏控件
    toolbar_handle = win32gui.FindWindowEx(taskbar_handle, None, "ToolbarWindow32", None)
    if not toolbar_handle:
        return 0
    # 发送消息获取按钮数量
    return win32gui.SendMessage(toolbar_handle, win32con.TB_BUTTONCOUNT, 0, 0)

if __name__ == "__main__":
    print(f"任务栏图标数量:{get_taskbar_icon_count()}")

C#/.NET 实现

通过P/Invoke调用Windows API,适合桌面应用开发:

using System;
using System.Runtime.InteropServices;

class TaskbarHelper
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    private const uint TB_BUTTONCOUNT = 0x0418;

    public static int GetTaskbarIconCount()
    {
        IntPtr taskbarHandle = FindWindow("Shell_TrayWnd", null);
        if (taskbarHandle == IntPtr.Zero) return 0;

        IntPtr toolbarHandle = FindWindowEx(taskbarHandle, IntPtr.Zero, "ToolbarWindow32", null);
        if (toolbarHandle == IntPtr.Zero) return 0;

        return SendMessage(toolbarHandle, TB_BUTTONCOUNT, 0, 0);
    }

    static void Main()
    {
        Console.WriteLine($"任务栏图标数量:{GetTaskbarIconCount()}");
    }
}

注意事项

  • Windows 11任务栏结构有调整,优先使用UI自动化方案(如pywinauto),能更稳定地获取图标列表;
  • 若需要实现图标居中,获取数量后可通过调整任务栏布局参数或模拟拖拽实现,具体需结合系统API或第三方工具库。

内容的提问来源于stack exchange,提问作者Axen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 12:30:44