中心任务栏图标与注册表

Center taskbar icons with the registry

我想将图标放在桌面中央的任务栏 windows10 上。 我知道著名的 TaskbarX 可以,但我不知道如何在 python 中执行,请有人向我解释或给我修改注册表中的路径

Example of center icons taskbar

Windows 10 上,您 无法仅通过修改 Windows 注册表 来使图标居中。

但在 Windows 11 上,您只需修改 TaskbarAl(0 = 左,1 = 居中)值 \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced.

TaskbarX 和其他类似工具对 Windows 10 和更早版本的作用是使用 UIAutomation 或 MSAA 等辅助功能计算任务栏图标位置。

如上图所示,使用微软的UI Accessibility Checker工具,显示了explorer.exe进程的MSAA树,特别是持有taskbar的实例。

首先,找到任务栏实例,使用EnumWindows and pick those beloging to explorer.exe process that has as Win Class Name "Shell_TrayWnd" (for primary monitor) and "Shell_SecondaryTrayWnd" (for the other monitors). Once you found them, using that tool as a guide, find the exact running applications list in that accessibility tree, for each taskbar instance (main monitor and secondary monitors). Once found the exact location of the taskbar list icons, pick the first one's left position and then the last one's right position. With that coordinates you will get its real width inside taskbar window, and using SetWindowPos修改其在任务栏区域中的位置,使用其childwindows容器。

在上图中,可以看到childWindows在主任务栏的Shell_TrayWndwindow层级句柄,包含 运行 应用程序的 child 名为 ReBarWindow32,它在任务栏 window 中有自己的坐标,您只需 修改它 使用计时器或 shell 事件,每当新进程启动,或现有进程关闭,或切换到另一个虚拟桌面,以及其他事件,所有这些都是为了 re-adjust它的位置(以你的情况为中心)。

通常情况下,使用定时器会不必要地轮询系统,建议使用Windows任务栏本身依赖的shell消息,如这里所述https://devblogs.microsoft.com/oldnewthing/20201228-00/?p=104610 you register your process as a shell message client or hook the shell message https://docs.microsoft.com/en-us/windows/win32/winmsg/about-hooks,这取决于在你身上。

您可以看一下 PowerToys 源代码,特别是 ShortCutGuide 模块,它用数字显示了任务栏的图标位置。 https://github.com/microsoft/PowerToys/blob/master/src/modules/ShortcutGuide/ShortcutGuide/tasklist_positions.cpp 找到一种方法将其移植到 Python 并按上述步骤进行。

我再说一遍,Windows 10,与 Windows 11 不同, 不提供开箱即用的居中功能。

如果您看到最后一张图片,则该快照属于 Windows 11 的任务栏 中的 child window 层次结构,但如果您将与您的 Windows 10、7、8 等(使用 UUSpy 工具或 Spy++)进行比较,您会注意到这张图片显示额外的 child windows,特别是使用 Xaml 字符串在其 class 名称中。这些是 新 children,现在不仅包含任务列表,还包含“开始”菜单按钮、搜索、小部件等 。在 Windows 10 或更早的版本中找不到它们。但是,Windows 11 仍然保留旧列表 child window“ReBarWindow32”(在以前的 Windows 版本中使用的列表)但它是隐藏的 有利于新的,我猜他们将来会删除它,因为这个新的在启动或关闭新应用程序时会进行居中和动画等。