-
vb.net 教程 20-3 控制Ie浏览器 8
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
七、一个完善的程序
Public Class FormMain
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (
ByVal hwndParent As Integer,
ByVal hwndChildAfter As Integer,
ByVal lpszClass As String,
ByVal lpszWindow As String) As Integer
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (
ByVal lpString As String) As Integer
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (
ByVal hWND As Integer,
ByVal msg As Integer,
ByVal wParam As Integer,
ByRef lParam As Integer,
ByVal fuFlags As Integer,
ByVal uTimeout As Integer,
ByRef lpdwResult As Integer) As Integer
Private Const WM_PAINT = &HF
Private Const WM_SIZE = &H5
Private Const SIZE_RESTORED = 0
Private Const SMTO_ABORTIFHUNG = &H2
Private Const SMTO_NOTIMEOUTIFNOTHUNG = &H8
Private Declare Function ObjectFromLresult Lib "oleacc" (
ByVal lResult As Integer,
ByRef riid As Guid,
ByVal wParam As Integer,
ByRef ppvObject As mshtml.IHTMLDocument2) As Integer
Private Structure IEWindowHwnd
Dim IEhwnd As Integer 'IE窗口句柄
Dim FTabhwnd As Integer 'Frame Tab的窗口句柄
Dim Ie_SHwnd As Integer '对应IE_Server的窗口句柄
End Structure
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
Private Sub cbListIE_Click(sender As Object, e As EventArgs) Handles cbListIE.Click
Dim listIe As New ArrayList
listIe = getIhtmlDoc()
If listIe.Count > 0 Then
For i As Integer = 0 To listIe.Count - 1
Dim subList As New ListViewItem()
subList.Text = i.ToString
Dim iedocInfo As New IeDocStructure
iedocInfo = CType(listIe.Item(i), IeDocStructure)
subList.SubItems.Add(iedocInfo.title)
subList.SubItems.Add(iedocInfo.url)
lvListIE.Items.Add(subList)
Next
End If
End Sub
''' <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
''' <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, 0, "TabWindowClass", Nothing)
If TWCHwnd = 0 Then
Continue For
End If
'shell DocObject View
Dim SDVHwnd As Integer
SDVHwnd = FindWindowEx(TWCHwnd, 0, "shell DocObject View", Nothing)
If SDVHwnd = 0 Then
Continue For
End If
'Internet Explorer_Server
Dim IESHwnd As Integer
IESHwnd = FindWindowEx(SDVHwnd, 0, "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
''' <summary>
''' 获得指定IE窗口中的"Frame Tab",可能存在多个
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function getIEFrameTab() As ArrayList
Dim IEFrameTabHwndArray As New ArrayList
Dim IEframeTabHwnd As IEWindowHwnd
'获得所有的IEFrame句柄
Dim IEHwndArray As New ArrayList
IEHwndArray = findAllIe()
'如果IEFrame数量为0,那么就立即返回空IEFrameTabHwndArray
If IEHwndArray.Count = 0 Then Return IEFrameTabHwndArray
Dim result As Integer
'需要查找类名"FrameTab"
Dim ieClass As String = "Frame Tab"
'循环获得FrameTab Hwnd
For i As Integer = 0 To IEHwndArray.Count - 1
Try
'从IEFrame句柄获得它下面的第一个FrameTab句柄
result = FindWindowEx(CType(IEHwndArray(i), Integer), 0, ieClass, Nothing)
Do While result <> 0
'记录IE窗口的Hwnd,一直传递下去
IEframeTabHwnd.IEhwnd = CType(IEHwndArray(i), Integer)
'记录当前FrameTab窗口的Hwnd,一直传递下去
IEframeTabHwnd.FTabhwnd = result
'用于记录IE_Server的窗口句柄
IEframeTabHwnd.Ie_SHwnd = 0
IEFrameTabHwndArray.Add(IEframeTabHwnd)
'从IEFrame句柄获得它下面的下一个FrameTab句柄,直到返回0
result = FindWindowEx(CType(IEHwndArray(i), Integer), result, ieClass, Nothing)
Loop
Catch ex As Exception
Continue For
End Try
Next
Return IEFrameTabHwndArray
End Function
''' <summary>
''' 获得所有的IE窗口hwnd
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function findAllIe() As ArrayList
Dim IEHwndArray As New ArrayList
Dim result As Integer
'需要查找类名 IEFrame
Dim ieClass As String = "IEFrame"
Try
'获得第一个打开的IE窗口
result = FindWindowEx(0, 0, ieClass, Nothing)
Do While result <> 0
IEHwndArray.Add(result)
'获得下一个IE窗口,直到返回0
result = FindWindowEx(0, result, ieClass, Nothing)
Loop
Catch ex As Exception
Return IEHwndArray
End Try
Return IEHwndArray
End Function
''' <summary>
''' 从Internet Explorer_Server获得IHTMLDocument2对象
''' </summary>
''' <param name="IEShwnd">Internet Explorer_Server 句柄</param>
''' <returns></returns>
''' <remarks></remarks>
'''
Public Function getDocumentfromIES(ByVal IEShwnd As Integer) As mshtml.IHTMLDocument2
Dim WM_Html_GETOBJECT As Integer
WM_Html_GETOBJECT = RegisterWindowMessage("WM_HTML_GETOBJECT")
Dim tempInt As Integer = 0
SendMessageTimeout(IEShwnd, WM_Html_GETOBJECT, 0, 0, SMTO_ABORTIFHUNG, 1000, tempInt)
Dim GUID_IHTMLDocument As New Guid("{626FC520-A41E-11CF-A731-00A0C9082637}")
Dim I_IEdocument As mshtml.IHTMLDocument2
If ObjectFromLresult(tempInt, GUID_IHTMLDocument, 0, I_IEdocument) = 0 Then
Return I_IEdocument
End If
Return Nothing
End Function
End Class
运行结果:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/uruseibest/article/details/77924699
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数