我必須實現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);
}
我知道這代碼並不是要在一個元素中找到文本,這將找到整個頁面中的文本我只想弄明白它會工作,但它不工作,
請建議一些有用的東西。
我想我有點明白你正在嘗試做的......所以,你是說突出顯示不起作用?如何將「高亮」CSS樣式應用於您當前正在處理的DOM元素? – 2012-02-23 05:41:29
感謝回覆。但我想強調這些詞,現在讓我們假設有一個div,並且有內容「這是一個網頁」,所以現在如果將應用CSS,那麼它可以在div上,而不是文本,所以整個div將突出顯示,但我想突出顯示單詞。 – Abhishek 2012-02-23 05:46:08