2012-02-23 19 views
1

我必須實現TTS功能​​才能讀出在webbrowser控件中打開的網頁,而閱讀文本時也必須突出顯示系統的工作閱讀,但我無法做到這一點。 我在這裏查看帖子,但沒有得到我想要的實際輸出。而且,當我嘗試下面的代碼thie我收到錯誤「System.Runtime.InteropServices.COMException是未處理 消息=從HRESULT異常:0x800A025E」關於trg.select()查找文本並在IHtmlElement中突出顯示與webbrowser控件相同的內容

IHTMLDocument2 currentDoc = (IHTMLDocument2)webBrowser1.Document.DomDocument; 

       foreach (IHTMLElement elem in currentDoc.body.all) 
       { 



          string[] splitSentences = elem.innerText.Split(" ".ToCharArray()); 

          for (int i = 0; i < splitSentences.Length; i++) 
          { 

           // highlight(splitSentences[i]); 

           mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)(webBrowser1.Document.DomDocument); 

           IHTMLBodyElement bodyElement = doc.body as IHTMLBodyElement; 

           IHTMLTxtRange trg = bodyElement.createTextRange(); 


           if (trg.findText(splitSentences[i], 0, 0)) 
           { 
            trg.select(); 
           } 

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


           //mshtml.IHTMLSelectionObject sel = (mshtml.IHTMLSelectionObject)doc.selection; 

           //mshtml.IHTMLTxtRange rng = (mshtml.IHTMLTxtRange)sel.createRange(); 
           //// rng.collapse(false); 
           //if (rng.findText(splitSentences[i], 1000000, 0)) 
           //{ 
           // rng.select(); 
           // sound_object.Speak(splitSentences[i], SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync); 
           //} 
           //sound_object.Speak(splitSentences[i], SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync); 


        } 
        Thread.Sleep(2000); 
       } 

我知道這代碼並不是要在一個元素中找到文本,這將找到整個頁面中的文本我只想弄明白它會工作,但它不工作,

請建議一些有用的東西。

+0

我想我有點明白你正在嘗試做的......所以,你是說突出顯示不起作用?如何將「高亮」CSS樣式應用於您當前正在處理的DOM元素? – 2012-02-23 05:41:29

+0

感謝回覆。但我想強調這些詞,現在讓我們假設有一個div,並且有內容「這是一個網頁」,所以現在如果將應用CSS,那麼它可以在div上,而不是文本,所以整個div將突出顯示,但我想突出顯示單詞。 – Abhishek 2012-02-23 05:46:08

回答

1

您可以使用下面的代碼:

IHTMLTxtRange rng = null; 
    private bool FindString(HtmlElement elem, string str) 
    {   
     bool strFound = false; 
     try 
     { 
      if (rng != null) 
      { 
       rng.collapse(false); 
       strFound = rng.findText(str, 1000000000, 0); 
       if (strFound) 
       { 
        rng.select(); 
        rng.scrollIntoView(true); 
       } 
      } 
      if (rng == null) 
      { 
       IHTMLDocument2 doc = 
         elem.Document.DomDocument as IHTMLDocument2; 

       IHTMLBodyElement body = doc.body as IHTMLBodyElement; 

       rng = body.createTextRange(); 
       rng.moveToElementText(elem.DomElement as IHTMLElement); 
       strFound = rng.findText(str, 1000000000, 0); 
       if (strFound) 
       { 
        rng.select(); 
        rng.scrollIntoView(true); 
       } 

      } 
     } 
     catch 
     { 

     } 
     return strFound; 
    } 
1

此示例代碼可以幫助我認爲 - MSDN Forums: WebBrowser Find Dialog

private string GetSelection() 
    { 
     IHTMLDocument2 doc = (IHTMLDocument2)browserInstance.Document; 
     IHTMLSelectionObject sel = doc.selection; 
     IHTMLTxtRange range = (IHTMLTxtRange)sel.createRange(); 
     return range.text; 
    } 
    private bool FindFirst(string text) 
    { 
     IHTMLDocument2 doc = (IHTMLDocument2)browserInstance.Document; 
     IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection; 
     sel.empty(); // get an empty selection, so we start from the beginning 
     IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange(); 
     if (rng.findText(text, 1000000000, 0)) 
     { 
      rng.select(); 
      return true; 
     } 

     return false; 
    } 
    private bool FindNext(string text) 
    { 
     IHTMLDocument2 doc = (IHTMLDocument2)browserInstance.Document; 
     IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection; 
     IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange(); 
     rng.collapse(false); // collapse the current selection so we start from the end of the previous range 
     if (rng.findText(text, 1000000000, 0)) 
     { 
      rng.select(); 
      return true; 
     } 

     return false; 
    } 
相關問題