2012-09-10 78 views
1

我需要實現查找對話框提供的搜索功能,當按下Ctrl + F時彈出。WebBrowser控件中的Bypass Find對話框

我有一個文本框,用戶輸入要搜索的字符串和一個「搜索」按鈕。當單擊按鈕時,需要高亮顯示HTML文檔中的匹配項 - 完全按照「查找」對話框中的實現。

有沒有辦法繞過WebBrowser控件中的查找對話框?是否可以將搜索參數發送到「查找」功能?

在此先感謝您的幫助。

編輯

理想情況下,我將能夠使用由查找對話框中提供的全部功能,包括「只匹配整個世界」,「區分大小寫」和「突出顯示所有比賽」 ...

回答

0

它並不難,做你自己,你要做的就是更換無論是在用下面的搜索文本框,說搜索詞是「你好」,然後你用下面的替換打招呼所有出現:

<font color="yellow">hello</font> 

當然,這個HTML可以用SPAN標籤替換(這是DIV標籤的內聯版本,所以你的線不會使用SPAN中斷,但會使用DIV)。但是,在這兩種情況下,這兩個標籤具有style屬性,在這裏你可以使用CSS來改變其顏色或數不勝數的是CSS兼容的其他屬性,如如下:

<SPAN style="background-color: yellow;">hello</SPAN> 

當然,還有其他數不勝數使用HTML更改顏色的方法,如果需要,可隨時在網上搜索。

現在,您可以使用dotnet中的.Replace()函數來執行此操作(替換搜索到的文本),這非常簡單。因此,您可以使用.DocumentText將整個文檔作爲字符串獲取,並且一旦所有的發生被替換(使用.Replace()),您可以將其設置回.DocumentText(因此,您使用.DocumentText獲取原始文檔字符串,並用替換的字符串設置.DocumentText)。當然,你可能不希望這樣做實際的HTML裏面的物品,這樣你就可以通過網頁上的所有元素通過在像下的所有元素做一個For Each循環只是循環:

For Each someElement as HTMLElement in WebBrowser1.Document.All 

並且每個元素都會有一個.InnerText/.InnerHTML和.OuterText/.OuterHTML,您可以獲取(讀取)和設置(用替換的文本覆蓋)。

當然,爲了您的需要,您可能只想替換並覆蓋.InnerText和/或.OuterText。

如果您需要更多幫助,請告訴我。無論哪種情況,我都想知道它是如何解決你的問題的,或者如果我們有更多的人能夠爲你的問題增加價值。乾杯。

+0

謝謝,幾天後我會回覆你,因爲我目前正在做別的事情。 – Rachel

+0

@Rachel太棒了,我希望它解決了你的問題,如果沒有,我們將一起努力。只是出於好奇,如果通常出現在Internet Explorer中的「查找對話框」實際上出現在WinForm中託管的WebBrowser控件中,而您按下ctrl + f,那麼對於我而言,從您的帖子中不會立即發現它。 ctrl + f對話框是否實際出現在託管在自定義Win32應用程序中的WebBrowser控件中? –

+0

@Rachel你最終試用了嗎? –

0

我對這個有很多困難,但我終於找到了一個解決方案。它有點混亂,但它可以像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功能的基礎知識。希望它有助於所有未來的讀者。