新完善的NumberEdit控件,能设定小数位和整数位
来源:互联网 发布:sql数据库存图片 编辑:程序博客网 时间:2024/06/05 09:37
- #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;}
- void SetAfterIntLen(UINT iAfterIntLen){m_iAfterIntLen =iAfterIntLen;}
- UINT GetAfterIntLen(){return m_iAfterIntLen;}
-
- protected:
- BOOL CheckNumber(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;
- UINT m_iAfterIntLen;
- protected:
-
- afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
- afx_msg void OnKillfocus();
-
- DECLARE_MESSAGE_MAP()
- };
- #endif
- #include "stdafx.h"
- #include "NumberEdit.h"
- CNumberEdit::CNumberEdit()
- {
- m_iAfterDotLen = 4;
- m_iAfterIntLen = 4;
- m_str = _T("0.00");
- }
- CNumberEdit::~CNumberEdit()
- {
- }
- BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
- ON_WM_CHAR()
- ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
- END_MESSAGE_MAP()
- void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
-
- if(nChar == 8)
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- return;
- }
- BOOL bChange = FALSE;
- GetWindowText(m_str);
- if(CheckNumber(nChar,nRepCnt,nFlags))
- {
- bChange = TRUE;
- }
-
-
-
-
- else if(CheckOneMinus(nChar,nRepCnt,nFlags))
- {
- bChange = TRUE;
- }
- else if(CheckOneDot(nChar,nRepCnt,nFlags))
- {
- bChange = TRUE;
- }
- if(bChange)
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
-
-
-
- }
- }
- void CNumberEdit::OnKillfocus()
- {
- 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 = "0"+ m_str;
- else if(iDotPos <0)
- m_str+=".";
-
- iDotPos = m_str.Find(TCHAR('.'));
- int iLen = m_str.GetLength() - 1 - iDotPos;
- iLoop = (int)m_iAfterDotLen - iLen;
- }
- if(iLoop >= 0)
- {
- for(int i=0;i<iLoop;i++)
- m_str+="0";
- }
- else
- {
- m_str = m_str.Mid(0,m_str.GetLength()+iLoop);
- }
-
- SetWindowText(m_str);
- }
- BOOL CNumberEdit::CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags)
- {
- if(::isdigit(nChar)==0)
- return FALSE;
- if (m_str =="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;
- }
- CString first = m_str.Left(1);
- if (first=="0")
- {
- if ((GetCaretXPos() <= iDotPos) && (GetCaretXPos() != 0))
- {
- return FALSE;
- }
- }
- int iIntLen = iDotPos;
- if (iIntLen>=(int)m_iAfterIntLen)
- {
- return FALSE;
- }
- }
- else
- {
- int iIntLen = m_str.GetLength();
- if (iIntLen>=(int)m_iAfterIntLen)
- {
- 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;
- if(nChar == '-')
- 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;
- }