void CNewEdit::OnChar(UINT nChar, UINTnRepCnt, UINT nFlags) 如下;
其它需要可以灵活定制。
void CNewEdit::OnChar(UINT nChar, UINTnRepCnt, UINT nFlags)
{
if(nChar==VK_BACK)//BackSpace
CEdit::OnChar(nChar, nRepCnt,nFlags);
//
//Some invariable parameter for further use
CPoint caretPt;
caretPt=GetCaretPos ();
caretPt.x = LOWORD (CharFromPos (caretPt));//Thefunction is the core,I'm blind to seek it for 3 daysCString str;
int nLength;
int nDot;
int neE;
double fabstr;
GetWindowText (str);
fabstr=fabs(atof(str));
nLength=str.GetLength();
nDot=str.Find(_T('.'));
neE=max(str.Find('e'),str.Find('E'));
//
//Input 0-9,especialy 0 needs moreattention.if (isdigit(nChar))
{
if(nChar>'0')
CEdit::OnChar(nChar, nRepCnt,nFlags); else//make use of '0'
{
if(str.IsEmpty())
{
CEdit::OnChar(nChar,nRepCnt, nFlags);
return;
}
if(str[0]=='0')
{
if(neE==-1)
{
if(caretPt.x>1)
{
CEdit::OnChar(nChar,nRepCnt, nFlags);
return;
}
else
return;
}
else
{
if((caretPt.x-1)!=neE&& caretPt.x>1)
{
CEdit::OnChar(nChar,nRepCnt, nFlags);
return;
}
else
return;
}
}
if(str[0]=='-')
{
if(str[1]=='0')
{
if(neE==-1)
{
if(caretPt.x>2)
{
CEdit::OnChar(nChar,nRepCnt, nFlags);
return;
}
else
return;
}
else
{
if((caretPt.x-1)!=neE&& caretPt.x>2)
{
CEdit::OnChar(nChar,nRepCnt, nFlags);
return;
}
else
return;
}
}
}
if(str[0]>='1'&& caretPt.x>0)
{
CEdit::OnChar(nChar,nRepCnt, nFlags); return;
}
}
}
else
//
//Determine the '-'
if (nChar == '-')
{
if(!caretPt.x)
{
if(((nLength > 0) && (str[0]!='-')) || (nLength==0))
CEdit::OnChar(nChar,nRepCnt, nFlags);
}
else
{
if ((str[caretPt.x-1] == 'e') || (str [caretPt.x-1] == 'E'))
CEdit::OnChar(nChar,nRepCnt, nFlags);
}
}else
//Input'e' or 'E'
if ((nChar == 'e') || (nChar =='E'))
{
if((caretPt.x == 1) && (str[0] == '-'))
return; if(caretPt.x)
{
if(neE!=-1)
return;
if(nDot!=-1&& caretPt.x<nDot)
return; CEdit::OnChar(nChar,nRepCnt, nFlags);
}
}else
//
//Input '.'
if (nChar == '.')
{
if(nDot!=-1)
return;
if(neE!=-1&& caretPt.x>neE)
return;
CEdit::OnChar(nChar,nRepCnt, nFlags);
}
此外如果要截取输入字符串末尾的‘0';可以利用消息WM_KILLFOCUS;
函数代码如下:
void CNewEdit::OnKillfocus()
{
// TODO: Add your control notification handlercode here
CString str,tr;
GetWindowText(str);
int nLength;
intnDot=-1;
int neE;
//double fstr;
nLength=str.GetLength();
for(int i=0;i<nLength;i++)
{
if(str[i]==_T('.'))
nDot=i;
}
neE=max(str.Find(_T('e')),str.Find(_T('E')));
if(nDot!=-1 && neE==-1)
str.TrimRight(_T("0"));
{
int t= str.Find('.');
if(t==str.GetLength()-1)
str+='0';
} SetWindowText(str);
}