在选择了“性能"选项卡的情况下调用 Windows 任务管理器 [英] Invoking windows task manager with 'performance' tab selected

查看:72
本文介绍了在选择了“性能"选项卡的情况下调用 Windows 任务管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently invoking the windows task manager using a click event in WPF. The event simply executes 'Process.Start("taskmgr").

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

My question is, is there a way to choose which tab inside task manager is selected when the process starts / is displayed? I am looking to have the 'performance' tab selected automatically whenever the click event is raised.

感谢您的帮助.

推荐答案

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

To expand on Philipp Schmid's post, I've whipped up a little demo:

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

Run it as a console application. You need to add references to UIAutomationClient and UIAutomationTypes.

您(或我,如果您愿意)可以进行的一项可能改进是最初隐藏窗口,仅在选择了正确的选项卡后才显示它.但是,我不确定这是否可行,因为我不确定 AutomationElement.FromHandle 是否能够找到隐藏的窗口.

One possible improvement you (or I, if you desire) can make is to hide the window initially, only showing it after the correct tab has been selected. I'm not sure if that would work, however, as I'm not sure that AutomationElement.FromHandle would be able to find a hidden window.

至少在我的计算机(Windows 7,32 位,.Net framework 4.0)上,以下代码最初创建了一个隐藏的任务管理器,并在选择了正确的选项卡后显示它.选择性能选项卡后,我没有明确显示窗口,因此其中一条自动化线路可能会产生副作用.

At least on my computer (Windows 7, 32 bit, .Net framework 4.0), the following code initially creates a hidden Task Manager and shows it after the correct tab has been selected. I don't explicitly show the window after selecting the performance tab, so probably one of the automation lines does as a side-effect.

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();
        }
    }
}

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

Why do I destroy previous instances of Task Manager? When an instance is already open, secondary instances will open but immediately close. My code doesn't check for this, so the code that finds the window handle will freeze.

这篇关于在选择了“性能"选项卡的情况下调用 Windows 任务管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆