NumberEdit.h
#ifndef __NUMBEREDIT_H__
#define __NUMBEREDIT_H__
class CNumberEdit : public CEdit
{
public:
CNumberEdit();
virtual ~CNumberEdit();
void SetAfterDotLen(UINT iAfterDotLen){m_iAfterDotLen =iAfterDotLen;}
UINT GetAfterDotLen(){return m_iAfterDotLen;}
protected:
BOOL CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags);
//BOOL CheckOnePlus(UINT nChar,UINT nRepCnt,UINT nFlags);
BOOL CheckOneMinus(UINT nChar,UINT nRepCnt,UINT nFlags);
BOOL CheckOneDot(UINT nChar,UINT nRepCnt,UINT nFlags);
int GetCaretXPos();
CString m_str;
UINT m_iAfterDotLen;
protected:
//{{AFX_MSG(CUntcNumberEdit)
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnKillfocus();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif
NumberEdit.cpp
#include "stdafx.h"
#include "NumberEdit.h"
CNumberEdit::CNumberEdit()
{
m_iAfterDotLen = 2;
m_str = _T("0.00");
}
CNumberEdit::~CNumberEdit()
{
}
BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
//{{AFX_MSG_MAP(CNumberEdit)
ON_WM_CHAR()
ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar == 8)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
BOOL bChange = FALSE;
GetWindowText(m_str);
if(CheckNumber(nChar,nRepCnt,nFlags))
{
bChange = TRUE;
}
//else if(CheckOnePlus(nChar,nRepCnt,nFlags))
//{
// ;//do nothing
//}
else if(CheckOneMinus(nChar,nRepCnt,nFlags))
{
bChange = TRUE;
}
else if(CheckOneDot(nChar,nRepCnt,nFlags))
{
bChange = TRUE;
}
if(bChange)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
//SetFormatText();
//m_Point.x += m_Point.y*6;
//SetCaretPos(m_Point);
}
}
void CNumberEdit::OnKillfocus()
{
// TODO: Add your control notification handler code here
GetWindowText(m_str);
int iLoop = m_iAfterDotLen;
if(m_str.IsEmpty())
m_str = "0.";
else if(m_str.GetLength() == 1 && m_str[0] == TCHAR('-'))
m_str = "0.";
else
{
int iDotPos = m_str.Find(TCHAR('.'));
if(iDotPos <0)
m_str+=".";
if(iDotPos >0)
{
int iLen = m_str.GetLength() - 1 - iDotPos;
if(iLen >= (int)m_iAfterDotLen)
return;
else
iLoop = (int)m_iAfterDotLen - iLen;
}
}
for(int i=0;i<iLoop;i++)
m_str+="0";
SetWindowText(m_str);
}
BOOL CNumberEdit::CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags)
{
if(::isdigit(nChar)==0)
return FALSE;
int iDotPos = m_str.Find(TCHAR('.'));
if(iDotPos >= 0)
{
int iLen = m_str.GetLength() - 1 - iDotPos;
if( (GetCaretXPos() >= iDotPos) && (iLen >= (int)m_iAfterDotLen))
{
return FALSE;
}
}
return TRUE;
}
BOOL CNumberEdit::CheckOneMinus(UINT nChar,UINT nRepCnt,UINT nFlags)
{
if(nChar != '-')
return FALSE;
if(GetCaretXPos() != 0)
return FALSE;
if(!m_str.IsEmpty() && m_str.GetAt(0) == TCHAR('-'))
return FALSE;
return TRUE;
}
BOOL CNumberEdit::CheckOneDot(UINT nChar,UINT nRepCnt,UINT nFlags)
{
if(nChar != '.')
return FALSE;
if(m_str.Find(TCHAR('.')) >=0)
return FALSE;
int iPos = GetCaretXPos();
if(iPos == 0)
return FALSE;
else if(iPos==1 && m_str[0] == TCHAR('-'))
return FALSE;
return TRUE;
}
int CNumberEdit::GetCaretXPos()
{
CPoint p = GetCaretPos();
return (p.x - p.y)/6;
}