vb.net 教程 20-3 控制Ie浏览器 5

前端之家收集整理的这篇文章主要介绍了vb.net 教程 20-3 控制Ie浏览器 5前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
四、获得Ie窗口 Internet Explorer_Server 句柄

IEFrame->Frame Tab->TabWindowClass->shell DocObject View->Internet Explorer_Server

上一小节获得了 Frame Tab 的句柄,本节继续获得 Internet Explorer_Server 句柄
需要注意的是,从Frame Tab之后,从TabWindowClass 到 Internet Explorer_Server 获得的都是单个的句柄。

具体代码如下:
    ''' <summary>
    ''' 获得IE的Internet Explorer_Server
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function getIEServer() As ArrayList
        Dim IEServerArray As New ArrayList

        Dim IEServerHwnd As IEWindowHwnd
        '获得所有的FraMetab句柄
        Dim IEFraMetabHwndArray As New ArrayList
        IEFraMetabHwndArray = getIEFraMetab()
        '如果FraMetab数量为0,那么就立即返回空IEServerArray
        If IEFraMetabHwndArray.Count = 0 Then Return IEServerArray

        '循环FraMetab最终获得Internet Explorer_Server 句柄
        For i As Integer = 0 To IEFraMetabHwndArray.Count - 1
            Try
                'TabWindowClass
                Dim TWCHwnd As Integer
                TWCHwnd = FindWindowEx(CType(IEFraMetabHwndArray(i),IEWindowHwnd).FTabhwnd,"TabWindowClass",Nothing)
                If TWCHwnd = 0 Then
                    Continue For
                End If
                'shell DocObject View
                Dim SDVHwnd As Integer
                SDVHwnd = FindWindowEx(TWCHwnd,"shell DocObject View",Nothing)
                If SDVHwnd = 0 Then
                    Continue For
                End If
                'Internet Explorer_Server
                Dim IESHwnd As Integer
                IESHwnd = FindWindowEx(SDVHwnd,"Internet Explorer_Server",Nothing)
                If IESHwnd <> 0 Then
                    '记录IE窗口的Hwnd,一直传递下去
                    IEServerHwnd.IEhwnd = CType(IEFraMetabHwndArray(i),IEWindowHwnd).IEhwnd
                    '记录Internet Explorer_Server窗口的Hwnd
                    IEServerHwnd.Ie_SHwnd = IESHwnd
                    IEServerHwnd.FTabhwnd = CType(IEFraMetabHwndArray(i),IEWindowHwnd).FTabhwnd
                    IEServerArray.Add(IEServerHwnd)
                End If

            Catch ex As Exception
                Continue For
            End Try
        Next

        Return IEServerArray
    End Function

测试代码如下:
    Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()

        Dim listIe As New ArrayList
        listIe = getIEServer()
        If listIe.Count > 0 Then
            For i As Integer = 0 To listIe.Count - 1
                ListBox1.Items.Add(CType(listIe.Item(i),IEWindowHwnd).Ie_SHwnd)
            Next
        End If
    End Sub

运行结果如下:
3

注意:spy++中是用十六进制显示的。


由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

原文链接:https://www.f2er.com/vb/256394.html

猜你在找的VB相关文章