在webbrowser控件中的IHtmlElement中查找文本并突出显示相同内容

时间:2012-02-23 05:36:32

标签: c# winforms webbrowser-control

我必须实现TTS功能​​来读出在webbrowser控件中打开的网页,而阅读文本也必须突出显示系统正在阅读的工作,但我无法做到这一点。 我在这里查看帖子,但没有得到我想要的实际输出。而当我尝试下面的代码我得到错误“System.Runtime.InteropServices.COMException未处理   消息=来自HRESULT的异常:0x00A025E“在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);
                }

我知道这个代码不是要在一个元素中找到文本,这将在整个页面中找到文本我想要弄清楚它会起作用但它不起作用,

请建议一些有用的东西。

2 个答案:

答案 0 :(得分:1)

此代码示例可以帮助我思考 - 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;
    }

答案 1 :(得分:1)

您可以使用以下代码:

    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;
    }