其中wb1是一个webbrowser控件
代码如下,可为什么光标位置无法恢复呢?
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (wb1.Document != null)
{
IHTMLDocument2 doc = wb1.Document.DomDocument as IHTMLDocument2;
if (doc != null)
{
IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
if (range != null) _bookmark = range.getBookmark();
}
}
_text = wb1.DocumentText;
wb1.DocumentText = "";
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(_bookmark))
{
wb1.DocumentText = _text;
if (wb1.Document != null)
{
IHTMLDocument2 doc = wb1.Document.DomDocument as IHTMLDocument2;
if (doc != null)
{
IHTMLBodyElement bodyElement = doc.body as IHTMLBodyElement;
if (bodyElement != null)
{
IHTMLTxtRange range = bodyElement.createTextRange();
range.moveToBookmark(_bookmark);
range.select();
}
}
}
_bookmark = string.Empty;
_text = "";
}
}
参考链接:
设置光标位置的问题:SetDocumentHTML(html) 之后, SetCaretPos(curpos) 为何失效?
http://topic.csdn.net/t/20050729/22/4177529.html
HTML可视化编辑器中IE丢失光标位置的问题。
http://hi.baidu.com/jindw/blog/item/8c3e928ba1f04dd0fc1f10d2.html