选中"性能"选项卡调用Windows任务管理器

d.m*_*ada 7 c# wpf process taskmanager

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

我的问题是,有没有办法在流程开始/显示时选择任务管理器中的哪个选项卡?我希望在引发click事件时自动选择"性能"选项卡.

谢谢您的帮助.

Chr*_*nte 6

为了扩展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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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