效果
关键代码
void CDecimalEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
// // 保证小数点最多只能出现一次
if ((nChar==0x08)||(nChar==0x10))//,Backspace,Delete
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
CString str;
// 获取原来编辑框中的字符串
GetWindowText(str);
int len = str.GetLength();
if (len >= 11)
{
return;
}
if(nChar=='.')
{
//若原来的字符串中已经有一个小数点,则不将小数点输入,保证了最多只能输入一个小数点
if(str.Find('.')!=-1)
{
}
// 否则就输入这个小数点
else
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
return;
}
// 除了小数点,还允许输入数字
if((nChar>='0' && nChar<='9'))
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
// 其它的键,都不响应
else
{
}
GetWindowText(str);
len = str.GetLength();
if (len >= 11)
{
CheckWidth(str);
return;
}
if (atoi(str) > 4000000)
{
AfxMessageBox("The maximum value must not exceed 4000000");
SetWindowText("");
return;
}
CheckWidth(str);
return;
}
工程下载:
http://download.csdn.net/detail/ycs34082419900527/9912505点击打开链接