📄 maskedit.cpp
字号:
// MaskEdit.cpp : implementation file
//
#include "stdafx.h"
#include "MaskEdit.h"
IMPLEMENT_DYNAMIC(CMaskEdit, CEdit)
BEGIN_MESSAGE_MAP(CMaskEdit, CEdit)
//{{AFX_MSG_MAP(CMaskEdit)
ON_WM_CHAR()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CMaskEdit::CMaskEdit()
{
m_bUseMask = FALSE;
m_strMask = _T("");
m_strLiteral = _T("");
m_strValid = _T("");
m_strHours = _T("47");
m_strMins = _T("59");
m_bMaskKeyInProgress = FALSE;
m_strMaskLiteral = _T("");
}
void CMaskEdit::SetMask(LPCSTR lpMask, LPCSTR lpLiteral, LPCSTR lpValid)
{
m_bUseMask = FALSE;
if (lpMask == NULL) return;
m_strMask = lpMask;
if (m_strMask.IsEmpty()) return;
if (lpLiteral != NULL)
{
m_strLiteral = lpLiteral;
if (m_strLiteral.GetLength() != m_strMask.GetLength())
m_strLiteral.Empty();
}
else
m_strLiteral.Empty();
if (lpValid != NULL)
m_strValid = lpValid;
else
m_strValid.Empty();
m_bUseMask = TRUE;
}
void CMaskEdit::SendChar(UINT nChar)
{
m_bMaskKeyInProgress = TRUE;
#ifdef WIN32
AfxCallWndProc(this, m_hWnd, WM_CHAR, nChar, 1);
#else
SendMessage(WM_CHAR, nChar, 1);
#endif
m_bMaskKeyInProgress = FALSE;
}
void CMaskEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!m_bMaskKeyInProgress)
if (!CheckChar(nChar)) return;
if (m_bUseMask)
{
if (isprint(nChar))
{
// si un masque existe, on est en insert mode
int startPos, endPos;
GetSel(startPos, endPos);
SetSel(startPos, endPos+1);
Clear();
m_str.SetAt(endPos, nChar); // added this
}
else if (nChar == VK_BACK)
{
int startPos, endPos;
GetSel(startPos, endPos);
// sanity range check
if ((startPos == endPos) && (startPos >= 1) && (startPos <= m_str.GetLength()))
{
char c;
// get the masked literal representation
if (!m_strMaskLiteral.IsEmpty())
c = m_strMaskLiteral[startPos-1];
TRACE("m_strMaskLiteral = [%s](%s)\n", m_strMaskLiteral, m_str);
// back space the cursor
SendMessage(WM_KEYDOWN, VK_LEFT, 0);
if (!m_strMaskLiteral.IsEmpty())
{
// update the char backspacing over
SendChar(c);
// back space the cursor again
SendMessage(WM_KEYDOWN, VK_LEFT, 0);
}
}
else // out of range or have more than one char selected
MessageBeep((UINT)-1);
return;
}
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
if (!m_bMaskKeyInProgress && m_bUseMask && !m_strLiteral.IsEmpty())
{
int startPos, endPos;
GetSel(startPos, endPos);
// make sure the string is not longer than the mask
if (endPos < m_strLiteral.GetLength())
{
UINT c = m_strLiteral.GetAt(endPos);
if (c != '_') SendChar(c);
}
}
}
BOOL CMaskEdit::CheckChar(UINT nChar)
{
UINT c;
// do not use mask
if (!m_bUseMask) return TRUE;
// control character, OK
if (!isprint(nChar)) return TRUE;
// unselect all selections, if any
int startPos, endPos;
GetSel(startPos, endPos);
SetSel(-1, 0);
SetSel(startPos, startPos);
// check the key against the mask
GetSel(startPos, endPos);
// make sure the string is not longer than the mask
if (endPos >= m_strMask.GetLength())
{
MessageBeep((UINT)-1);
return FALSE;
}
// check to see if a literal is in this position
c = '_';
if (!m_strLiteral.IsEmpty())
c = m_strLiteral.GetAt(endPos);
if (c != '_')
{
SendChar(c);
GetSel(startPos, endPos);
}
// check the valid string character
if (m_strValid.Find(nChar) != -1) return TRUE;
// check the key against the mask
c = m_strMask.GetAt(endPos);
BOOL doit = TRUE;
switch (c)
{
case '0': // digit only //completely changed this
{
BOOL doit = TRUE;
if(isdigit(nChar))
{
if(m_isdate)
{
if(endPos == 0)
{
if(nChar > '3')
doit = FALSE;
}
if(endPos == 1)
{
if(m_str.GetAt(0) == '3')
{
if(nChar > '1')
doit = FALSE;
}
}
if(endPos == 3)
{
if(nChar > '1')
doit = FALSE;
}
if(endPos == 4)
{
if(m_str.GetAt(3) == '1')
{
if(nChar > '2')
doit = FALSE;
}
}
}
else
{
if(endPos == 0)
{
if(nChar > (UINT)m_strHours[0])
doit = FALSE;
}
if(endPos == 1)
{
if(m_str.GetAt(0) == m_strHours[0])
{
if(nChar > (UINT)m_strHours[1])
doit = FALSE;
}
}
if(endPos == 3)
{
if(nChar > (UINT)m_strMins[0])
doit = FALSE;
}
if(endPos == 4)
{
if(m_str.GetAt(3) == m_strMins[0])
{
if(nChar > (UINT)m_strMins[1])
doit = FALSE;
}
}
}
return doit;
}
break;
}
case '9': // digit or space
{
if (isdigit(nChar)) return TRUE;
if (nChar != VK_SPACE) return TRUE;
break;
}
case '#': // digit or space or '+' or '-'
{
if (isdigit(nChar)) return TRUE;
if (nChar == VK_SPACE || nChar == VK_ADD || nChar == VK_SUBTRACT) return TRUE;
break;
}
case 'L': // alpha only
{
if (isalpha(nChar)) return TRUE;
break;
}
case '?': // alpha or space
{
if (isalpha(nChar)) return TRUE;
if (nChar == VK_SPACE) return TRUE;
break;
}
case 'A': // alpha numeric only
{
if (isalnum(nChar)) return TRUE;
break;
}
case 'a': // alpha numeric or space
{
if (isalnum(nChar)) return TRUE;
if (nChar == VK_SPACE) return TRUE;
break;
}
case '&': // all print character only
{
if (isprint(nChar)) return TRUE;
break;
}
}
MessageBeep((UINT)-1);
return FALSE;
}
void CMaskEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// si un masque existe, tester les touches sp閏iales
if (m_bUseMask)
{
switch (nChar)
{
case VK_DELETE:
case VK_INSERT: return;
}
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
CDateTimeEdit::CDateTimeEdit()
{ m_bisTime = FALSE;
m_isdate = FALSE;
m_bUseMask = TRUE;
m_strMask = "00.00.00 00:00:00";
m_strLiteral = "__.__.__ __:__:__";
m_str = "00.01.01 00:00:00";
m_strMaskLiteral = m_str;
}
CDateTimeEdit::~CDateTimeEdit()
{
}
BOOL CDateTimeEdit::SubclassDlgItem(UINT nID, CWnd* pParent)
{
if(CEdit::SubclassDlgItem(nID,pParent))
{
SetWindowText(m_str);
return 1;
}
return 0;
}
int CDateTimeEdit::GetTime(int &year, int &mon, int &day, int &hour, int &min, int &sec)
{
char buf[40];
GetWindowText(buf,40);
sscanf(buf,"%02d.%02d.%02d %02d:%02d:%02d",&year,&mon,&day,&hour,&min,&sec);
year+=2000;
return 0;
}
unsigned long CDateTimeEdit::GetTimeCode()
{
extern unsigned long int GetDefineTime(int y,int mon,int d,int h,int m,int s);
int y, mon, d, h, m, s;
GetTime(y,mon,d,h,m,s);
return GetDefineTime(y,mon,d,h,m,s);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -