在Web浏览器控件的IHtmlElement中查找文本并高亮显示

本文关键字:文本 查找 高亮 显示 IHtmlElement Web 浏览器 控件 | 更新日期: 2023-09-27 18:25:26

我必须实现TTS功能来读取在web浏览器控件中打开的网页,同时阅读文本我还必须突出显示系统正在阅读的工作,但我无法做到这一点。我在这里查看帖子,但没有得到我想要的实际输出。而且当我尝试下面的代码时,我会得到错误"System.Runtime.InteropServices.COMException未处理消息=trg.select()上的HRESULT:0x800A025E出现异常

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

我知道这段代码不会在一个元素中找到带有的文本,这会在整个页面中找到带有文本,我只想弄清楚它是如何工作的,但它不工作,

请提出有用的建议。

在Web浏览器控件的IHtmlElement中查找文本并高亮显示

您可以使用以下代码:

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

这个代码示例可以帮助我-MSDN论坛:WebBrowser查找对话框

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