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

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

六、获得Ie窗口的标题和网址

在上一小节,我们创建了从Internet Explorer_Server句柄获得IHTMLDocument2对象的方法:getDocumentfromIES()

vb.net 教程 12-4 msHtml 1,已经学习过获得IHTMLDocument2的用处。

这里以获得当前打开的Ie窗口为例说明如何获得标题和网址:

创建一个自定义结构用来存储相关信息:

    Public Structure IeDocStructure
        Dim IEhwnd As Integer   'IE窗口句柄
        Dim FTabhwnd As Integer 'Frame Tab的窗口句柄
        Dim IE_SHwnd As Integer '对应IE_Server的窗口句柄
        Dim title As String     'Document title
        Dim url As String       '网址
    End Structure

具体实现代码

    ''' <summary>
    ''' 获得所有打开IE的 mshtml.IHTMLDocument2
    ''' </summary>
    ''' <returns>返回所有mshtml.IHTMLDocument2 ArrayList</returns>
    ''' <remarks></remarks>
    Public Function getIhtmlDoc() As ArrayList
        Dim IEDocArray As New ArrayList
        Dim IEDocInfo As IeDocStructure

        '获得IEWindowHwnd结构的ArrayList
        Dim IESArray As New ArrayList
        IESArray = getIEServer()
        If IESArray.Count = 0 Then Return IESArray
        '循环获得返回的IEWindowHwnd结构
        For i As Integer = 0 To IESArray.Count - 1
            Dim IESHwnd As IEWindowHwnd = CType(IESArray(i),IEWindowHwnd)
            '记录IE窗口的Hwnd
            IEDocInfo.IEhwnd = IESHwnd.IEhwnd
            '记录Frame Tab 窗口的Hwnd
            IEDocInfo.FTabhwnd = IESHwnd.FTabhwnd
            '记录Internet Explorer_Server窗口的Hwnd
            IEDocInfo.IE_SHwnd = IESHwnd.Ie_SHwnd

            '获得IHTMLDocument2接口
            Dim IEdoc As mshtml.IHTMLDocument2
            IEdoc = getDocumentfromIES(IESHwnd.Ie_SHwnd)
            If IEdoc Is Nothing Then

            Else
                '当前的Url
                IEDocInfo.url = IEdoc.url
                '当前IE网页文档的标题
                IEDocInfo.title = IEdoc.title
                Select Case IEdoc.url
                    Case "about:blank"  '如果无标题,且网址为about:blank
                        IEDocInfo.title = "about:blank"
                    Case "about:tabs"   '如果无标题,且网址为about:tabs
                        IEDocInfo.title = "about:tabs"
                    Case Else
                        If IEdoc.title = "" Then
                            IEDocInfo.title = IEdoc.url
                        End If
                        IEDocArray.Add(IEDocInfo)
                End Select

            End If
        Next
        '返回IeDocStructure结构的ArrayList
        Return IEDocArray
    End Function

测试用代码

    Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()

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

运行时情况:


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

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

原文链接:/vb/256392.html

猜你在找的VB相关文章