在CRichEditCtrl中设置字符的长度一般用LimitText,但我用了这个好感觉不太好,它没有区分字符和汉字汉字长度是字符的2倍(SetLimitText是可以的)。虽然MSDN里说的
Specifies the length (in bytes) of the text that the user can enter. If this parameter is 0 (the default value), the text length is set to 64K bytes.
但实际上好像不是这样的,输入的汉字和字符一样多。对此,我不的不自己判断。下面给出代码,可以可以帮到人。如果是我说了,可以区分的,请告诉我,谢谢。
ON_EN_CHANGE(IDC_RICHSEND, OnChangeRicheditSend)
//添加CRichEditCtrl的ENM_CHANGE事件 2010/1/31 ADD
long lMask = m_richSend.GetEventMask();
lMask |= ENM_CHANGE;
lMask &= ~ENM_PROTECTED;
m_richSend.SetEventMask(lMask);
// 2010/1/31 END
消息响应
void CDlgTalkViewer::OnChangeRicheditSend()
{
CString str;
GetDlgItemText(IDC_RICHSEND,str );
//判断输入的长度,超出400截取前400
if ( str.GetLength() > 400 )
{
char ch[401];
ZeroMemory(ch,401);
strncpy(ch, str, 400 ); //截取前400
SetDlgItemText(IDC_RICHSEND, ch );
m_richSend.SetSel(400,400); //光标移到最后
int linecout = m_richSend.GetLineCount(); //活动总行数
//调整显示的位置,保证最后一行在最后一行显示--注意5是我们控件可以显示的行数
m_richSend.LineScroll(linecout-5);
}
}