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

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

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

谢谢您的帮助。

为了扩展Philipp Schmid的post,我掀起了一个小小的演示:

将其作为控制台应用程序运行。 您需要添加对UIAutomationClientUIAutomationTypes引用。

您(或我,如果您愿意)可以做的一个可能的改进是最初隐藏窗口,仅在选择了正确的选项卡后显示它。 但是,我不确定这是否可行,因为我不确定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(); } } } 

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

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

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

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