⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numericedit.cpp

📁 Resource editor base speadrum Chinese mobile
💻 CPP
字号:
// NumericEdit.cpp : implementation file
//

#include "stdafx.h"
#include "NumericEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNumericEdit

CNumericEdit::CNumericEdit():m_strOldText((TCHAR)0,0)
{
    m_bReturn = FALSE;
	m_nMode = DEC;
	m_nOldIndex = 0;
}

CNumericEdit::~CNumericEdit()
{
}


BEGIN_MESSAGE_MAP(CNumericEdit, CEdit)
	//{{AFX_MSG_MAP(CNumericEdit)
	ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNumericEdit message handlers

void CNumericEdit::OnUpdate() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CEdit::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
    //m_bReturn is true shows this time
    //we enter OnUpdate because we called
    //SetWindowText in the OnUpdate function
    //last time,so we do not to check the content
    if(m_bReturn)
    {
        m_bReturn = false;
        return;
    }

    //Get the original caret pos
    CPoint pt = GetCaretPos();    
    int index = LOWORD(CharFromPos(pt));

    //Get the edit content after you make a change
    CString strText;
    GetWindowText(strText);

    //to see whether or not is a heximal number
    //if(strText.Left(2).CompareNoCase(_T("0x")))
	if(m_nMode == DEC)
    {
		int l = strText.GetLength();
		if(l>3)
		{
			m_bReturn = TRUE;
			SetWindowText(m_strOldText);	
			keybd_event(VK_HOME,0,0,0);
			keybd_event(VK_HOME,0,KEYEVENTF_KEYUP,0);
			for(int j=0;j<index;j++)
			{
				keybd_event(VK_RIGHT,0,0,0);
				keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
			}
			return;
		}
        //decimal,in this situation only digit
        //is valid
        for(int i=0;i<l;i++)
        {
            if(!isdigit(strText[i]))
            {
                //find invalid char,restore the content
                m_bReturn = true;
                SetWindowText(m_strOldText);
                //because SetWindowText moves the caret
                //to the begining of the text,so we must
                //move it to the right place
                for(int j=0;j<index;j++)
                {
                    keybd_event(VK_RIGHT,0,0,0);
                    keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
                }
                return;
            }
        } 
	}
    else
    {
        //heximal,in this situation digit and char a to
        //f is valid  
		CString strTemp;
		if(strText.Find(_T("0x"))!=0 /*&& !strText.IsEmpty()*/)
		{
			if(m_strOldText.Find(_T("0x"))==0/* && m_strOldText.GetLength()==2*/)
			{
				 m_bReturn = TRUE;
				 SetWindowText(m_strOldText);
				 keybd_event(VK_HOME,0,0,0);
				 keybd_event(VK_HOME,0,KEYEVENTF_KEYUP,0);
				 for(int j=0;j<2;j++)
				 {
					   keybd_event(VK_RIGHT,0,0,0);
					   keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
				 }
				 return;
			}
			strTemp.Format(_T("0x%s"),strText);
			strText = strTemp;
			SetWindowText(strText);
			if(index==0)
			{
				index = strText.GetLength()+2;
			}
			for(int j=0;j<index;j++)
			{
				keybd_event(VK_RIGHT,0,0,0);
				keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
			}
		}
		int l = strText.GetLength();

		if(l>8)
		{
			m_bReturn = TRUE;
			SetWindowText(m_strOldText);
			keybd_event(VK_HOME,0,0,0);
			keybd_event(VK_HOME,0,KEYEVENTF_KEYUP,0);
			for(int j=0;j<index;j++)
			{
				keybd_event(VK_RIGHT,0,0,0);
				keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
			}
			return;
		}

        if(l > 2)
        {            
            for(int i=2;i<strText.GetLength();i++)
            {
                int c = strText[i];
                if(!isdigit(c))
                {
                    c = tolower(c);
                    if(c < 'a' || c > 'f')
                    {
                        //find invalid char,restore the content
                        m_bReturn = true;
                        SetWindowText(m_strOldText);
                        //because SetWindowText moves the caret
                        //to the begining of the text,so we must
                        //move it to the right place
                        for(int j=0;j<index;j++)
                        {
                            keybd_event(VK_RIGHT,0,0,0);
                            keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
                        }                       
                        return;
                    }
                }
            }
        } 

	}
    if(strText.GetLength()>m_strOldText.GetLength() && index==1)
	{
		if(index<strText.GetLength()  )
		{
			index=0;
		}
		for(int j=0;j<index;j++)
 		{
			keybd_event(VK_RIGHT,0,0,0);
			keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
 		}
	}
	m_nOldIndex = index;
    //the input is valid, so we save a copy
    //of the content
    m_strOldText = strText;
}

void CNumericEdit::SetDispMode(int nMode)
{
    if(nMode != HEX && nMode != DEC)
        return;     // Invalidate parameter
    m_nMode = nMode;

}

UINT CNumericEdit::GetDigit()
{
    CString strText;
    GetWindowText(strText);

    UINT lRet = 0;
    if(m_nMode == HEX)
    {
        // Hex mode
        _stscanf(strText,_T("%X"),&lRet);
    }
    else
    {
        // Decimal mode
        _stscanf(strText,_T("%d"),&lRet);
    }

    return lRet;
}
void CNumericEdit::SetDigit(UINT nDigit)
{
	CString strText;
	if(m_nMode == DEC)
	{
		strText.Format(_T("%d"),nDigit);
	}
	else
	{
		strText.Format(_T("0x%.6X"),nDigit);
	}
	SetWindowText(strText);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -