在WebBrowser控件中绕过查找对话框 [英] Bypass Find dialog box in WebBrowser control

查看:279
本文介绍了在WebBrowser控件中绕过查找对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个文本框,用户输入要搜索的字符串和搜索按钮。单击按钮时,HTML文档中的匹配项需要高亮显示 - 完全按照查找对话框中的实现。



有没有办法绕过查找对话框在WebBrowser控件框?是否有可能将搜索参数发送到查找功能?



感谢您的帮助。

<理想情况下,我将能够使用查找对话框提供的全部功能,包括匹配整个世界,编辑,编辑,编辑 ,Match case和Highlight all matches...

解决方案

我终于找到了一个解决方案。它有点混乱,但它为Winforms WebBrowser控件的预期工作。这是在.Net 4.0中,Microsoft.mshtml 7.0.3300引用导入。

 使用mshtml; 

private int _findClicks = 0;
私人字符串_searchText =;

public string SearchText
{
get {return _searchText; }
set
{
if(value.ToUpper()!= _searchText)
{
ClearFind();
_searchText = value.ToUpper();
txtSearch.Text = value.ToUpper();

_findClicks = 0;



$ private void btnSearch_Click(object sender,EventArgs e)
{
SearchText = txtSearch.Text;
if(_findClicks == 0)
FindFirst();
else
FindNext();
}

///< summary>
///搜索所有文本以查找。将所有的事件设置为背景颜色黄色。
///< / summary>
private void FindFirst()
{
if(_searchText ==)
return;
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject sel = doc.selection;
IHTMLTxtRange range = sel.createRange()as IHTMLTxtRange;
//标记所有出现的背景色黄色
while(true)
{
if((range.findText(_searchText))&&& amp;(range.htmlText!= span style ='background-color:yellow;'>+ _searchText +< / span>))
{
range.pasteHTML(< span style ='background-color :yellow;'>+ _searchText +< / span>);
}
else
break;
}
//移至开头并选择第一个出现。
range.moveStart(word,-9999999);
range.findText(_searchText);
range.select();
_findClicks ++;
}

///< summary>
///查找下一个搜索到的文本并选择它。
///< / summary>
private void FindNext()
{
if(_searchText ==)
return;
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject sel = doc.selection;
IHTMLTxtRange range = sel.createRange()as IHTMLTxtRange;
range.collapse(false); / /折叠当前选择,所以我们从前一个范围的结尾开始

if(range.findText(_searchText,1000000,0))
{
range.select );
}
else //如果在列表结尾处,则转到开始处并再搜索一次。
{
range.moveStart(word,-9999999);
if(range.findText(_searchText,1000000,0))
{
range.select();
}
}
}

///< summary>
///删除之前搜索的所有单词的突出显示。
///< / summary>
private void ClearFind()
{
if(_searchText ==|| webBrowser1.ReadyState!= WebBrowserReadyState.Complete)
return;
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject sel = doc.selection;
IHTMLTxtRange range = sel.createRange()as IHTMLTxtRange;
range.moveStart(word,-9999999); ($)
while(true)
{
if((range.findText(_searchText))&&(!range.htmlText.Contains(span style ='background-color:white )))
{
range.pasteHTML(< span style ='background-color:white;'>+ _searchText +< / span>);
}
else
break;


$ b code $
$ b $ p
$ b

大概可以清理一下。这几乎复制了Web浏览器控件中Ctrl + F功能的基础知识。希望对所有未来的读者都有所帮助。

I need to implement the Search functionality provided by the Find dialog box which pops up when Ctrl+F is pressed.

I have a textbox where the user enters the string to be searched for, and a "Search" button. When the button is clicked clicked, matches in the HTML document need to be highlighted - exactly as implemented in the Find dialog box.

Is there a way to bypass the Find dialog box in a WebBrowser control? Is it possible to send the search parameters to the "find" functionality?

Thanks in advance for the help.

EDIT

Ideally, I would be able to use the full functionality provided by the Find dialog box, including "Match whole world only", "Match case" and "Highlight all matches"...

解决方案

I had a lot of difficulty with this one, but I finally got a solution working. It's somewhat messy but it works as intended for the Winforms WebBrowser control. This is in .Net 4.0 with the Microsoft.mshtml 7.0.3300 reference imported.

using mshtml;

private int _findClicks = 0;
private string _searchText = "";

public string SearchText
    {
        get { return _searchText; }
        set
        {
            if (value.ToUpper() != _searchText)
            {
                ClearFind();
                _searchText = value.ToUpper();
                txtSearch.Text = value.ToUpper();

                _findClicks = 0;
            }
        }
    }

private void btnSearch_Click(object sender, EventArgs e)
    {
        SearchText = txtSearch.Text;
        if (_findClicks == 0)
            FindFirst();
        else
            FindNext();
    }

 /// <summary>
    /// Search through all text to find. Sets all occurrences to background color yellow.
    /// </summary>
    private void FindFirst()
    {
            if (_searchText == "")
                return;
            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject sel = doc.selection;
            IHTMLTxtRange range = sel.createRange() as IHTMLTxtRange;
            //Mark all occurrences with background color yellow
            while (true)
            {
                if ((range.findText(_searchText)) && (range.htmlText != "span style='background-color: yellow;'>" + _searchText + "</span>"))
                {
                    range.pasteHTML("<span style='background-color: yellow;'>" + _searchText + "</span>");
                }
                else
                    break;
            }
            //Move to beginning and select first occurence.
            range.moveStart("word", -9999999);
            range.findText(_searchText);
            range.select();
            _findClicks++;
        }

/// <summary>
    /// Finds next occurrence of searched text and selects it.
    /// </summary>
    private void FindNext()
    {
            if (_searchText == "")
                return;
            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject sel = doc.selection;
            IHTMLTxtRange range = sel.createRange() as IHTMLTxtRange;
            range.collapse(false); // collapse the current selection so we start from the end of the previous range

            if (range.findText(_searchText, 1000000, 0))
            {
                range.select();
            }
            else // If at end of list, go to beginning and search one more time.
            {
                range.moveStart("word", -9999999);
                if (range.findText(_searchText, 1000000, 0))
                {
                    range.select();
                }
            }
    }

/// <summary>
    /// Remove highlighting on all words from previous search.
    /// </summary>
    private void ClearFind()
    {
            if (_searchText == "" || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                return;
            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject sel = doc.selection;
            IHTMLTxtRange range = sel.createRange() as IHTMLTxtRange;
            range.moveStart("word", -9999999);
            while (true)
            {
                if ((range.findText(_searchText)) && (!range.htmlText.Contains("span style='background-color: white")))
                {
                    range.pasteHTML("<span style='background-color: white;'>" + _searchText + "</span>");
                }
                else
                    break;
            }

    }

Again, this is somewhat messy and could probably be cleaned up quite a bit. This pretty much replicates the basics of the Ctrl+F function in the Web Browser control. Hope it helps to all future readers.

这篇关于在WebBrowser控件中绕过查找对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆