8

我目前正在使用 WPF 中的单击事件调用 Windows 任务管理器。该事件只是执行'Process.Start("taskmgr")。

我的问题是,有没有办法选择在进程启动/显示时选择任务管理器中的哪个选项卡?我希望在引发点击事件时自动选择“性能”选项卡。

谢谢您的帮助。

4

4 回答 4

7

为了扩展 Philipp Schmid 的帖子,我做了一个小演示:

将其作为控制台应用程序运行。您需要添加对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();
        }
    }
}

为什么我要销毁以前的任务管理器实例?当实例已经打开时,辅助实例将打开但立即关闭。我的代码没有对此进行检查,因此找到窗口句柄的代码将冻结。

于 2011-08-08T19:18:32.013 回答
2

虽然 taskmgr.exe 没有任何命令行参数来指定所选选项卡,但您可以使用Windows UI 自动化“导航”到性能选项卡。

于 2011-08-08T19:10:14.167 回答
1

不幸的是,taskmgr.exe不支持任何命令行参数。

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

于 2011-08-08T19:02:41.677 回答
0

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

更新:

  • 单击开始菜单,然后在搜索框中键入“Windows 更新”
  • 选择“Windows 更新设置”
  • 在左侧面板中单击“预览构建”
  • 现在点击“检查”。
  • 下载新版本。

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

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

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

此(实验性)功能仅适用于某些 Windows 预览体验成员。

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 中测试

于 2020-04-06T14:41:57.793 回答