📄 numberedit.cpp
字号:
// NumberEdit.cpp: implementation of the CNumberEdit class
/////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NumberEdit.h"
//////////////////////////////////////////////////////////////////////
CNumberEdit::CNumberEdit()
{
m_nNumType = 1;
m_nDecimal = 2;
m_lpText[0] = 0x00;
m_bSection = FALSE;
m_bNegative = FALSE;
}
CNumberEdit::~CNumberEdit()
{
}
BEGIN_MESSAGE_MAP(CNumberEdit, CFlatEdit)
//{{AFX_MSG_MAP(CNumberEdit)
ON_WM_CREATE()
ON_WM_RBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////
// CNumberEdit handler
void CNumberEdit::ClearData()
{
if(m_hWnd == NULL) return;
// 定点小数设置格式
if(m_nNumType == 2)
{
ASSERT(m_nDecimal < 60);
// 小数位给零
memset(m_lpText + 1, 0x30, m_nDecimal);
m_lpText[m_nDecimal + 1] = 0x00;
m_lpText[0] = '.';
// 设置文本内容
SetWindowText(m_lpText);
}
else
{
m_lpText[0] = 0x00;
SetWindowText(m_lpText);
}
}
BOOL CNumberEdit::IsInputContent()
{
if(m_nNumType == 2)
{
char lpMask[64];
// 小数位给零
memset(lpMask + 1, 0x30, m_nDecimal);
lpMask[m_nDecimal + 1] = 0x00;
lpMask[0] = '.';
if(strcmp(lpMask, m_lpText) == 0)
return FALSE;
}
else
{
if(strlen(m_lpText) == 0)
return FALSE;
}
return TRUE;
}
void CNumberEdit::PreSubclassWindow()
{
CFlatEdit::PreSubclassWindow();
// 定点小数格式
ClearData();
}
// 查找字符
static long XSearchChar(LPCTSTR lpSrc, TCHAR nChar)
{
long i = 0;
while(lpSrc[i] != 0x00)
{
if(lpSrc[i ++] == nChar)
return i;
}
return 0;
}
// 插入字符
static void XInsertChar(LPTSTR lpSrc, long nPos, TCHAR nChar)
{
long nLength = lstrlen(lpSrc);
if(nPos > nLength) return;
// 移动位置
while(nPos <= nLength)
{
lpSrc[nLength + 1] = lpSrc[nLength];
nLength --;
}
// 插入字符
lpSrc[nPos] = nChar;
}
// 删除字符
static void XDeleteChar(LPTSTR lpSrc, long nPos)
{
long nLength = lstrlen(lpSrc);
if(nPos >= nLength) return;
// 移动位置
while(nPos < nLength)
{
lpSrc[nPos] = lpSrc[nPos + 1];
nPos ++;
}
}
// 删除子串
static void XDeleteStr(LPTSTR lpSrc, long nBegin, long nEnd)
{
long nLength = lstrlen(lpSrc);
// 删除位置调整
if(nBegin >= nLength) return;
if(nEnd > nLength) nEnd = nLength;
// 开始删除字符
while(nEnd <= nLength)
{
lpSrc[nBegin] = lpSrc[nEnd];
nBegin ++; nEnd ++;
}
}
// 删除子串
static void XCleanStr(LPTSTR lpSrc, long nBegin, long nEnd)
{
long nLength = lstrlen(lpSrc);
// 删除位置调整
if(nBegin >= nLength) return;
if(nEnd > nLength) nEnd = nLength;
// 开始删除字符
while(nBegin < nEnd)
{
lpSrc[nBegin] = 0x30;
nBegin ++;
}
}
void CNumberEdit::OnCharInteger(TCHAR nChar)
{
// 字符串长度
int nTemp = lstrlen(m_lpText);
// 当前位置
int nStart, nClose;
GetSel(nStart, nClose);
// 防止超界
long nMaxSize = m_nMaxSize;
if(nMaxSize < 0 || nMaxSize > 63)
nMaxSize = 63;
if(nTemp >= nMaxSize && nChar > 0x20)
{
if(nStart == nClose) return;
else if(nChar == '-') return;
}
// 负号问题
if(nChar == '-' || nChar == '+')
{
if( m_bNegative )
{
// 负号变成正号
if(m_lpText[0] == '-')
{
XDeleteChar(m_lpText, 0);
SetWindowText(m_lpText);
// 设置新的位置
if(nStart > 0) nStart --;
if(nClose > 0) nClose --;
SetSel(nStart, nClose);
}
else // 正号变成负号
{
XInsertChar(m_lpText, 0, '-');
SetWindowText(m_lpText);
// 设置新的位置
nStart ++; nClose ++;
SetSel(nStart, nClose);
}
}
return;
}
// 根据字符,分类分析
if(nChar == 0x08)
{
// 删除选择部分
if(nClose > nStart)
{
XDeleteStr(m_lpText, nStart, nClose);
nClose = nStart;
}
else
{
if(nStart == 0) return;
// 如果是逗号
nStart --; nClose --;
if(m_lpText[nStart] == ',')
{
SetSel(nStart, nStart);
return;
}
// 回删单个字符
XDeleteChar(m_lpText, nStart);
}
// 设置当前文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
else if(nChar == 0x0e)
{
// 删除选择部分
if(nClose > nStart)
{
XDeleteStr(m_lpText, nStart, nClose);
nClose = nStart;
}
else
{
if(m_lpText[nStart] == 0x00) return;
// 如果是逗号
if(m_lpText[nStart] == ',')
{
nStart ++; nClose ++;
SetSel(nStart, nStart);
return;
}
// 删除单个字符
XDeleteChar(m_lpText, nStart);
}
// 设置当前文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
else if(nChar > 0x2f && nChar < 0x3a)
{
// 删除选择部分
if(nClose > nStart)
{
XDeleteStr(m_lpText, nStart, nClose);
nClose = nStart;
}
// 输入单个数字
if(m_lpText[nStart] != '-')
{
XInsertChar(m_lpText, nStart, nChar);
nStart ++; nClose = nStart;
}
// 设置当前文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
else // ------ 不可输入字符
{
// 非法输入字符
return;
}
// 如果分段显示
if( m_bSection )
{
TCHAR lpTemp[128];
long nByte = 0, nIdx = 127;
long nCount = lstrlen(m_lpText);
lpTemp[127] = 0x00;
if(nStart == nCount)
nClose = nIdx;
// 重新生成Section
while(nCount > 0)
{
nCount --;
if(m_lpText[nCount] != ',')
{
nIdx --; nByte ++;
lpTemp[nIdx] = m_lpText[nCount];
// 留出逗号来
if(nByte == 3)
{
nIdx --; nByte = 0;
lpTemp[nIdx] = ',';
}
}
// 记载nStart的位置
if(nCount == nStart)
{
nClose = nIdx;
if(lpTemp[nIdx] == ',')
nClose ++;
}
}
if(lpTemp[nIdx] == ',')
nIdx ++;
// 检查前面的逗号
if( lpTemp[nIdx] == '-' &&
lpTemp[nIdx + 1] == ',' )
{
nIdx ++;
lpTemp[nIdx] = '-';
}
// 设置当前文本
lstrcpy(m_lpText, lpTemp + nIdx);
SetWindowText(m_lpText);
nStart = nClose - nIdx;
SetSel(nStart, nStart);
}
}
void CNumberEdit::OnCharDecimal(TCHAR nChar)
{
// 字符串长度
int nTemp = lstrlen(m_lpText);
// 当前位置
int nStart, nClose;
GetSel(nStart, nClose);
// 防止超界
long nMaxSize = m_nMaxSize;
if(nMaxSize < 0 || nMaxSize > 63)
nMaxSize = 63;
if(nTemp >= nMaxSize && nChar > 0x20)
{
if(nStart == nClose) return;
else if(nChar == '-') return;
}
// 负号问题
if(nChar == '-' || nChar == '+')
{
if( m_bNegative )
{
// 负号变成正号
if(m_lpText[0] == '-')
{
XDeleteChar(m_lpText, 0);
SetWindowText(m_lpText);
// 设置新的位置
if(nStart > 0) nStart --;
if(nClose > 0) nClose --;
SetSel(nStart, nClose);
}
else // 正号变成负号
{
XInsertChar(m_lpText, 0, '-');
SetWindowText(m_lpText);
// 设置新的位置
nStart ++; nClose ++;
SetSel(nStart, nClose);
}
}
return;
}
// 根据字符,分类分析
if(nChar == 0x08) // 回删
{
// 删除选择部分
if(nClose > nStart)
{
XDeleteStr(m_lpText, nStart, nClose);
nClose = nStart;
}
else
{
if(nStart == 0) return;
// 如果是逗号
nStart --; nClose --;
if(m_lpText[nStart] == ',')
{
SetSel(nStart, nStart);
return;
}
// 回删单个字符
XDeleteChar(m_lpText, nStart);
}
// 设置当前文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
else if(nChar == 0x0e) // 删除
{
// 删除选择部分
if(nClose > nStart)
{
XDeleteStr(m_lpText, nStart, nClose);
nClose = nStart;
}
else
{
if(m_lpText[nStart] == 0x00) return;
// 如果是逗号
if(m_lpText[nStart] == ',')
{
nStart ++; nClose ++;
SetSel(nStart, nStart);
return;
}
// 删除单个字符
XDeleteChar(m_lpText, nStart);
}
// 设置当前文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
else if(nChar == '.') // 小数点
{
// 查找小数点的位置
long nIndex = XSearchChar(m_lpText, '.');
if(nIndex > 0)
{
nStart = nIndex;
SetSel(nStart, nStart);
return;
}
else
{
// 负号前不能输入小数点
if(m_lpText[nStart] == '-') return;
// 插入小数点
XInsertChar(m_lpText, nStart, '.');
nStart ++; SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
}
else if(nChar > 0x2f && nChar < 0x3a)
{
// 删除选择部分
if(nClose > nStart)
{
XDeleteStr(m_lpText, nStart, nClose);
nClose = nStart;
}
// 输入单个数字
if(m_lpText[nStart] != '-')
{
XInsertChar(m_lpText, nStart, nChar);
nStart ++; nClose = nStart;
}
// 设置当前文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
else // ------ 不可输入字符
{
// 非法输入字符
return;
}
// 如果分段显示
if( m_bSection )
{
TCHAR lpTemp[128];
BOOL bBefore = FALSE;
long nByte = 0, nIdx = 127;
long nCount = lstrlen(m_lpText);
lpTemp[127] = 0x00;
if(nStart == nCount)
nClose = nIdx;
// 是否有逗号呢
if(XSearchChar(m_lpText, '.') == 0)
bBefore = TRUE;
// 重新生成Section
while(nCount > 0)
{
nCount --;
if(m_lpText[nCount] != ',')
{
nIdx --; nByte ++;
lpTemp[nIdx] = m_lpText[nCount];
// 遇到逗号
if(m_lpText[nCount] == '.')
{
nByte = 0;
bBefore = TRUE;
}
// 留出逗号来
if(bBefore && nByte == 3)
{
nIdx --; nByte = 0;
lpTemp[nIdx] = ',';
}
}
// 记载nStart的位置
if(nCount == nStart)
{
nClose = nIdx;
if(lpTemp[nIdx] == ',')
nClose ++;
}
}
if(lpTemp[nIdx] == ',')
nIdx ++;
// 检查前面的逗号
if( lpTemp[nIdx] == '-' &&
lpTemp[nIdx + 1] == ',' )
{
nIdx ++;
lpTemp[nIdx] = '-';
}
// 设置当前文本
lstrcpy(m_lpText, lpTemp + nIdx);
SetWindowText(m_lpText);
nStart = nClose - nIdx;
SetSel(nStart, nStart);
}
}
void CNumberEdit::OnCharRadixPoint(TCHAR nChar)
{
// 字符串长度
int nTemp = lstrlen(m_lpText);
// 当前位置
int nStart, nClose;
GetSel(nStart, nClose);
// 防止超界
long nMaxSize = m_nMaxSize;
if(nMaxSize < 0 || nMaxSize > 63)
nMaxSize = 63;
if(nTemp >= nMaxSize && nChar > 0x20)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -