【发布时间】:2016-04-13 18:23:00
【问题描述】:
作为this suggestion,我正在使用以下代码在 webBrowser 中突出显示选定的文本:
using mshtml;
if (webBrowser1.Document != null)
{
IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (document != null)
{
IHTMLSelectionObject currentSelection = document.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
string oldText = range.text.Replace("\n", "</br>");
string newHtmlText = "<span style='background-color: rgb(255, 255, 0);'>" +oldText + "</span>";
range.pasteHTML(newHtmlText);
}
}
}
When a normal text is selected every thing is OK.但是,在选择某些混合文本时,您可以在此图像中看到,它将损坏文档。
有时选定的文本可能包含表格和其他格式的文本。如何在不更改格式的情况下突出显示文档的任何部分?
【问题讨论】:
标签: c# html .net webbrowser-control highlighting