如何突出显示WebBrowser控件C#中的特定单词

问题描述 投票:9回答:3

我有一个Web浏览器控件,并且我能够由用户获取所选的单词。我将该单词保存在文件中,并使用它保存字节偏移量和长度。

让我们说我的Web浏览器控件中有一些文本,例如“ Hello Hey Hello”,它表示用户选择了上一个问候。

现在,该单词已与我一起保存,包括其长度等其他信息。

[当用户重新加载文件并向我发送该单词及其长度和字节偏移量时,我需要提供突出显示所选单词的功能

有什么办法吗?

我有一个Web浏览器控件,并且我能够由用户获取所选的单词。我将此单词保存在文件中,并使用它保存字节偏移量和长度。可以说我在...

c# winforms webbrowser-control
3个回答
6
投票

如果尚未导入Microsoft.mshtml程序集引用,请添加]​​>

using mshtml;

        if (webBrowser1.Document != null)
        {
            IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
            if (document != null)
            {
                IHTMLBodyElement bodyElement = document.body as IHTMLBodyElement;
                if (bodyElement != null)
                {
                    IHTMLTxtRange trg = bodyElement.createTextRange();


                    if (trg != null)
                    {
                        const String SearchString = "Privacy"; // This is the search string you're looking for.
                        const int wordStartOffset = 421; // 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();
                    }
                }
            }
        }

2
投票

我是新手程序员,是我最好的样品。只是花很多时间。


0
投票

我刚刚使用了上面的示例...,它可以进行1次搜索。

所以我将其更改为可进行多次搜索...它可以工作,但是当我滚动网络浏览器时,它再次研究。

© www.soinside.com 2019 - 2024. All rights reserved.