编辑控件 id 为 ID_NAME_EDIT,对其关联一变量为 m_strName。 限定最大字数值为 32,
DDX_Text(pDX, ID_NAME_EDIT, m_strName);
DDV_MaxChars(pDX, m_strName, 32);
代码段如下:
#define BOOK_MAX_NAME_SIZE 32
void CMfc_DlgDlg::OnEnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
//#if defined(CM215F) || defined(CM215FW) || defined(FX_M218FW) || defined(FX_M215FW)
// UTF-8
CEdit *pEdit=(CEdit*)GetDlgItem(ID_NAME_EDIT);
CString strName;
pEdit->GetWindowText(strName);
int nStartChar,nEndChar;
pEdit->GetSel(nStartChar,nEndChar);
int length = strName.GetLength();
DWORD dwNum=0,dwLineLength=0;
char buffer[1024]={0x00};
dwNum = WideCharToMultiByte(CP_UTF8,NULL,strName,strName.GetLength(),NULL,0,NULL,FALSE);
if (dwNum > BOOK_MAX_NAME_SIZE)
{
if (nStartChar >= length)
{
do
{
dwNum = WideCharToMultiByte(CP_UTF8,NULL,strName,-1,NULL,0,NULL,FALSE)-1;
dwLineLength=pEdit->LineLength();
if(dwNum>BOOK_MAX_NAME_SIZE)
{
strName=strName.Left(dwLineLength-1);
m_strName=strName;
UpdateData(FALSE);
pEdit->SetSel(nStartChar,nEndChar);
}
}while(dwNum>BOOK_MAX_NAME_SIZE);
}
else
{
nStartChar--;
UpdateData(FALSE);
pEdit->SetSel(nStartChar,nStartChar);
}
}
UpdateData();
}