分类:
2008-10-13 16:47:14
通过程序向RichEditView写文本
作者:
void CRichEditUseDoc::OnTextWriteafter() { CString str; int nTextLength; CRichEditView *pView = GetView(); CRichEditCtrl &edit = pView->GetRichEditCtrl(); //要写的内容为ABCDEFG str = "ABCDEFG"; //加入换行 str += "\r\n"; //获得文字的长度 nTextLength = edit.GetWindowTextLength(); //将光标放在文本最末 edit.SetSel(nTextLength, nTextLength); //写入文本 edit.ReplaceSel(str); }二、在光标后插入文本,可以使用CHARRANGE结构
void CRichEditUseDoc::OnTextWriteCur() { CString str; CHARRANGE crPos; CRichEditView *pView = GetView(); CRichEditCtrl &edit = pView->GetRichEditCtrl(); //用CHARRANGE结构体获得选择的文本位置 edit.GetSel(crPos); //要写的内容为12345 str = "12345"; //如果没有选中文本,就直接写在光标后 //如果选中了文本,就替代选中的文本 if (crPos.cpMin != crPos.cpMax) { edit.SetSel(crPos.cpMin, crPos.cpMax); } else { edit.SetSel(crPos.cpMax, crPos.cpMax); } edit.ReplaceSel(str); }三、全选文本
void CRichEditUseDoc::OnTextSelectall() { CRichEditView *pView = GetView(); CRichEditCtrl &edit = pView->GetRichEditCtrl(); edit.SetSel(0, -1); }