在 webbrowser 控件中的 IHtmlElement 中查找文本并突出显示相同 [英] Finding the text and highlighting the same with in IHtmlElement in webbrowser control
问题描述
我必须实现 TTS 功能来读出在 webbrowser 控件中打开的网页,同时阅读文本也必须突出显示系统正在阅读的工作,但我无法这样做.我在这里查看帖子,但没有按照我的意愿获得实际输出.而且当我尝试下面的代码时,我收到错误System.Runtime.InteropServices.COMException is unhandledMessage=Exception from HRESULT: 0x800A025E" on the trg.select()
I have to implement the TTS functonality to read out the web page opened in webbrowser control, while the reading the the text also i have to highlighting the work the system is reading but i am unable to do same. I view post here but not get the actual output as i want. and also when i am trying thie below code i am getting the error "System.Runtime.InteropServices.COMException was unhandled Message=Exception from HRESULT: 0x800A025E" on the trg.select()
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);
}
我知道这段代码不会在元素中找到文本,这将在整个页面中找到文本我只想弄清楚它是如何工作的,但它不起作用,
i know this code is not about to find the text with in an element, this will find the text with in whole page i want just figure out hows will it work but it is not working,
请推荐一些有用的东西.
Please suggest something usefull.
推荐答案
可以使用以下代码:
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;
}
这篇关于在 webbrowser 控件中的 IHtmlElement 中查找文本并突出显示相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!