111,120
社区成员
发帖
与我相关
我的任务
分享
Point pt;//X是文本起始位置,Y是文本长度。
IHTMLTxtRange textRange;
...
mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)Webbrowser.Document.DomDocument;
IHTMLBodyElement body = (IHTMLBodyElement)document.body;
textRange = (IHTMLTxtRange)body.createTextRange();
textRange.moveStart("character", -document.body.innerText.Length);
textRange.moveEnd("character", document.body.innerText.Length);
textRange.moveStart("character", pt.X);
textRange.moveEnd("character", -(document.body.innerText.Length - (pt.X + pt.Y)));
MessageBox.Show(document.body.innerText.Substring(pt.X, pt.Y));
textRange.select();
if (textRange != null)
{
if (pt.Y > 0)
{
textRange = (IHTMLTxtRange)document.selection.createRange();
document = (mshtml.IHTMLDocument2)Obj.Document.DomDocument;
//textRange.moveStart("character", -document.body.innerText.Length);
//textRange.moveEnd("character", document.body.innerText.Length);
string prev = document.body.innerText.Substring(0, pt.X);
string back = document.body.innerText.Substring(pt.X + pt.Y);
//经测试发现,moveStart及moveEnd方法没有将换行符计算在内。
Regex reg = new Regex("\\r\\n");
MatchCollection mc = reg.Matches(prev);
MatchCollection mc2 = reg.Matches(back);
int n1 = mc.Count;
int n2 = mc2.Count;
textRange.moveStart("character", pt.X - n1);
textRange.moveEnd("character", -(document.body.innerText.Length - (pt.X + pt.Y)-n2));
}
textRange.select();
}