最近苦逼项目需要用mfc做一个显示界面,其中有一个功能就是需要在editbox末尾追加一个cstring
http://blog.sina.com.cn/s/blog_7176c8440100mbzt.html 中给出的做法如下:
int nCount = 0;
int nLastLine = 0;
int nLastLine = 0;
nCount = pEditHandle->GetLineCount(); //获取行数,包括回车行
nLastLine = pEditHandle->LineIndex( nCount - 1 ); //获取字符数,许可多行
pEditHandle->SetSel( nLastLine + 1, nLastLine + 2 ); //设定光标选中的区域
pEditHandle->ReplaceSel( "你要输出的信息" ); //文字替换
nLastLine = pEditHandle->LineIndex( nCount - 1 ); //获取字符数,许可多行
pEditHandle->SetSel( nLastLine + 1, nLastLine + 2 ); //设定光标选中的区域
pEditHandle->ReplaceSel( "你要输出的信息" ); //文字替换
但是如果最后一行有文字的话就会出问题,参考了msdn中关于lineindex的用法,多加了两行代码解决了这个问题:
http://msdn.microsoft.com/zh-cn/magazine/90dfhy8f(VS.80).aspx
int nCount = 0; int nLastLineStart = 0; int nLastLineEnd = 0; nCount = pEditHandle->GetLineCount(); //获取行数,包括回车行 nLastLineStart = pEditHandle->LineIndex( nCount - 1 ); //获取字符数,许可多行 nLastLineEnd = nLastLineStart + pEditHandle->LineLength(nLastLineStart); pEditHandle->SetSel( nLastLineEnd + 1, nLastLineEnd + 2 ); //设定光标选中的区域 pEditHandle->ReplaceSel( "你要输出的信息" ); //文字替换=