获取Internet Explorer选项卡标题

时间:2016-06-02 14:31:46

标签: c# winforms

我正在尝试获取所有打开的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;
        }

我特别关注标题中“无法显示此页面”的标签。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

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

    [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<IntPtr> list = gch.Target as List<IntPtr>;
        if (list == null)
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
        list.Add(handle);
        return true;
    }
    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        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<IntPtr> EnumAllWindows(IntPtr hwnd, string childClassName)
    {
        List<IntPtr> 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();        
  }

结果:

enter image description here

如果您需要更多解释,请不要犹豫。 您可以在http://www.pinvoke.net/

上找到所有pInvoke签名

祝你有美好的一天!

相关问题