gpt4 book ai didi

c# - 使用c#获取chrome浏览器标题

转载 作者:行者123 更新时间:2023-11-30 13:38:24 66 4
gpt4 key购买 nike

假设我打开了多个 chrome 窗口(不是标签页),
如何查看浏览器标题?

我尝试了以下方法:

Process[] p = Process.GetProcessesByName("chrome");

foreach (Process item in p)
{
Console.WriteLine(item.MainWindowTitle);
}

但它只返回最后打开的窗口名称,其他所有都是空白..

最佳答案

我不得不做这样的事情,但它涉及到调用 Windows API 函数,非常繁琐。问题是 Chrome 似乎对多个窗口使用单个进程或其他一些奇怪的东西,这意味着简单的方法对我不起作用。

无论如何,试试这个,看看它是否有效。基本上它使用 Chrome 窗口类名(可能是 Chrome_WidgetWin_0Chrome_WidgetWin_1)来枚举所有具有该类名的窗口,并返回那些非空白的窗口标题.

请注意,出于某种原因,这也总是会返回一个名为 "Chrome App Launcher" 的窗口标题,因此您可能需要将其过滤掉。

注意:您还可以通过使用“MozillaWindowClass”为 Firefox 执行此操作,通过使用“IEFrame”为 IE 执行此操作(尽管其中任何一个都可能随着不同的版本而改变)。

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;

namespace Demo
{
class WindowsByClassFinder
{
public delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lparam);

[SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lparam);

[SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
[DllImport("User32", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetWindowText(IntPtr windowHandle, StringBuilder stringBuilder, int nMaxCount);

[DllImport("user32.dll", EntryPoint = "GetWindowTextLength", SetLastError = true)]
internal static extern int GetWindowTextLength(IntPtr hwnd);


/// <summary>Find the windows matching the specified class name.</summary>

public static IEnumerable<IntPtr> WindowsMatching(string className)
{
return new WindowsByClassFinder(className)._result;
}

private WindowsByClassFinder(string className)
{
_className = className;
EnumWindows(callback, IntPtr.Zero);
}

private bool callback(IntPtr hWnd, IntPtr lparam)
{
if (GetClassName(hWnd, _apiResult, _apiResult.Capacity) != 0)
{
if (string.CompareOrdinal(_apiResult.ToString(), _className) == 0)
{
_result.Add(hWnd);
}
}

return true; // Keep enumerating.
}

public static IEnumerable<string> WindowTitlesForClass(string className)
{
foreach (var windowHandle in WindowsMatchingClassName(className))
{
int length = GetWindowTextLength(windowHandle);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(windowHandle, sb, sb.Capacity);
yield return sb.ToString();
}
}

public static IEnumerable<IntPtr> WindowsMatchingClassName(string className)
{
if (string.IsNullOrWhiteSpace(className))
throw new ArgumentOutOfRangeException("className", className, "className can't be null or blank.");

return WindowsMatching(className);
}

private readonly string _className;
private readonly List<IntPtr> _result = new List<IntPtr>();
private readonly StringBuilder _apiResult = new StringBuilder(1024);
}

class Program
{
void run()
{
ChromeWindowTitles().Print();
}

public IEnumerable<string> ChromeWindowTitles()
{
foreach (var title in WindowsByClassFinder.WindowTitlesForClass("Chrome_WidgetWin_0"))
if (!string.IsNullOrWhiteSpace(title))
yield return title;

foreach (var title in WindowsByClassFinder.WindowTitlesForClass("Chrome_WidgetWin_1"))
if (!string.IsNullOrWhiteSpace(title))
yield return title;
}

static void Main()
{
new Program().run();
}
}

static class DemoUtil
{
public static void Print(this object self)
{
Console.WriteLine(self);
}

public static void Print(this string self)
{
Console.WriteLine(self);
}

public static void Print<T>(this IEnumerable<T> self)
{
foreach (var item in self)
Console.WriteLine(item);
}
}
}

关于c# - 使用c#获取chrome浏览器标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958051/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com