使用 '性能' 选项卡打开 Windows 任务管理器

9

我目前正在使用WPF的点击事件调用Windows任务管理器。 该事件仅执行“Process.Start(“taskmgr”)”。

我的问题是,是否有一种方法可以在进程启动/显示时选择任务管理器内选择哪个标签? 我希望在引发单击事件时自动选择“性能”选项卡。

感谢您的帮助。

4个回答

7

针对Philipp Schmid的帖子进行补充,我做了一个小demo:

将其作为控制台应用程序运行。你需要添加对UIAutomationClient和UIAutomationTypes的引用。

一个可能的改进是在选择正确的标签页后才显示窗口,并且最初隐藏窗口。然而,我不确定这是否有效,因为我不确定AutomationElement.FromHandle是否能够找到隐藏的窗口。

编辑:至少在我的电脑上(Windows 7,32位,.Net framework 4.0),以下代码最初创建了一个隐藏的任务管理器,并在选择正确的标签页后将其显示出来。在选择性能选项卡之后,我没有明确地显示窗口,所以很可能一些自动化行可能因副作用而实现。

using System;
using System.Diagnostics;
using System.Windows.Automation;

namespace ConsoleApplication2 {
    class Program {
        static void Main(string[] args) {
            // Kill existing instances
            foreach (Process pOld in Process.GetProcessesByName("taskmgr")) {
                pOld.Kill();
            }

            // Create a new instance
            Process p = new Process();
            p.StartInfo.FileName = "taskmgr";
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            Console.WriteLine("Waiting for handle...");

            while (p.MainWindowHandle == IntPtr.Zero) ;

            AutomationElement aeDesktop = AutomationElement.RootElement;
            AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle);
            Console.WriteLine("Got handle");

            // Get the tabs control
            AutomationElement aeTabs = aeForm.FindFirst(TreeScope.Children,
  new PropertyCondition(AutomationElement.ControlTypeProperty,
    ControlType.Tab));

            // Get a collection of tab pages
            AutomationElementCollection aeTabItems = aeTabs.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty,
    ControlType.TabItem));

            // Set focus to the performance tab
            AutomationElement aePerformanceTab = aeTabItems[3];
            aePerformanceTab.SetFocus();
        }
    }
}

为什么我要销毁之前的任务管理器实例?当一个实例已经打开时,次要实例会打开但立即关闭。我的代码没有检查这一点,所以寻找窗口句柄的代码会冻结。


感谢您提供快速解决方案。知道这可以“技术上”完成后,我会更深入地研究自动化并看看我能做些什么。 - d.moncada
很高兴能够帮助。上面的代码应该适合生产使用,只要您添加一些错误检查即可。在您提问之前,我实际上对这些托管自动化工具一无所知。因此,感谢您意外地向我介绍它们 :) - Chris Laplante
1
另外,如果你使用我的代码,请确保将 while 循环替换为...更好的内容。它有可能无限循环。 - Chris Laplante

2

虽然taskmgr.exe没有任何命令行参数来指定选定的标签页,但是您可以使用Windows UI Automation来“导航”到性能标签页。


1

非常遗憾,taskmgr.exe 不支持任何命令行参数。

运行时,它将始终激活上次关闭时处于活动状态的选项卡。


7
一种丑陋的方法是通过注册表在“HKCU/Software/Microsoft/Windows NT/CurrentVersion/TaskManager”中更改最后活动标签。 - user703016
taskmgr支持CLI参数。 - Zimba

0

从Windows 10的版本18305开始,您现在可以将首选选项卡设置为默认打开任务管理器。

更新方法:

  • 单击“开始菜单”,在搜索框中键入“Windows Update”
  • 选择“Windows更新设置”
  • 在左侧面板中单击“预览构建”
  • 立即单击“检查”。
  • 下载新的构建。

更新后,在Win注册表键 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\TaskManager 中更改StartUpTab的dword值:

0 – Processes tab
1 – Performance tab
2 – App history tab
3 – Startup tab
4 – Users tab
5 – Details tab
6 – Services tab

Win CMD:
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "startup" /t REG_DWORD /d "1"

此(实验性)功能仅适用于某些 Windows Insiders。

旧版本的 Win 10 仅支持“启动”选项卡,不支持其他选项卡:
taskmgr /4 /startup

重置操作:
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "Preferences" /f

确认修改后的键:
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager" /f & regedit

在 Win 10 CMD 中测试通过


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接