VB 编写的OCX 如何取得当前页面的URL,必须兼容所有IE内核的浏览器(如360浏览器)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB 编写的OCX 如何取得当前页面的URL,必须兼容所有IE内核的浏览器(如360浏览器)相关的知识,希望对你有一定的参考价值。

页面加载时,我已通过GetActiveWindow()获取到该窗口的句柄.

在IE浏览器下面:
我通过ShelllWindows的Document.URL已经取得了所有的地址,再通过对象的hwnd与GetActiveWindow()判断是不是当前窗口,并正常可以取得地址.

可是在360浏览器下面,却报错了(方法'~'作用于对象'~'失败)。具体原因就是对象的hwnd这句话不能执行。

请问,基于IE内核的浏览器,该如何正确的获取当前窗口的URL?谢谢
ActID = GetActiveWindow() Dim mShellWindow As New SHDocVw.ShellWindows '循环变量 For i = 0 To mShellWindow.Count - 1 If VBA.TypeName(mShellWindow.Item(i).Document) = "htmlDocument" Then hw = mShellWindow.Item(i).hwnd '在360下面报错 if hw=ActID then MsgBox mShellWindow.Item(i).Document.URL exit for end if End If Next i 报错:方法'~'作用于对象'~'失败 请问大家,如何才能正确的获取IE内核的浏览器的URL? 请不要把网上的一些东西贴出来,因为我已经找遍了网上的东西,并已加也测试,都无法通过。必须得亲自测试后,然后告诉,经我测试成功后,追加100-300分!

*******************************************************************************
已解决,原来如此简单,奶奶的,害得我想了N多办法。一代码就可以搞定了!

此题本人已经解决了,目前只供大家讨论和研究,到时候我会选取一个最佳的答案.
我解决是一句代码就搞定了!

参考技术A 以下代码在360浏览器下调试成功。分两步:一、把下面代码复制到一个模块中:Option ExplicitDeclare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPublic Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As LongPublic Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As LongPublic Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPublic Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As PointAPI) As LongType PointAPI x As Long Y As LongEnd TypePrivate Type UUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As ByteEnd TypePrivate Declare Function GetClassName Lib "user32" _ Alias "GetClassNameA" ( _ ByVal hWnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As LongPrivate Declare Function EnumChildWindows Lib "user32" ( _ ByVal hWndParent As Long, _ ByVal lpEnumFunc As Long, _ lParam As Long) As LongPrivate Declare Function RegisterWindowMessage Lib "user32" _ Alias "RegisterWindowMessageA" ( _ ByVal lpString As String) As LongPrivate Declare Function SendMessageTimeout Lib "user32" _ Alias "SendMessageTimeoutA" ( _ ByVal hWnd As Long, _ ByVal msg As Long, _ ByVal wParam As Long, _ lParam As Any, _ ByVal fuFlags As Long, _ ByVal uTimeout As Long, _ lpdwResult As Long) As LongPrivate Const SMTO_ABORTIFHUNG = &H2Private Declare Function ObjectFromLresult Lib "oleacc" ( _ ByVal lResult As Long, _ riid As UUID, _ ByVal wParam As Long, _ ppvObject As Any) As LongPrivate Declare Function FindWindow Lib "user32" _ Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long'Function IEDOMFromhWnd(ByVal hWnd As Long) As IHTMLDocumentDim IID_IHTMLDocument As UUIDDim hWndChild As LongDim lRes As LongDim lMsg As LongDim hr As Long If hWnd <> 0 Then If Not IsIEServerWindow(hWnd) Then ' Find a child IE server window EnumChildWindows hWnd, AddressOf EnumChildProc, hWnd End If If hWnd <> 0 Then ' Register the message lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT") ' Get the object pointer Call SendMessageTimeout(hWnd, lMsg, 0, 0, _ SMTO_ABORTIFHUNG, 1000, lRes) If lRes Then ' Initialize the interface ID With IID_IHTMLDocument .Data1 = &H626FC520 .Data2 = &HA41E .Data3 = &H11CF .Data4(0) = &HA7 .Data4(1) = &H31 .Data4(2) = &H0 .Data4(3) = &HA0 .Data4(4) = &HC9 .Data4(5) = &H8 .Data4(6) = &H26 .Data4(7) = &H37 End With ' Get the object from lRes hr = ObjectFromLresult(lRes, IID_IHTMLDocument, _ 0, IEDOMFromhWnd) End If End If End IfEnd FunctionPrivate Function IsIEServerWindow(ByVal hWnd As Long) As BooleanDim lRes As LongDim sClassname As String sClassname = String$(100, 0) lRes = GetClassName(hWnd, sClassname, Len(sClassname)) sClassname = Left$(sClassname, lRes) IsIEServerWindow = StrComp(sClassname, _ "Internet Explorer_Server", _ vbTextCompare) = 0End Function'Function EnumChildProc(ByVal hWnd As Long, lParam As Long) As Long If IsIEServerWindow(hWnd) Then lParam = hWnd Else EnumChildProc = 1 End IfEnd Function二、建立一个窗体,加入按钮command1,并复制下面代码:Private Sub Command1_Click()Dim doc, wnd As Longwnd = FindWindow("360se5_Frame", vbNullString)wnd = FindWindowEx(wnd, 0, "SeWnd", vbNullString)wnd = FindWindowEx(wnd, 0, "XWnd", vbNullString)wnd = FindWindowEx(wnd, 0, "Container", vbNullString)wnd = FindWindowEx(wnd, 0, "Shell Embedding", vbNullString)wnd = FindWindowEx(wnd, 0, "Shell DocObject View", vbNullString)wnd = FindWindowEx(wnd, 0, "Internet Explorer_Server", vbNullString)Set doc = IEDOMFromhWnd(wnd)MsgBox "360浏览器当前的网址是:" & doc.URLEnd Sub三、打开360浏览器,随便浏览某个网址。然后运行本程序,点击command1按钮,Ok! 参考技术B 其实不用那么复杂,在ocx加个timer,Private Sub Timer1_Timer()
Dim oShellApp, oShellAppWindows, oWin
Dim URL As String
Set oShellApp = CreateObject("Shell.Application")
Set oShellAppWindows = oShellApp.Windows
For Each oWin In oShellAppWindows
If LCase(TypeName(oWin.Document)) = "htmldocument" Then
URL = oWin.LocationURL

End If
Next
End Sub

以上是关于VB 编写的OCX 如何取得当前页面的URL,必须兼容所有IE内核的浏览器(如360浏览器)的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net怎么获取其他页面的URL

VB编写的获取网卡MAC和IP程序在没安装VB的电脑上运行出现429错误.如何解决

VB6.0 获取WebBrowser当前打开页面的网址

vb.net中如何获得DataGridView单元格内容

Environment.SystemDirectory获得的路径没有反斜杠

C# VB6 OCX 生成的消费事件