Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WebBrowser控件中绕过“查找”对话框_C#_Search_Find_Webbrowser Control - Fatal编程技术网

C# 在WebBrowser控件中绕过“查找”对话框

C# 在WebBrowser控件中绕过“查找”对话框,c#,search,find,webbrowser-control,C#,Search,Find,Webbrowser Control,我需要实现由按下Ctrl+F时弹出的“查找”对话框提供的搜索功能 我有一个文本框,用户在其中输入要搜索的字符串,还有一个搜索按钮。单击按钮时,需要突出显示HTML文档中的匹配项-与查找对话框中的实现完全相同 有没有办法绕过WebBrowser控件中的“查找”对话框?是否可以将搜索参数发送到“查找”功能 提前谢谢你的帮助 编辑 理想情况下,我可以使用“查找”对话框提供的全部功能,包括仅匹配整个世界、匹配大小写和突出显示所有匹配项…自己做并不难,只需将搜索文本框中的内容替换为以下内容,比如说搜索词是

我需要实现由按下Ctrl+F时弹出的“查找”对话框提供的搜索功能

我有一个文本框,用户在其中输入要搜索的字符串,还有一个搜索按钮。单击按钮时,需要突出显示HTML文档中的匹配项-与查找对话框中的实现完全相同

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

提前谢谢你的帮助

编辑


理想情况下,我可以使用“查找”对话框提供的全部功能,包括仅匹配整个世界、匹配大小写和突出显示所有匹配项…

自己做并不难,只需将搜索文本框中的内容替换为以下内容,比如说搜索词是hello,然后将所有hello事件替换为以下内容:

<font color="yellow">hello</font>
每个元素都有一个.InnerText/.InnerHTML和.OuterText/.OuterHTML,您可以从中读取它们,并设置覆盖替换文本

当然,出于您的需要,您可能只需要替换和覆盖.InnerText和/或.OuterText


如果你需要更多的帮助,请告诉我。不管是哪种情况,我都想知道这对你有什么好处,或者我们中的任何人是否还有什么可以为你的问题增加价值的。干杯。

这一次我遇到了很多困难,但我终于找到了解决办法。这有点凌乱,但它的工作原理与Winforms WebBrowser控件的预期一致。这是在.Net 4.0中导入的Microsoft.mshtml 7.0.3300引用

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

    }

同样,这有点凌乱,可能需要清理一下。这在很大程度上复制了Web浏览器控件中Ctrl+F函数的基本功能。希望它能帮助所有未来的读者。

谢谢,我会在几天后回复您,因为我目前正在做其他事情。@Rachel fantastic,我希望它能解决您的问题,如果不能,我们将一起解决。出于好奇,我在你的帖子中并没有立即发现,当你按下ctrl+f时,通常出现在Internet Explorer中的“查找”对话框是否真的出现在WinForm中托管的WebBrowser控件中?ctrl+f对话框是否真的出现在自定义Win32应用程序中托管的WebBrowser控件中?@Rachel您是否尝试过?抱歉,我没有时间,可能有几个星期没有时间,因为我正忙于其他工作。我最终一定会尝试一下,让你知道它是如何运行的。虽然这可能会起作用,但这并不是我想要的。突出显示html文档中的文本与使用浏览器控件的“查找”功能不同,它包括“匹配大小写”、“仅匹配整个单词”等。我知道这些可以重新实现,但我想知道是否有某种方法可以在不必重新发明轮子的情况下实际使用“查找”功能。
For Each someElement as HTMLElement in WebBrowser1.Document.All
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;
            }

    }