从Internet Explorer_Server对象获取SelStart和SelLength的等效项

时间:2014-08-12 15:56:58

标签: winapi vb6 webbrowser-control

有没有办法从Internet Explorer_Server对象中获取VB的SelStart和SelLength属性?

我尝试使用SendMessage EM_GETSEL,但它没有返回任何内容。

2 个答案:

答案 0 :(得分:0)

您必须查询InternetExplorer_Server的HWND以查找其IHTMLDocument2界面,然后您可以使用浏览器的DOM界面根据需要操作浏览器内容:< / p>

How to get IHTMLDocument2 from a HWND

IHTMLDocument2::selection Property

答案 1 :(得分:0)

使用此代码来计算SelStart和SelLength。将InternetExplorer_Server.hWnd替换为对象的句柄。

Dim DomObj As IHTMLDocument2
Dim SelObj As IHTMLTxtRange

Set DomObj = IEDOMFromhWnd(InternetExplorer_Server.hWnd)
Set SelObj = DomObj.selection.createRange
TextToCheck$ = DomObj.body.innerText
' Calculate SelLength...
SelLength = Len(SelObj.Text)
SelObj.moveStart "character", -Len(TextToCheck$)
' Calculate SelStart...
SelStart = Len(SelObj.Text)

模块中需要以下代码:

Private Type UUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(0 To 7) As Byte
End Type

Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private 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 Long
Private Declare Function ObjectFromLresult Lib "oleacc" (ByVal lResult As Long, riid As UUID, ByVal wParam As Long, ppvObject As Any) As Long


Public Function IEDOMFromhWnd(ByVal hwnd As Long) As IHTMLDocument2

Dim IID_IHTMLDocument2 As UUID
Dim hWndChild As Long
Dim lRes As Long
Dim lMsg As Long
Dim hr As Long

' 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_IHTMLDocument2
   .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_IHTMLDocument2, 0, IEDOMFromhWnd)

End If

End Function