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