Finding the text and highlighting the same with in

2019-06-05 06:22发布

问题:

I have to implement the TTS functonality to read out the web page opened in webbrowser control, while the reading the the text also i have to highlighting the work the system is reading but i am unable to do same. I view post here but not get the actual output as i want. and also when i am trying thie below code i am getting the error "System.Runtime.InteropServices.COMException was unhandled Message=Exception from HRESULT: 0x800A025E" on the trg.select()

IHTMLDocument2 currentDoc = (IHTMLDocument2)webBrowser1.Document.DomDocument;

                foreach (IHTMLElement elem in currentDoc.body.all)
                {



                            string[] splitSentences = elem.innerText.Split(" ".ToCharArray());

                            for (int i = 0; i < splitSentences.Length; i++)
                            {

                                // highlight(splitSentences[i]);

                                mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)(webBrowser1.Document.DomDocument);

                                IHTMLBodyElement bodyElement = doc.body as IHTMLBodyElement;

                                IHTMLTxtRange trg = bodyElement.createTextRange();


                                if (trg.findText(splitSentences[i], 0, 0))
                                {
                                    trg.select();
                                }

                                //if (trg != null)
                                //{
                                //    String SearchString = splitSentences[i];// "Privacy"; // This is the search string you're looking for.
                                //    int wordStartOffset = 0; // This is the starting position in the HTML where the word you're looking for starts at.
                                //    int wordEndOffset = SearchString.Length;
                                //    trg.move("character", wordStartOffset);
                                //    trg.moveEnd("character", wordEndOffset);

                                //    trg.select();
                                //}


                                //mshtml.IHTMLSelectionObject sel = (mshtml.IHTMLSelectionObject)doc.selection;

                                //mshtml.IHTMLTxtRange rng = (mshtml.IHTMLTxtRange)sel.createRange();
                                //// rng.collapse(false);
                                //if (rng.findText(splitSentences[i], 1000000, 0))
                                //{
                                //    rng.select();
                                //    sound_object.Speak(splitSentences[i], SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);
                                //}
                                //sound_object.Speak(splitSentences[i], SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);


                    }
                    Thread.Sleep(2000);
                }

i know this code is not about to find the text with in an element, this will find the text with in whole page i want just figure out hows will it work but it is not working,

Please suggest something usefull.

回答1:

You can use the following code:

    IHTMLTxtRange rng = null;
    private bool FindString(HtmlElement elem, string str)
    {           
        bool strFound = false;
        try
        {
            if (rng != null)
            {
                rng.collapse(false);
                strFound = rng.findText(str, 1000000000, 0);
                if (strFound)
                {
                    rng.select();
                    rng.scrollIntoView(true);
                }
            }
            if (rng == null)
            {
                IHTMLDocument2 doc =
                       elem.Document.DomDocument as IHTMLDocument2;

                IHTMLBodyElement body = doc.body as IHTMLBodyElement;

                rng = body.createTextRange();
                rng.moveToElementText(elem.DomElement as IHTMLElement);
                strFound = rng.findText(str, 1000000000, 0);
                if (strFound)
                {
                    rng.select();
                    rng.scrollIntoView(true);
                }

            }
        }
        catch 
        {

        }
        return strFound;
    }


回答2:

This code sample could help I think - MSDN Forums: WebBrowser Find Dialog

    private string GetSelection()
    {
        IHTMLDocument2 doc = (IHTMLDocument2)browserInstance.Document;
        IHTMLSelectionObject sel = doc.selection;
        IHTMLTxtRange range = (IHTMLTxtRange)sel.createRange();
        return range.text;
    }
    private bool FindFirst(string text)
    {
        IHTMLDocument2 doc = (IHTMLDocument2)browserInstance.Document;
        IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
        sel.empty(); // get an empty selection, so we start from the beginning
        IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
        if (rng.findText(text, 1000000000, 0))
        {
            rng.select();
            return true;
        }

        return false;
    }
    private bool FindNext(string text)
    {
        IHTMLDocument2 doc = (IHTMLDocument2)browserInstance.Document;
        IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
        IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
        rng.collapse(false); // collapse the current selection so we start from the end of the previous range
        if (rng.findText(text, 1000000000, 0))
        {
            rng.select();
            return true;
        }

        return false;
    }