获取Internet Explorer选项卡标题

我正在尝试获取所有打开的IE选项卡标题的列表或搜索特定的选项卡标题。

我一直在使用它,但由于某些原因不适用于每个选项卡:

// Get a handle to an application window. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); IntPtr explorerHandle = FindWindow("IEFrame", "Google - Internet Explorer"); // Verify that we found the Window. if (explorerHandle == IntPtr.Zero) { MessageBox.Show("Didn't find an instance of IE"); return; } 

我特别想找标题中有“此页面无法显示”的标签。

有什么建议?

我最后一次需要做这样的事情我使用了这段代码:(它正在工作!)

  [DllImport("user32.Dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static public extern IntPtr GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount); [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam); private static bool EnumWindow(IntPtr handle, IntPtr pointer) { GCHandle gch = GCHandle.FromIntPtr(pointer); List list = gch.Target as List; if (list == null) throw new InvalidCastException("GCHandle Target could not be cast as List"); list.Add(handle); return true; } public static List GetChildWindows(IntPtr parent) { List result = new List(); GCHandle listHandle = GCHandle.Alloc(result); try { Win32Callback childProc = new Win32Callback(EnumWindow); EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle)); } finally { if (listHandle.IsAllocated) listHandle.Free(); } return result; } public static string GetWinClass(IntPtr hwnd) { if (hwnd == IntPtr.Zero) return null; StringBuilder classname = new StringBuilder(100); IntPtr result = GetClassName(hwnd, classname, classname.Capacity); if (result != IntPtr.Zero) return classname.ToString(); return null; } public static IEnumerable EnumAllWindows(IntPtr hwnd, string childClassName) { List children = GetChildWindows(hwnd); if (children == null) yield break; foreach (IntPtr child in children) { if (GetWinClass(child) == childClassName) yield return child; foreach (var childchild in EnumAllWindows(child, childClassName)) yield return childchild; } } 

并使用它:

  IntPtr handle = FindWindow("IEFrame", "Google"); var hwndChilds = EnumAllWindows(handle, "Frame Tab"); 

hwndChilds是所有Frame Tab的IntPtr列表。

编辑 :我完成我的答案,接下来的步骤,以获得标签的标题。

  [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam); [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hWnd); public static string GetWindowTextRaw(IntPtr hwnd) { uint WM_GETTEXT = 0x000D; uint WM_GETTEXTLENGTH = 0x000E; // Allocate correct string length first int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null); StringBuilder sb = new StringBuilder(length + 1); SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb); return sb.ToString(); } 

你可以测试它:

  static void Main(string[] args) { IntPtr handle = FindWindow("IEFrame", "Google"); var hwndChild = EnumAllWindows(handle, "Frame Tab"); foreach (var intPtr in hwndChild) { var ptr = GetParent(intPtr); var text = GetWindowTextRaw(ptr); Console.WriteLine(text); } Console.ReadLine(); } 

结果:

在此处输入图像描述

如果您需要更多解释,请不要犹豫。 您可以在http://www.pinvoke.net/上找到所有pInvoke签名

祝你有美好的一天 !