如何在其他窗口中的Internet explorer_server中获取所选文本?

时间:2016-09-29 19:49:48

标签: c# vb.net

我正在开发一个应用程序,用于在另一个软件中读取Internet explorer_server控件中的选定文本。我在互联网上做了很多搜索,但无济于事。要更详细地解决问题,请查看以下图片:

这是一个软件的小窗口。我想从这个窗口中读取所选文本(就像我在屏幕截图中突出显示的那样)。使用spy ++我发现文本在internet exprlorer_server。

我尝试使用SendMessageTimeout

获取所选文本
  Dim hWndParent As IntPtr = GetForegroundWindow()
        If hWndParent <> IntPtr.Zero AndAlso hWndParent <> Me.Handle Then
            Dim hWndFocusChild = GethWndWithFocus(hWndParent)
            Dim strClassName As String = GetClassName(hWndFocusChild)
            If strClassName.ToLower.Contains("internet explorer_server") Then
                GetHTMLContent(hWndFocusChild)
            End If                  
        End If

但是SenMessageTimeout总是在lRes中返回零。

我正在尝试使用代码来获取所选文本:

  Public Shared Function GetHTMLContent(htmlWindow As IntPtr) As mshtml.HTMLDocument
    Dim htmlDocument As New mshtml.HTMLDocument()
    Dim thedoc As New mshtml.HTMLDocument()
    Dim htmlDoc As IHTMLDocument = Nothing
    Dim foundWindow As Integer = htmlWindow.ToInt32()
    Dim htmlContent As String = ""
    Dim IID_IHTMLDocument As New UUID()
    Dim lRes As Long
    Dim lMsg As Long = 0
    Dim hr As Integer = 0
    If foundWindow <> 0 Then
        lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
        Dim result As Long = SendMessageTimeout(htmlWindow, lMsg, 0, 0, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, lRes)
        If result <> 0 Then
            If lRes <> 0 Then
                ' Initialize the interface ID 
                IID_IHTMLDocument.Data1 = &H626FC520
                IID_IHTMLDocument.Data2 = &HA41E
                IID_IHTMLDocument.Data3 = &H11CF

                IID_IHTMLDocument.Data4 = New Byte(7) {}
                IID_IHTMLDocument.Data4(0) = &HA7
                IID_IHTMLDocument.Data4(1) = &H31
                IID_IHTMLDocument.Data4(2) = &H0
                IID_IHTMLDocument.Data4(3) = &HA0
                IID_IHTMLDocument.Data4(4) = &HC9
                IID_IHTMLDocument.Data4(5) = &H8
                IID_IHTMLDocument.Data4(6) = &H26
                IID_IHTMLDocument.Data4(7) = &H37
                Try
                    'htmlDoc = (mshtml.IHTMLDocument)ObjectFromLresult(, IID_IHTMLDocument, 0, htmlDoc);
                    hr = ObjectFromLresult(lRes, IID_IHTMLDocument, 0, thedoc)
                Catch e As Exception
                    MessageBox.Show("Did not get IHTMLDocument: " + e.Message)
                End Try
            End If
        End If
    End If
    Return thedoc
End Function

以下是GetHTMLContent函数:

{{1}}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

我解决了我的问题。 我使用SendMessage而不是SendMessageTimeout。 声明SendMessage如下所示:

 private static extern IntPtr SendMessage(int hWnd, UInt32 Msg, int wParam, IntPtr lParam);

并将其称为如下所示:

lResult = SendMessage(hWnd.ToInt32(), lMsg, 0, p);

现在htmlDocument = ObjectFromLresult(lResult, typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;会返回所需的结果。