请问,基于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分!
参考技术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