我目前正在使用WPF的点击事件调用Windows任务管理器。 该事件仅执行“Process.Start(“taskmgr”)”。
我的问题是,是否有一种方法可以在进程启动/显示时选择任务管理器内选择哪个标签? 我希望在引发单击事件时自动选择“性能”选项卡。
感谢您的帮助。
我目前正在使用WPF的点击事件调用Windows任务管理器。 该事件仅执行“Process.Start(“taskmgr”)”。
我的问题是,是否有一种方法可以在进程启动/显示时选择任务管理器内选择哪个标签? 我希望在引发单击事件时自动选择“性能”选项卡。
感谢您的帮助。
针对Philipp Schmid的帖子进行补充,我做了一个小demo:
将其作为控制台应用程序运行。你需要添加对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 10的版本18305开始,您现在可以将首选选项卡设置为默认打开任务管理器。
更新方法:
更新后,在Win注册表键 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\TaskManager
中更改StartUpTab
的dword值:
0 – Processes tab
1 – Performance tab
2 – App history tab
3 – Startup tab
4 – Users tab
5 – Details tab
6 – Services tab
Win CMD:
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "startup" /t REG_DWORD /d "1"
此(实验性)功能仅适用于某些 Windows Insiders。
旧版本的 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 中测试通过