vb.net 教程 20-3 控制Ie浏览器 7 获得Ie窗口的标题和网址
生活随笔
收集整理的这篇文章主要介绍了
vb.net 教程 20-3 控制Ie浏览器 7 获得Ie窗口的标题和网址
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
六、获得Ie窗口的标题和网址
在上一小节,我们创建了从Internet Explorer_Server句柄获得IHTMLDocument2对象的方法:getDocumentfromIES()
在 vb.net 教程 12-4 msHtml 1 ,已经学习过获得IHTMLDocument2的用处。
这里以获得当前打开的Ie窗口为例说明如何获得标题和网址:
创建一个自定义结构用来存储相关信息:
Public Structure IeDocStructureDim IEhwnd As Integer 'IE窗口句柄Dim FTabhwnd As Integer 'Frame Tab的窗口句柄Dim IE_SHwnd As Integer '对应IE_Server的窗口句柄Dim title As String 'Document titleDim url As String '网址End Structure具体实现代码:
''' <summary>''' 获得所有打开IE的 mshtml.IHTMLDocument2''' </summary>''' <returns>返回所有mshtml.IHTMLDocument2 ArrayList</returns>''' <remarks></remarks>Public Function getIhtmlDoc() As ArrayListDim IEDocArray As New ArrayListDim IEDocInfo As IeDocStructure'获得IEWindowHwnd结构的ArrayListDim IESArray As New ArrayListIESArray = getIEServer()If IESArray.Count = 0 Then Return IESArray'循环获得返回的IEWindowHwnd结构For i As Integer = 0 To IESArray.Count - 1Dim IESHwnd As IEWindowHwnd = CType(IESArray(i), IEWindowHwnd)'记录IE窗口的HwndIEDocInfo.IEhwnd = IESHwnd.IEhwnd'记录Frame Tab 窗口的HwndIEDocInfo.FTabhwnd = IESHwnd.FTabhwnd'记录Internet Explorer_Server窗口的HwndIEDocInfo.IE_SHwnd = IESHwnd.Ie_SHwnd'获得IHTMLDocument2接口Dim IEdoc As mshtml.IHTMLDocument2IEdoc = getDocumentfromIES(IESHwnd.Ie_SHwnd)If IEdoc Is Nothing ThenElse'当前的UrlIEDocInfo.url = IEdoc.url'当前IE网页文档的标题IEDocInfo.title = IEdoc.titleSelect Case IEdoc.urlCase "about:blank" '如果无标题,且网址为about:blankIEDocInfo.title = "about:blank"Case "about:tabs" '如果无标题,且网址为about:tabsIEDocInfo.title = "about:tabs"Case ElseIf IEdoc.title = "" ThenIEDocInfo.title = IEdoc.urlEnd IfIEDocArray.Add(IEDocInfo)End SelectEnd IfNext'返回IeDocStructure结构的ArrayListReturn IEDocArrayEnd Function测试用代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickListBox1.Items.Clear()Dim listIe As New ArrayListlistIe = getIhtmlDoc()If listIe.Count > 0 ThenFor i As Integer = 0 To listIe.Count - 1ListBox1.Items.Add(CType(listIe.Item(i), IeDocStructure).title)NextEnd IfEnd Sub运行时情况:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
总结
以上是生活随笔为你收集整理的vb.net 教程 20-3 控制Ie浏览器 7 获得Ie窗口的标题和网址的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: .plan
- 下一篇: 小程序 onReachBottom 上拉