【问题标题】:Obtaining tab title/text using process ID使用进程 ID 获取选项卡标题/文本
【发布时间】:2015-08-14 15:05:42
【问题描述】:

我不想使用SetForegroundWindow()、发送键盘按键或类似技术,因为这可能会导致我的软件出现问题(意外行为)。
我曾尝试使用 Cheat Engine 程序找到标题(但没有发现任何有用的东西,因为 Google Chrome 似乎“颠倒”工作)。

所以我向前迈了一步,使用 Process Hacker 程序,我意识到有一个父 (chrome.exe) 进程具有当前活动选项卡的有效窗口句柄,所有其他 chrome 进程都是它的子进程,也就是后台进程(窗口无效句柄)。
通过深入浏览 chrome.exe(父进程)的窗口,我发现窗口句柄的类名是“Chrome_WidgetWin_1”和当前活动选项卡的标题/文本。

Here's Google Chrome 的图片任务管理器。

我正在寻找 C# 或 C 或 C++ 中的函数,该函数将采用整数(进程 ID)并返回字符串(选项卡标题/文本)。

static string GetChromeTabTitle(uint processId)
{
    // Assuming I call this function with valid process identifier (PID).
    // What do I do next, here??
}

【问题讨论】:

  • 你想通过获取 Chrome 标签的文本来做什么?
  • 另一种考虑的方法:给定一个进程,获取 DOM 对象并检索title 标签。
  • @JamesThorpe 这听起来确实是个好主意。但我不知道从哪里开始以及如何开始..
  • 我也不是 - 只是想在您研究时强调它是一种潜在的替代方案 :)
  • 在 chrome://memory-internals/ 和 chrome://memory-redirect/ 页面上有有用的信息(链接只能从 Google Chrome 网络浏览器查看)。但是,没有这样的 API(据我所知 - 截至 2015 年 8 月 14 日)可以直接从 3rd 方应用程序/代码中“提取/访问”这些信息。伤心:-(

标签: c# google-chrome


【解决方案1】:

我发现最好的方法是使用System.Windows.Automation 库。它允许与应用程序进行交互(主要用于辅助目的),但您可以将其用于其他目的,例如获取 Chrome 标签。

请注意,这在 Chrome 窗口未最小化时有效。

这个过程并不简单,如果你想你可以看看我在我自己的项目中是怎么做的,虽然它不是你可以复制粘贴的东西,你会在ChromeTabsFinder中找到你需要的:@ 987654322@

这是代码(您需要自动化库):

public IEnumerable<ITab> GetTabsOfWindow(IntPtr hWnd)
{
  var cacheRequest = new CacheRequest();
  cacheRequest.Add(AutomationElement.NameProperty);
  cacheRequest.Add(AutomationElement.LocalizedControlTypeProperty);
  cacheRequest.Add(SelectionItemPattern.Pattern);
  cacheRequest.Add(SelectionItemPattern.SelectionContainerProperty);
  cacheRequest.TreeScope = TreeScope.Element;

  AutomationElement tabBarElement;

  using (cacheRequest.Activate())
  {
    var chromeWindow = AutomationElement.FromHandle(hWnd);

    var mainElement = chromeWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));

    if (mainElement == null)
      yield break;

    tabBarElement = mainElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab"));
  }

  if(tabBarElement == null)
    yield break;

  var tabElements = tabBarElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab item"));

  for (var tabIndex = 0; tabIndex < tabElements.Count; tabIndex++)
  {
    yield return "Tab: " + tabElements[tabIndex].Current.Name + ", Index: " + tabIndex + 1;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    相关资源
    最近更新 更多