四、获得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, 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
测试代码如下:
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 教程 目录