Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用进程ID获取选项卡标题/文本_C#_Google Chrome - Fatal编程技术网

C# 使用进程ID获取选项卡标题/文本

C# 使用进程ID获取选项卡标题/文本,c#,google-chrome,C#,Google Chrome,我不想使用setforegroughindow(),发送键盘键或类似技术,因为这可能会导致我的软件出现问题(意外行为)。 我曾尝试使用作弊引擎程序查找标题(但没有发现任何有用的内容,因为谷歌Chrome似乎工作“颠倒”)。 因此,我继续前进,使用Process Hacker程序,我意识到有一个父进程(chrome.exe)对当前活动选项卡具有有效的窗口句柄,所有其他chrome进程都是它的子进程,也称为后台进程(具有无效的窗口句柄)。 通过深入浏览chrome.exe(父进程)的窗口,我发现窗

我不想使用
setforegroughindow()
,发送键盘键或类似技术,因为这可能会导致我的软件出现问题(意外行为)。
我曾尝试使用作弊引擎程序查找标题(但没有发现任何有用的内容,因为谷歌Chrome似乎工作“颠倒”)。

因此,我继续前进,使用Process Hacker程序,我意识到有一个父进程(chrome.exe)对当前活动选项卡具有有效的窗口句柄,所有其他chrome进程都是它的子进程,也称为后台进程(具有无效的窗口句柄)。
通过深入浏览chrome.exe(父进程)的窗口,我发现窗口句柄的类名是“chrome\u WidgetWin\u 1”和当前活动选项卡的标题/文本。

谷歌浏览器任务管理器的图片。

我正在寻找C或C或C++中的函数,它将取一个整数(进程ID)并返回一个字符串(tab标题/文本)。
我找到的最好的方法就是使用图书馆。它允许与应用程序交互(主要用于可访问性目的),但您可以将其用于其他目的,如获取Chrome标签

请注意,这仅在Chrome窗口未最小化时才起作用

这个过程并不简单,如果你想知道我是如何在我自己的项目中完成的,尽管这不是你可以复制粘贴的东西,但是你可以在
ChromeTabsFinder
中找到你需要的东西:

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

public IEnumerable gettAbsolfWindow(IntPtr hWnd)
{
var cacheRequest=new cacheRequest();
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(AutomationElement.LocalizedControlTypeProperty);
cacheRequest.Add(SelectionItemPattern.Pattern);
添加(SelectionItemPattern.SelectionContainerProperty);
cacheRequest.TreeScope=TreeScope.Element;
自动元素选项卡;
使用(cacheRequest.Activate())
{
var chromeWindow=AutomationElement.FromHandle(hWnd);
var main element=chromeWindow.FindFirst(TreeScope.Children,新属性条件(AutomationElement.NameProperty,“谷歌浏览器”);
if(main元素==null)
屈服断裂;
tabBarElement=main element.FindFirst(TreeScope.substands,新属性条件(AutomationElement.LocalizedControlTypeProperty,“tab”);
}
if(tabBarElement==null)
屈服断裂;
var tabElements=tabBarElement.FindAll(TreeScope.Children,新属性条件(AutomationElement.LocalizedControlTypeProperty,“选项卡项”);
对于(var-tabIndex=0;tabIndex
我找到的最好的方法是使用库。它允许与应用程序交互(主要用于可访问性目的),但您可以将其用于其他目的,如获取Chrome标签

请注意,这仅在Chrome窗口未最小化时才起作用

这个过程并不简单,如果你想知道我是如何在我自己的项目中完成的,尽管这不是你可以复制粘贴的东西,但是你可以在
ChromeTabsFinder
中找到你需要的东西:

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

public IEnumerable gettAbsolfWindow(IntPtr hWnd)
{
var cacheRequest=new cacheRequest();
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(AutomationElement.LocalizedControlTypeProperty);
cacheRequest.Add(SelectionItemPattern.Pattern);
添加(SelectionItemPattern.SelectionContainerProperty);
cacheRequest.TreeScope=TreeScope.Element;
自动元素选项卡;
使用(cacheRequest.Activate())
{
var chromeWindow=AutomationElement.FromHandle(hWnd);
var main element=chromeWindow.FindFirst(TreeScope.Children,新属性条件(AutomationElement.NameProperty,“谷歌浏览器”);
if(main元素==null)
屈服断裂;
tabBarElement=main element.FindFirst(TreeScope.substands,新属性条件(AutomationElement.LocalizedControlTypeProperty,“tab”);
}
if(tabBarElement==null)
屈服断裂;
var tabElements=tabBarElement.FindAll(TreeScope.Children,新属性条件(AutomationElement.LocalizedControlTypeProperty,“选项卡项”);
对于(var-tabIndex=0;tabIndex
通过获取Chrome标签的文本,您想做什么?另一种需要考虑的方法:给定一个进程,获取DOM对象并检索
title
标签。@JamesThorpe这听起来确实是个好主意。但是我不知道从哪里开始,如何开始……我也不知道——我只是想在你研究的时候强调它是一个潜在的替代品:)关于它有一些有用的信息chrome://memory-internals/ 及chrome://memory-redirect/ 页面(链接只能从Google Chrome web浏览器查看)。但是,没有这样的API(据我所知,截至2015年8月14日),可以直接从第三方应用程序/代码“提取/访问”这些信息。悲哀的:-(获取Chrome tab的文本,你想做什么?另一种需要考虑的方法:给定一个过程,抓住DOM对象并检索
title
标记。@JamesThorpe这听起来确实是个好主意。但我不知道从何处开始以及如何开始……我也不-只是想在你研究时突出显示它作为一个潜在的替代方案ing:)有关于chrome://memory-internals/ 及chrome://memory-redirect/ 页面(链接只能从Google Chrome web浏览器中查看)。但是,没有这样的API(据我所知,截至2015年8月14日),可以直接从第三方应用程序/代码中“提取/访问”这些信息。遗憾的是:-(
static string GetChromeTabTitle(uint processId)
{
    // Assuming I call this function with valid process identifier (PID).
    // What do I do next, here??
}
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;
  }
}