Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

c# - How to get selected text in internet explorer_server in other window?

I am developing an application that reads selected text in internet explorer_server control in another software. I did lots of search on internet but to no avail. To get the problem in more detail please have a look at the following picture:

This is a small window of a software. I want to read the selected text (like i highlighted in the screen shot) from this window. Using spy++ i found that the text is in internet exprlorer_server.

I tried to get selected text using SendMessageTimeout

        lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
        Dim result As Long = SendMessageTimeout(htmlWindow, lMsg, 0, 0, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, lRes)

But SenMessageTimeout always returns zero in lRes.

I am trying following code to get the selected text:

  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

Following is GetHTMLContent function:

  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

Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i solved my problem. i used SendMessage instead of SendMessageTimeout. Declared SendMessage as shown below:

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

and called it as shown below:

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

Now htmlDocument = ObjectFromLresult(lResult, typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2; returns desired result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...