123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "stdafx.h"
- #include "NumEdit.h"
- IMPLEMENT_DYNAMIC(CNumEdit, CEdit)
- CNumEdit::CNumEdit()
- {
- m_bNegative = FALSE;
- m_bPoint = FALSE;
- m_bZero = FALSE;
- }
- CNumEdit::~CNumEdit()
- {
- }
- BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
- ON_WM_CHAR()
- END_MESSAGE_MAP()
- void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
-
- POINT caret;
- ::GetCaretPos(&caret);
-
- caret.x = LOWORD(CharFromPos(caret));
- CString strEdit;
- GetWindowText(strEdit);
- if ( strEdit.IsEmpty() )
- {
- m_bPoint = FALSE;
- m_bZero = FALSE;
- m_bNegative = FALSE;
- }
- if ( strEdit.Find(_T(".")) == -1 )
- m_bPoint = FALSE;
- if ( strEdit.Find(_T("-")) == -1 )
- m_bNegative = FALSE;
-
- if (nChar == VK_BACK && strEdit.GetLength())
- {
-
- if ( caret.x && strEdit[caret.x-1] == _T('.') )
- m_bPoint = FALSE;
-
- if ( caret.x && strEdit[caret.x-1] == _T('-') )
- m_bNegative = FALSE;
- if ( m_bNegative )
- {
- if ( m_bPoint )
- {
- if ( m_bZero && caret.x == 2 )
- {
- m_bZero = FALSE;
- }
- }
- else
- {
- if ( m_bZero && caret.x == 2 && ( strEdit.GetLength() < 2 || strEdit[2] != _T('0')) )
- {
- m_bZero = FALSE;
- }
- }
- }
- else
- {
- if ( m_bPoint )
- {
- if ( m_bZero && caret.x == 1 )
- {
- m_bZero = FALSE;
- }
- }
- else
- {
- if ( m_bZero && caret.x == 1 && ( strEdit.GetLength() < 1 || strEdit[1] != _T('0')) )
- {
- m_bZero = FALSE;
- }
- }
- }
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- }
-
- if (isdigit(nChar))
- {
-
- if ( m_bNegative && caret.x == 0 )
- return;
- if ( m_bNegative )
- {
-
- if ( caret.x == 0 || nChar == _T('-'))
- return;
- if ( m_bZero )
- {
-
- if ( (caret.x == 1 || caret.x == 2 ) && nChar == _T('0') )
- return;
- }
- else
- {
-
- if (caret.x == 1 && nChar == _T('0'))
- {
- m_bZero = TRUE;
- }
- }
- }
- else
- {
- if ( m_bZero )
- {
-
- if ( (caret.x == 0 || caret.x == 1) && nChar == _T('0'))
- return;
- }
- else
- {
- if (caret.x == 0 && nChar == _T('0'))
- {
- m_bZero = TRUE;
- }
- }
- }
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- }
- else if (nChar == '-' && !m_bNegative)
- {
- if (0 != caret.x)
- return;
- m_bNegative = TRUE;
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- }
- else if (nChar == '.' && !m_bPoint)
- {
- if ( m_bNegative && caret.x == 1)
- {
- return;
- }
- m_bPoint = TRUE;
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- }
- }
- BOOL CNumEdit::PreTranslateMessage(MSG* pMsg)
- {
-
- if ( pMsg->message == WM_KEYDOWN )
- {
- if ( pMsg->wParam == VK_DELETE )
- {
- POINT caret;
- ::GetCaretPos(&caret);
-
- LONG nPos = LOWORD(CharFromPos(caret));
- printf("NumEdit,%d,%d\t 插入符最近的字符位置:%d\n", caret.x, caret.y, nPos);
- CString strEdit;
- GetWindowText(strEdit);
- if (strEdit.GetLength())
- {
-
- if ( strEdit.GetAt(nPos) == _T('.') )
- m_bPoint = FALSE;
- if ( strEdit.GetAt(nPos) == _T('-') )
- m_bNegative = FALSE;
- if ( m_bNegative )
- {
- if ( m_bPoint )
- {
- if ( m_bZero && caret.x == 2 )
- {
- m_bZero = FALSE;
- }
- }
- else
- {
- if ( m_bZero && caret.x == 2 && ( strEdit.GetLength() < 2 || strEdit[2] != _T('0')) )
- {
- m_bZero = FALSE;
- }
- }
- }
- else
- {
- if ( m_bPoint )
- {
- if ( m_bZero && caret.x == 1 )
- {
- m_bZero = FALSE;
- }
- }
- else
- {
- if ( m_bZero && caret.x == 1 && ( strEdit.GetLength() < 1 || strEdit[1] != _T('0')) )
- {
- m_bZero = FALSE;
- }
- }
- }
- }
- }
- }
- return CEdit::PreTranslateMessage(pMsg);
- }
|