新完善的NumberEdit控件,能设定小数位和整数位

来源:互联网 发布:sql数据库存图片 编辑:程序博客网 时间:2024/06/05 09:37
  1. //NumberEdit.h
  2. #ifndef __NUMBEREDIT_H__
  3. #define __NUMBEREDIT_H__
  4. class CNumberEdit : public CEdit
  5. {
  6. public:
  7.     CNumberEdit();
  8.     virtual ~CNumberEdit();
  9.     void SetAfterDotLen(UINT iAfterDotLen){m_iAfterDotLen =iAfterDotLen;}
  10.     UINT GetAfterDotLen(){return m_iAfterDotLen;}
  11.     void SetAfterIntLen(UINT iAfterIntLen){m_iAfterIntLen =iAfterIntLen;}
  12.     UINT GetAfterIntLen(){return m_iAfterIntLen;}
  13.     
  14. protected:
  15.     BOOL CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags);
  16.     //BOOL CheckOnePlus(UINT nChar,UINT nRepCnt,UINT nFlags);
  17.     BOOL CheckOneMinus(UINT nChar,UINT nRepCnt,UINT nFlags);
  18.     BOOL CheckOneDot(UINT nChar,UINT nRepCnt,UINT nFlags);         
  19.     int GetCaretXPos();  
  20.     CString m_str;
  21.     UINT m_iAfterDotLen;
  22.     UINT m_iAfterIntLen;
  23. protected:
  24.     //{{AFX_MSG(CUntcNumberEdit)
  25.     afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  26.     afx_msg void OnKillfocus();
  27.     //}}AFX_MSG
  28.     DECLARE_MESSAGE_MAP()
  29. };
  30. #endif
  31. //NumberEdit.cpp
  32. #include "stdafx.h"
  33. #include "NumberEdit.h"
  34. CNumberEdit::CNumberEdit()
  35. {
  36.     m_iAfterDotLen = 4;
  37.     m_iAfterIntLen = 4;
  38.     m_str = _T("0.00");  
  39. }
  40. CNumberEdit::~CNumberEdit()
  41. {
  42. }
  43. BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
  44. //{{AFX_MSG_MAP(CNumberEdit)
  45. ON_WM_CHAR()
  46. ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  50. {
  51.     // TODO: Add your message handler code here and/or call default
  52.     if(nChar == 8)
  53.     {
  54.         CEdit::OnChar(nChar, nRepCnt, nFlags);
  55.         return;
  56.     }
  57.     BOOL bChange = FALSE;
  58.     GetWindowText(m_str);
  59.     if(CheckNumber(nChar,nRepCnt,nFlags))
  60.     {
  61.         bChange = TRUE;
  62.     }
  63.     //else if(CheckOnePlus(nChar,nRepCnt,nFlags))
  64.     //{    
  65.     //     ;//do nothing
  66.     //}
  67.     else if(CheckOneMinus(nChar,nRepCnt,nFlags))
  68.     {
  69.         bChange = TRUE;
  70.     }
  71.     else if(CheckOneDot(nChar,nRepCnt,nFlags))
  72.     {
  73.         bChange = TRUE;
  74.     }
  75.     if(bChange)
  76.     {
  77.         CEdit::OnChar(nChar, nRepCnt, nFlags);
  78.         //SetFormatText();
  79.         //m_Point.x += m_Point.y*6;
  80.         //SetCaretPos(m_Point);
  81.     }
  82. }
  83. void CNumberEdit::OnKillfocus() 
  84.     GetWindowText(m_str);
  85.     int iLoop = m_iAfterDotLen;
  86.     if(m_str.IsEmpty()) 
  87.     {
  88.         m_str = "0.";
  89.     }
  90.     else if(m_str.GetLength() == 1 && m_str[0] == TCHAR('-'))
  91.     {
  92.         m_str = "0.";
  93.     }
  94.     else
  95.     {
  96.         int iDotPos = m_str.Find(TCHAR('.'));
  97.         if(iDotPos == 0)
  98.             m_str = "0"+ m_str;
  99.         else if(iDotPos <0)
  100.             m_str+=".";
  101.         
  102.         iDotPos = m_str.Find(TCHAR('.'));  
  103.         int iLen = m_str.GetLength() - 1 - iDotPos;  
  104.         iLoop = (int)m_iAfterDotLen - iLen;
  105.     }
  106.     if(iLoop >= 0)
  107.     {
  108.         for(int i=0;i<iLoop;i++)
  109.             m_str+="0";
  110.     }
  111.     else 
  112.     {
  113.         m_str = m_str.Mid(0,m_str.GetLength()+iLoop);
  114.     }
  115.     
  116.     SetWindowText(m_str); 
  117. }
  118. BOOL CNumberEdit::CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags)
  119. {
  120.     if(::isdigit(nChar)==0)
  121.         return FALSE; 
  122.     if (m_str =="0")
  123.     {
  124.         return FALSE;
  125.     }
  126.     int iDotPos = m_str.Find(TCHAR('.'));
  127.     if(iDotPos >= 0)
  128.     {
  129.         int iLen = m_str.GetLength() - 1 - iDotPos;
  130.         if( (GetCaretXPos() > iDotPos) && (iLen >= (int)m_iAfterDotLen))
  131.         {
  132.             return FALSE;
  133.         }
  134.         CString first = m_str.Left(1);
  135.         if (first=="0")
  136.         {
  137.             if ((GetCaretXPos() <= iDotPos) && (GetCaretXPos() != 0))
  138.             {
  139.                 return FALSE;
  140.             }
  141.         }
  142.         int iIntLen = iDotPos;
  143.         if (iIntLen>=(int)m_iAfterIntLen)
  144.         {
  145.             return FALSE;
  146.         }
  147.     }
  148.     else
  149.     {
  150.         int iIntLen = m_str.GetLength();
  151.         if (iIntLen>=(int)m_iAfterIntLen)
  152.         {
  153.             return FALSE;
  154.         }
  155.     }
  156.     return TRUE;
  157. }
  158. BOOL CNumberEdit::CheckOneMinus(UINT nChar,UINT nRepCnt,UINT nFlags)
  159. {
  160.     if(nChar != '-')
  161.         return FALSE;
  162.     if(GetCaretXPos() != 0)
  163.         return FALSE;
  164.     if(!m_str.IsEmpty() && m_str.GetAt(0) == TCHAR('-'))                  
  165.         return FALSE; 
  166.     if(nChar == '-')
  167.         return FALSE;
  168.     return TRUE;  
  169. }
  170. BOOL CNumberEdit::CheckOneDot(UINT nChar,UINT nRepCnt,UINT nFlags)
  171. {
  172.     if(nChar != '.')
  173.         return FALSE; 
  174.     if(m_str.Find(TCHAR('.')) >=0)
  175.         return FALSE;       
  176.     int iPos = GetCaretXPos();
  177.     if(iPos == 0)
  178.         return FALSE;
  179.     else if(iPos==1 && m_str[0] == TCHAR('-'))
  180.         return FALSE;
  181.     return TRUE;
  182. }
原创粉丝点击