#include "stdafx.h"
#include "NumEdit.h"
#define VK_CTRL_X 0x18
#define VK_CTRL_C 0x03
#define VK_CTRL_V 0x16
// CNumEdit
IMPLEMENT_DYNAMIC(CNumEdit, CEdit)
CNumEdit::CNumEdit()
{
m_strValue = _T("");
m_nMaxValue = 0;
m_nMinValue = 0;
}
CNumEdit::~CNumEdit()
{
}
BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
ON_WM_CHAR()
END_MESSAGE_MAP()
void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int nCurserPos = 0;
if ((nChar>='0'&&nChar<='9')|| VK_BACK==nChar)
{
GetWindowText( m_strValue );
CString str;
int nLen = 0;
int nBegin = 0;
int nEnd = 0;
GetSel( nBegin, nEnd );
nLen = m_strValue.GetLength();
if( VK_BACK == nChar )
{
str = m_strValue;
m_strValue = m_strValue.Left(nBegin -1 );
m_strValue +=str.Right(nLen-nBegin);
nCurserPos= nBegin-1;
}
else
{
if( nBegin == nEnd )
{
if( nBegin == nLen )
{
m_strValue +=(char)nChar;
nCurserPos = nLen+1;
}
else
{
m_strValue.Insert( nBegin, (char)nChar );
nCurserPos= nBegin +1;
}
}
else
{
for( int nIndex = 0; nIndex< nBegin; nIndex++ )
{
str+=m_strValue.GetAt(nIndex);
}
str +=(char)nChar;
for( int nIndex = nEnd; nIndex <nLen; nIndex++ )
{
str+=m_strValue.GetAt(nIndex);
}
m_strValue = str;
nCurserPos= nBegin +1;
}
int nCurLen = 0;
nCurLen = m_strValue.GetLength();
str.Format("%d", m_nMaxValue );
nLen = str.GetLength();
if( nCurLen> nLen )
{
m_strValue = m_strValue.Left( nLen );
}
nCurLen = memcmp( str, m_strValue, nLen );
if( nCurLen< 0 )
{
m_strValue = m_strValue.Left(nLen-1);
}
}
SetWindowText( m_strValue );
SetSel(nCurserPos , nCurserPos );
}
if( nChar== VK_CTRL_C || nChar==VK_CTRL_V || nChar==VK_CTRL_X )
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
void CNumEdit::SetValueRang( unsigned int nMinValue, unsigned int nMaxValue )
{
m_nMaxValue = nMaxValue;
m_nMinValue = nMinValue;
}
//此处为对右键面板和快捷键的剪切复制粘贴的处理,为对话框EDIT控件onupdate消息响应的调用
注意此处函数 SetWindowText( strValue );每次只能调用一次,否则程序会崩溃掉,故下面代码做了相应的处理
void CNumEdit::AnalysValue()
{
BOOL bCutStr = FALSE;
CString strValue;
GetWindowText( strValue );
int nCmpRtn = 0;
char chValue = '\0';
int nIndex = 0;
int nLeng = 0;
nLeng = strValue.GetLength();
CString strMaxValue;
strMaxValue.Format( "%d", m_nMaxValue );
int nMaxLen = 0;
nMaxLen = strMaxValue.GetLength();
if( nMaxLen< nLeng )
{
nLeng = nMaxLen;
strValue = strValue.Left( nLeng );
bCutStr = TRUE;
}
while( nIndex < nLeng )
{
chValue = strValue.GetAt( nIndex++ );
if( chValue>='0'&& chValue <= '9')
{
continue;
}
else
{
break;
}
}
if( nIndex == nLeng )
{
if( nLeng == nMaxLen )
{
nCmpRtn = memcmp( strMaxValue, strValue, nLeng );
if( nCmpRtn< 0 )
{
strValue = strValue.Left( nLeng-1 );
SetWindowText( strValue );
}
else
{
if( bCutStr )
{
SetWindowText( strValue );
nIndex = strValue.GetLength();
SetSel( nIndex, nIndex );
}
}
}
else
{
if( bCutStr )
{
SetWindowText( strValue );
nIndex = strValue.GetLength();
SetSel( nIndex, nIndex );
}
}
}
else
{
nIndex--;
strValue = strValue.Left( nIndex );
if( strValue=="" )
{
strValue = m_strValue;
}
SetWindowText( strValue );
nIndex = strValue.GetLength();
SetSel( nIndex, nIndex );
}
m_strValue = strValue;
}
GetSel( nBegin, nEnd );
为得到当前光标所在文本框的位置,若选中对话框中的某字符串时,nBegin为选中字符串的第一个位置,nEnd为选中字符串的后一个位置,若没有选中字符串时,nBegin=nEnd为光标所在位置
SetSel( nIndex, nIndex );
为设置选中字符串或者为选中光标