也谈根据IE窗口句柄hWnd获得IWebBrowser接口
1 关于如何根据IE窗口句柄hWnd获得IWebBrowser接口,网络上代码很多,大多是使用了MSAA里的一个函数GetObjectFromLResult进行获取,一方面,许多朋友尤其是VB爱好者对MSAA技术并不了解,另一方面,用此方法非常繁琐。我的偶象之一超级绿豆甚至在2005年的时侯使用汇编来获取IWebBrowser,虽然思路和方法都很好,但不容易理解和掌握。其实使用Sehll对象,可以非常方便简单地根据窗口句柄获得IWebBrowser接口,由于Shell是COM对象,不仅VB可以做得,VC一样可以做得,甚至在VBS里也可以。
2
3 具体代码如下:
4
5 Option Explicit
6
7 '* ************************************************************** *
8 '* 程序名称:GetIWebBrowserFromHWND.bas
9 '* 程序功能:根据IE窗口句柄获得IE对象
10 '* 作者:lyserver
11 '* 联系方式:http://blog.csdn.net/lyserver
12 '* ************************************************************** *
13
14 Public Function GetIWebBrowserFromHWND(ByVal hWnd As Long) As Object
15 Dim SHApp As Object, SHWin As Object
16
17 Set SHApp = CreateObject("Shell.Application")
18 For Each SHWin In SHApp.windows
19 If LCase(Right(SHWin.FullName, 12)) = "iexplore.exe" Then
20 If SHWin.hWnd = hWnd Then
21 Set GetIWebBrowserFromHWND = SHWin
22 Exit For
23 End If
24 End If
25 Next
26 Set SHApp = Nothing
27 End Function
28 Option Explicit
29
30 '* ************************************************************** *
31 '* 程序名称:GetIWebBrowserFromHWND.bas
32 '* 程序功能:根据IE窗口句柄获得IE对象
33 '* 作者:lyserver
34 '* 联系方式:http://blog.csdn.net/lyserver
35 '* ************************************************************** *
36
37 Public Function GetIWebBrowserFromHWND(ByVal hWnd As Long) As Object
38 Dim SHApp As Object, SHWin As Object
39
40 Set SHApp = CreateObject("Shell.Application")
41 For Each SHWin In SHApp.windows
42 If LCase(Right(SHWin.FullName, 12)) = "iexplore.exe" Then
43 If SHWin.hWnd = hWnd Then
44 Set GetIWebBrowserFromHWND = SHWin
45 Exit For
46 End If
47 End If
48 Next
49 Set SHApp = Nothing
50 End Function
51
52 怎么样,简单吧,总共还不到10行代码。
53
54 假设现在有一个窗口的句柄为327932,那么就可以使用以下代码获得这个窗口的网页源代码:
55
56 view plaincopy to clipboardprint?
57 Sub main()
58 Debug.Print GetIWebBrowserFromHWND(327932).document.body.innerHTML
59 End Sub
60
2
3 具体代码如下:
4
5 Option Explicit
6
7 '* ************************************************************** *
8 '* 程序名称:GetIWebBrowserFromHWND.bas
9 '* 程序功能:根据IE窗口句柄获得IE对象
10 '* 作者:lyserver
11 '* 联系方式:http://blog.csdn.net/lyserver
12 '* ************************************************************** *
13
14 Public Function GetIWebBrowserFromHWND(ByVal hWnd As Long) As Object
15 Dim SHApp As Object, SHWin As Object
16
17 Set SHApp = CreateObject("Shell.Application")
18 For Each SHWin In SHApp.windows
19 If LCase(Right(SHWin.FullName, 12)) = "iexplore.exe" Then
20 If SHWin.hWnd = hWnd Then
21 Set GetIWebBrowserFromHWND = SHWin
22 Exit For
23 End If
24 End If
25 Next
26 Set SHApp = Nothing
27 End Function
28 Option Explicit
29
30 '* ************************************************************** *
31 '* 程序名称:GetIWebBrowserFromHWND.bas
32 '* 程序功能:根据IE窗口句柄获得IE对象
33 '* 作者:lyserver
34 '* 联系方式:http://blog.csdn.net/lyserver
35 '* ************************************************************** *
36
37 Public Function GetIWebBrowserFromHWND(ByVal hWnd As Long) As Object
38 Dim SHApp As Object, SHWin As Object
39
40 Set SHApp = CreateObject("Shell.Application")
41 For Each SHWin In SHApp.windows
42 If LCase(Right(SHWin.FullName, 12)) = "iexplore.exe" Then
43 If SHWin.hWnd = hWnd Then
44 Set GetIWebBrowserFromHWND = SHWin
45 Exit For
46 End If
47 End If
48 Next
49 Set SHApp = Nothing
50 End Function
51
52 怎么样,简单吧,总共还不到10行代码。
53
54 假设现在有一个窗口的句柄为327932,那么就可以使用以下代码获得这个窗口的网页源代码:
55
56 view plaincopy to clipboardprint?
57 Sub main()
58 Debug.Print GetIWebBrowserFromHWND(327932).document.body.innerHTML
59 End Sub
60