MFC 编辑框限定字数的大小(支持多节字语言,比如中日韩文)

编辑控件 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();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值