【问题标题】:How Do I programatically select text in a webBrowser Control? c#如何以编程方式选择 webBrowser 控件中的文本? C#
【发布时间】:2011-04-24 08:55:50
【问题描述】:

问题来了: 我想让我的程序的用户能够在 webBrowser 控件中搜索给定的关键字(标准 Ctrl+F)。我在文档中找到关键字并使用 span 和 replace() 函数突出显示所有实例没有问题。我无法获得我想要工作的“查找下一个”功能。当用户单击查找下一个时,我希望文档滚动到下一个实例。如果我能得到一个边界框,我可以使用导航功能。我使用以下代码在富文本框中具有相同的功能

                //Select the found text
                this.richTextBox.Select(matches[currentMatch], text.Length);
                //Scroll to the found text
                this.richTextBox.ScrollToCaret();
                //Focus so the highlighting shows up
                this.richTextBox.Focus();

谁能提供一种方法来让它在 webBrowser 中工作?

【问题讨论】:

    标签: c# select browser highlighting


    【解决方案1】:

    我在具有嵌入式 Web 浏览器控件的 WinForms 应用程序中实现了搜索功能。它有一个用于输入搜索字符串的单独文本框和一个“查找”按钮。如果自上次搜索后搜索字符串发生了变化,则单击按钮表示常规查找,如果没有,则表示“再次查找”。这是按钮处理程序:

    private IHTMLTxtRange m_lastRange;
    private AxWebBrowser m_browser;
    
    private void OnSearch(object sender, EventArgs e) {
    
        if (Body != null) {
    
            IHTMLTxtRange range = Body.createTextRange();
    
            if (! m_fTextIsNew) {
    
                m_lastRange.moveStart("word", 1);
                m_lastRange.setEndPoint("EndToEnd", range);
                range = m_lastRange;
            }
    
            if (range.findText(m_txtSearch.Text, 0, 0)) {
    
                try {
                    range.select();
    
                    m_lastRange = range;
    
                    m_fTextIsNew = false;
                } catch (COMException) {
    
                    // don't know what to do
                }
            }
        }
    }
    
    private DispHTMLDocument Document {
        get {
            try {
                if (m_browser.Document != null) {
                    return (DispHTMLDocument) m_browser.Document;
                }
            } catch (InvalidCastException) {
    
                // nothing to do
            }
    
            return null;
        }
    }
    
    private DispHTMLBody Body {
        get {
            if ( (Document != null) && (Document.body != null) ) {
                return (DispHTMLBody) Document.body;
            } else {
                return null;
            }
        }
    }
    

    m_fTextIsNew 在搜索框的 TextChanged 处理程序中设置为 true。

    希望这会有所帮助。

    编辑:添加正文和文档属性

    【讨论】:

    • 这看起来正是我需要的。还有一个问题。 Visual Studio 没有为我提供 createTextRange() 函数。我想做 IHTMLTxtRange range = this.webBrowser.Document.Body.createTextRange();我是否混淆了您的“身体”属性返回的内容。
    • 我在答案中添加了 Body 和 Document 的代码。
    • 谢谢我找到了另一种让我的代码(createTextRange)工作的方法。您的所有其他代码都非常有帮助。 findText 标志也没有按我预期的方式工作,但这本身就是一个问题。非常感谢!下面的代码希望这对其他人有帮助。 IHTMLDocument2 htmlDoc =(IHTMLDocument2)webBrowser.Document.DomDocument; IHTMLBodyElement Body = htmlDoc.body 作为 IHTMLBodyElement; IHTMLTxtRange 范围 = Body.createTextRange();
    猜你喜欢
    • 2012-03-11
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多