[转] – MSHTML选择文字


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();  
} 

[出处:http://topic.csdn.net/u/20100108/21/f4f55afc-f913-4839-82d4-af34e8cf91a6.html]