[英]Invoking windows task manager with 'performance' tab selected
我目前正在使用 WPF 中的单击事件调用 Windows 任务管理器。 该事件只是执行'Process.Start("taskmgr")。
我的问题是,有没有办法选择在进程启动/显示时选择任务管理器中的哪个选项卡? 我希望在引发点击事件时自动选择“性能”选项卡。
谢谢您的帮助。
为了扩展 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();
}
}
}
为什么我要销毁以前的任务管理器实例? 当一个实例已经打开时,辅助实例将打开但立即关闭。 我的代码不会对此进行检查,因此找到窗口句柄的代码将冻结。
虽然 taskmgr.exe 没有任何命令行参数来指定所选选项卡,但您可以使用Windows UI 自动化“导航”到性能选项卡。
不幸的是, taskmgr.exe
不支持任何命令行参数。
运行时,它将始终激活上次关闭时处于活动状态的选项卡。
从 Windows 10 内部版本 18305 开始,您现在可以设置首选选项卡以默认打开任务管理器。
更新:
更新后,更改Win注册表项中StartUpTab
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 中测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.