📄 maskedit.cpp
字号:
// MaskEdit.cpp: implementation of the CMaskEdit class
////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MaskEdit.h"
///////////////////////////////////////////////////////////////
CMaskEdit::CMaskEdit()
{
m_lpMask[0] = 0x00;
m_lpText[0] = 0x00;
}
CMaskEdit::~CMaskEdit()
{
}
BEGIN_MESSAGE_MAP(CMaskEdit, CFlatEdit)
//{{AFX_MSG_MAP(CMaskEdit)
ON_WM_CREATE()
ON_WM_CHAR()
ON_WM_KEYDOWN()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////
// CMaskEdit handler
void CMaskEdit::ClearData()
{
if(m_hWnd == NULL) return;
// 设置文本信息
long nLength = lstrlen(m_lpMask);
for(long i = 0; i < nLength; i ++)
{
if( IsMaskChar(i) )
m_lpText[i] = 0x20;
else // Not Mask Char
m_lpText[i] = m_lpMask[i];
}
m_lpText[nLength] = 0x00;
SetWindowText(m_lpText);
}
void CMaskEdit::SetEditText(LPCTSTR lpString)
{
bool bString = true;
long nLength = lstrlen(m_lpMask);
for(long i = 0; i < nLength; i ++)
{
if(bString && lpString[i] == 0x00)
bString = false;
if( IsMaskChar(i) )
{
if( bString )
m_lpText[i] = lpString[i];
else
m_lpText[i] = 0x20;
}
else // Not Mask Char
m_lpText[i] = m_lpMask[i];
}
CEdit::SetWindowText(m_lpText);
}
BOOL CMaskEdit::IsInputContent()
{
// 设置文本信息
if(strcmp(m_lpMask, m_lpText) == 0)
return FALSE;
return TRUE;
}
void CMaskEdit::PreSubclassWindow()
{
CFlatEdit::PreSubclassWindow();
// 显示掩码字符串
ClearData();
}
// 基本操作
BOOL CMaskEdit::IsMaskChar(long nIdx)
{
// 判断字符
TCHAR nChar = m_lpMask[nIdx];
if(nChar == '#' || nChar == '$' || nChar == '?')
return TRUE;
return FALSE;
}
long CMaskEdit::GetNextEntryChar(long nIdx, bool bOrder)
{
long nSave = nIdx;
long nLength = lstrlen(m_lpMask);
if(nIdx > nLength) return nLength;
// 向下搜索位置
if( bOrder )
{
while(nIdx < nLength && !IsMaskChar(nIdx))
nIdx ++;
// 到达尾部了
if(nIdx == nLength)
{
// 向回搜索
nIdx = nSave;
while(nIdx > 0 && !IsMaskChar(nIdx - 1))
nIdx --;
// 一个Mask都没有
if(nIdx == 0 && !IsMaskChar(0))
nIdx = nSave;
}
}
else
{
if(nIdx > 0 && IsMaskChar(nIdx - 1) )
return (nIdx - 1);
// 向前搜索第一个
while(nIdx > 0 && !IsMaskChar(nIdx - 1))
nIdx --;
// 到达头部了
if(nIdx == 0 && !IsMaskChar(0))
{
// 向后搜索
nIdx = nSave;
while(nIdx < nLength && !IsMaskChar(nIdx))
nIdx ++;
// 一个Mask都没有
if(nIdx == 0 && !IsMaskChar(0))
nIdx = nSave;
}
// 一个Mask都没有
if(nIdx == nLength) nIdx = nSave;
}
return nIdx;
}
void CMaskEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// 如果只读的话
if(GetStyle() & ES_READONLY)
return;
// 删除键
if(nChar == VK_DELETE)
{
OnChar(0x0e, 0x01, 0x01);
return;
}
// 记载输入字符
CFlatEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMaskEdit::OnLButtonUp(UINT nFlags, CPoint point)
{
CFlatEdit::OnLButtonUp(nFlags, point);
// 计算光标位置
int nStart, nClose;
GetSel(nStart, nClose);
// 看前面是否有位置
if( IsMaskChar(nClose - 1) )
{
while(nClose > 0)
{
if( IsMaskChar(nClose - 1) &&
m_lpText[nClose - 1] == 0x20 )
nClose --;
else break;
}
// 设置位置
SetSel(nClose, nClose);
}
else if( !IsMaskChar(nClose) )
{
nClose = GetNextEntryChar(nClose, false);
while(nClose > 0)
{
if( IsMaskChar(nClose - 1) &&
m_lpText[nClose - 1] == 0x20 )
nClose --;
else break;
}
// 设置位置
SetSel(nClose, nClose);
}
}
void CMaskEdit::OnRButtonDown(UINT nFlags, CPoint point)
{
/* 防止右键激活粘贴 */
}
void CMaskEdit::DeleteString(long nBegin, long nEnd)
{
for(long i = nBegin; i < nEnd; i ++)
{
if( IsMaskChar(i) )
m_lpText[i] = 0x20;
else
m_lpText[i] = m_lpMask[i];
}
}
BOOL CMaskEdit::ReplaceChar(long nIdx, TCHAR nChar)
{
// 删除字符
if(nChar == 0x00 && IsMaskChar(nIdx))
{
m_lpText[nIdx] = 0x20;
return TRUE;
}
switch( m_lpMask[nIdx] )
{
case '$': // 数字
if(nChar > 0x2f && nChar < 0x3a)
{
m_lpText[nIdx] = nChar;
return TRUE;
}
break;
case '#': // 字母
if( (nChar > 0x40 && nChar < 0x5b) ||
(nChar > 0x60 && nChar < 0x7b) )
{
m_lpText[nIdx] = nChar;
return TRUE;
}
break;
case '?': // 字符
m_lpText[nIdx] = nChar;
return TRUE;
}
return FALSE;
}
void CMaskEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// 如果只读的话
if(GetStyle() & ES_READONLY)
return;
// 当前位置
int nStart, nClose;
GetSel(nStart, nClose);
// 分类处理
if(nChar == 0x08)
{
// 删除选择部分
if(nClose > nStart)
{
DeleteString(nStart, nClose);
nClose = nStart;
}
else
{
if(nStart == 0) return;
ReplaceChar(nStart - 1, 0x00);
nStart = GetNextEntryChar(nStart, false);
}
}
else if(nChar == 0x0e)
{
// 删除选择部分
if(nClose > nStart)
{
DeleteString(nStart, nClose);
nClose = nStart;
}
else
{
if(m_lpText[nStart] == 0x00) return;
// 删除单个字符
ReplaceChar(nStart, 0x00);
nStart = GetNextEntryChar(nStart + 1, true);
}
}
else // 一般的文字
{
// 删除选择部分
if(nClose > nStart)
{
DeleteString(nStart, nClose);
nClose = nStart;
}
// 到达尾部了
if(m_lpText[nStart] == 0x00) return;
// 输入文字
if( ReplaceChar(nStart, nChar) )
{
nStart ++;
nStart = GetNextEntryChar(nStart, true);
}
else if( !IsMaskChar(nStart) )
{
// 移到输入位置
nStart = GetNextEntryChar(nStart, true);
}
}
// 设置新的文本
SetWindowText(m_lpText);
SetSel(nStart, nStart);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -