簡體   English   中英

在選擇了“性能”選項卡的情況下調用 Windows 任務管理器

[英]Invoking windows task manager with 'performance' tab selected

我目前正在使用 WPF 中的單擊事件調用 Windows 任務管理器。 該事件只是執行'Process.Start("taskmgr")。

我的問題是,有沒有辦法選擇在進程啟動/顯示時選擇任務管理器中的哪個選項卡? 我希望在引發點擊事件時自動選擇“性能”選項卡。

謝謝您的幫助。

為了擴展 Philipp Schmid 的帖子,我制作了一個小演示:

將其作為控制台應用程序運行。 您需要添加對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不支持任何命令行參數。

運行時,它將始終激活上次關閉時處於活動狀態的選項卡。

從 Windows 10 內部版本 18305 開始,您現在可以設置首選選項卡以默認打開任務管理器。

更新:

  • 單擊開始菜單,然后在搜索框中鍵入“Windows 更新”
  • 選擇“Windows 更新設置”
  • 在左側面板中單擊“預覽版本”
  • 現在點擊“檢查”。
  • 下載新版本。

更新后,更改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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM