📄 amsedit.cpp
字号:
else if (nYear > m_dateMax.GetYear())
nYear = m_dateMax.GetYear();
return nYear;
}
int CAMSEdit::DateBehavior::GetYear() const
{
CString strText = m_pEdit->GetText();
int nSlash = strText.ReverseFind(m_cSep);
if (nSlash > 0)
return _ttoi(strText.Mid(nSlash + 1));
return 0;
}
int CAMSEdit::DateBehavior::GetDay() const
{
CString strText = m_pEdit->GetText();
int nSlash = strText.Find(m_cSep);
if (nSlash > 0)
return _ttoi(strText.Mid(nSlash + 1, 2));
return 0;
}
int CAMSEdit::DateBehavior::GetValidDay() const
{
int nDay = GetDay();
// It it's outside the range, fix it
if (nDay < GetMinDay())
nDay = GetMinDay();
else if (nDay > GetMaxDay())
nDay = GetMaxDay();
return nDay;
}
void CAMSEdit::DateBehavior::SetYear(int nYear)
{
ASSERT(IsValidYear(nYear));
SelectionSaver selection = m_pEdit; // remember the current selection
if (GetYear() > 0) // see if there's already a year
m_pEdit->SetSel(GetYearStartPosition(), GetYearStartPosition() + 4);
CString strText;
strText.Format(_T("%4d"), nYear);
m_pEdit->ReplaceSel(strText, TRUE); // set the year
AdjustMaxMonthAndDay(); // adjust the month and/or day if they're out of range
}
void CAMSEdit::DateBehavior::SetMonth(int nMonth)
{
ASSERT(IsValidMonth(nMonth));
SelectionSaver selection = m_pEdit; // remember the current selection
if (GetMonth() > 0) // see if there's already a month
m_pEdit->SetSel(GetMonthStartPosition(), GetMonthStartPosition() + 3);
CString strText;
strText.Format(_T("%02d%c"), nMonth, m_cSep);
m_pEdit->ReplaceSel(strText, TRUE); // set the month
AdjustMaxDay(); // adjust the day if it's out of range
}
void CAMSEdit::DateBehavior::SetDay(int nDay)
{
ASSERT(IsValidDay(nDay));
SelectionSaver selection = m_pEdit; // remember the current selection
if (GetDay() > 0) // see if there's already a day
m_pEdit->SetSel(GetDayStartPosition(), GetDayStartPosition() + 3);
CString strText;
strText.Format(_T("%02d%c"), nDay, m_cSep);
m_pEdit->ReplaceSel(strText, TRUE); // set the day
}
void CAMSEdit::DateBehavior::ShowErrorMessage() const
{
CString strMessage;
strMessage.Format(_T("Please specify a date between %s and %s."), m_dateMin.Format(_T("%m/%d/%Y")), m_dateMax.Format(_T("%m/%d/%Y")));
AfxMessageBox(strMessage, MB_ICONEXCLAMATION);
}
CTime CAMSEdit::DateBehavior::GetDate() const
{
return CTime(GetYear(), GetMonth(), GetDay(), 0, 0, 0);
}
COleDateTime CAMSEdit::DateBehavior::GetOleDate() const
{
return COleDateTime(GetYear(), GetMonth(), GetDay(), 0, 0, 0);
}
void CAMSEdit::DateBehavior::SetDate(int nYear, int nMonth, int nDay)
{
#ifdef _DEBUG // verify it's within the range (when debugging)
COleDateTime date(nYear, nMonth, nDay, 0, 0, 0);
ASSERT(date >= m_dateMin);
ASSERT(date <= m_dateMax);
#endif
CString strText;
if (m_uFlags & DayBeforeMonth)
strText.Format(_T("%02d%c%02d%c%4d"), nDay, m_cSep, nMonth, m_cSep, nYear);
else
strText.Format(_T("%02d%c%02d%c%4d"), nMonth, m_cSep, nDay, m_cSep, nYear);
m_pEdit->SetWindowText(strText);
}
void CAMSEdit::DateBehavior::SetDate(const CTime& date)
{
SetDate(date.GetYear(), date.GetMonth(), date.GetDay());
}
void CAMSEdit::DateBehavior::SetDate(const COleDateTime& date)
{
SetDate(date.GetYear(), date.GetMonth(), date.GetDay());
}
void CAMSEdit::DateBehavior::SetDateToToday()
{
SetDate(COleDateTime::GetCurrentTime());
}
bool CAMSEdit::DateBehavior::IsLeapYear(int nYear)
{
return (nYear & 3) == 0 && (nYear % 100 != 0 || nYear % 400 == 0);
}
bool CAMSEdit::DateBehavior::IsValid() const
{
COleDateTime date(GetYear(), GetMonth(), GetDay(), 0, 0, 0);
return (date.GetStatus() == COleDateTime::valid && date >= m_dateMin && date <= m_dateMax);
}
bool CAMSEdit::DateBehavior::CheckIfValid(bool bShowErrorIfNotValid /*= true*/)
{
if (!m_pEdit->IsWindowEnabled())
return true;
bool bValid = IsValid();
if (!bValid && bShowErrorIfNotValid)
{
ShowErrorMessage();
m_pEdit->SetFocus();
}
return bValid;
}
LONG CAMSEdit::DateBehavior::_OnPaste(UINT, LONG)
{
int nStart, nEnd;
m_pEdit->GetSel(nStart, nEnd);
CString strTextBefore = m_pEdit->GetText();
_Default();
if (!IsValid())
{
MessageBeep(MB_ICONEXCLAMATION);
m_pEdit->SetWindowText(strTextBefore);
m_pEdit->SetSel(nStart, nEnd);
}
return 0;
}
void CAMSEdit::DateBehavior::SetRange(const CTime& dateMin, const CTime& dateMax)
{
ASSERT(dateMin >= AMS_MIN_CTIME);
ASSERT(dateMax <= AMS_MAX_CTIME);
ASSERT(dateMin <= dateMax);
m_dateMin.SetDate(dateMin.GetYear(), dateMin.GetMonth(), dateMin.GetDay());
m_dateMax.SetDate(dateMax.GetYear(), dateMax.GetMonth(), dateMax.GetDay());
_Redraw();
}
void CAMSEdit::DateBehavior::SetRange(const COleDateTime& dateMin, const COleDateTime& dateMax)
{
ASSERT(dateMin >= AMS_MIN_OLEDATETIME);
ASSERT(dateMax <= AMS_MAX_OLEDATETIME);
ASSERT(dateMin <= dateMax);
m_dateMin = dateMin;
m_dateMax = dateMax;
_Redraw();
}
void CAMSEdit::DateBehavior::GetRange(CTime* pDateMin, CTime* pDateMax) const
{
ASSERT(pDateMin);
ASSERT(pDateMax);
*pDateMin = CTime(m_dateMin.GetYear(), m_dateMin.GetMonth(), m_dateMin.GetDay(), 0, 0, 0);
*pDateMax = CTime(m_dateMax.GetYear(), m_dateMax.GetMonth(), m_dateMax.GetDay(), 0, 0, 0);
}
void CAMSEdit::DateBehavior::GetRange(COleDateTime* pDateMin, COleDateTime* pDateMax) const
{
ASSERT(pDateMin);
ASSERT(pDateMax);
*pDateMin = m_dateMin;
*pDateMax = m_dateMax;
}
void CAMSEdit::DateBehavior::SetSeparator(TCHAR cSep)
{
ASSERT(cSep);
ASSERT(!_istdigit(cSep));
if (m_cSep != cSep)
{
m_cSep = cSep;
_Redraw();
}
}
TCHAR CAMSEdit::DateBehavior::GetSeparator() const
{
return m_cSep;
}
void CAMSEdit::DateBehavior::ShowDayBeforeMonth(bool bDayBeforeMonth /*= true*/)
{
ModifyFlags(bDayBeforeMonth ? DayBeforeMonth : 0, bDayBeforeMonth ? 0 : DayBeforeMonth);
}
bool CAMSEdit::DateBehavior::IsDayShownBeforeMonth() const
{
return (m_uFlags & DayBeforeMonth) ? true : false;
}
void CAMSEdit::DateBehavior::ModifyFlags(UINT uAdd, UINT uRemove)
{
UINT uFlags = (m_uFlags & ~uRemove) | uAdd;
if (m_uFlags != uFlags)
{
m_uFlags = uFlags;
_Redraw();
}
}
UINT CAMSEdit::DateBehavior::GetFlags() const
{
return m_uFlags;
}
CString CAMSEdit::DateBehavior::_GetValidText() const
{
CString strText = m_pEdit->GetText();
if (strText.IsEmpty() || IsValid())
return strText;
// If the date is empty, try using today
if (GetYear() == 0 && GetMonth() == 0 && GetDay() == 0)
((CAMSEdit::DateBehavior*)this)->SetDateToToday();
int nYear = GetValidYear();
int nMonth = GetValidMonth();
int nDay = GetValidDay();
if (!IsValid(COleDateTime(nYear, nMonth, nDay, 0, 0, 0)))
nMonth = GetMinMonth();
if (!IsValid(COleDateTime(nYear, nMonth, nDay, 0, 0, 0)))
nDay = GetMaxDay();
CString strNewText;
if (m_uFlags & DayBeforeMonth)
strNewText.Format(_T("%02d%c%02d%c%4d"), nDay, m_cSep, nMonth, m_cSep, nYear);
else
strNewText.Format(_T("%02d%c%02d%c%4d"), nMonth, m_cSep, nDay, m_cSep, nYear);
return strNewText;
}
/////////////////////////////////////////////////////////////////////////////
// CAMSAlphanumericEdit window
// Constructs the object using the given set of strInvalidChars
CAMSAlphanumericEdit::CAMSAlphanumericEdit(int nMaxChars /*= 0*/, const CString& strInvalidChars /*= _T("%'*\"+?><:\\"")*/) :
AlphanumericBehavior(this, nMaxChars, strInvalidChars)
{
}
BEGIN_MESSAGE_MAP(CAMSAlphanumericEdit, CEdit)
//{{AFX_MSG_MAP(CAMSAlphanumericEdit)
ON_WM_CHAR()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CLEAR, OnClear)
ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()
// Returns the current window's text in a valid format
CString CAMSAlphanumericEdit::GetValidText() const
{
return _GetValidText();
}
void CAMSAlphanumericEdit::OnChar(UINT uChar, UINT nRepCnt, UINT nFlags)
{
_OnChar(uChar, nRepCnt, nFlags);
}
/////////////////////////////////////////////////////////////////////////////
// CAMSMaskedEdit window
// Constructs the object using the given numeric strMask.
CAMSMaskedEdit::CAMSMaskedEdit(const CString& strMask /*= _T("")*/) :
MaskedBehavior(this, strMask)
{
}
BEGIN_MESSAGE_MAP(CAMSMaskedEdit, CEdit)
//{{AFX_MSG_MAP(CAMSMaskedEdit)
ON_WM_CHAR()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CLEAR, OnClear)
ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()
// Returns the current window's text in a valid format
CString CAMSMaskedEdit::GetValidText() const
{
return _GetValidText();
}
// Cuts the current selection into the clipboard.
LONG CAMSMaskedEdit::OnCut(UINT, LONG)
{
int nStart, nEnd;
GetSel(nStart, nEnd);
CString str = _T("");
if (nStart < nEnd)
{
SendMessage(WM_COPY); // copy the selection and...
// SendMessage(WM_KEYDOWN, VK_DELETE); // delete it
GetWindowText(str);
str = str.Left(nStart);
for(int i = nStart; i < m_strMask.GetLength() && i < nEnd; i++)
{
TCHAR cMask = m_strMask.GetAt(i);
if(CheckCode(0, cMask) == 100)
str += cMask;
else
str += _T(" ");
}
SetWindowText(str);
SetSel(nStart, nStart);
}
return 0;
}
void CAMSMaskedEdit::OnChar(UINT uChar, UINT nRepCnt, UINT nFlags)
{
_OnChar(uChar, nRepCnt, nFlags);
}
void CAMSMaskedEdit::OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags)
{
_OnKeyDown(uChar, nRepCnt, nFlags);
}
/////////////////////////////////////////////////////////////////////////////
// CAMSNumericEdit window
// Constructs the object using the given nMaxWholeDigits and nMaxDecimalPlaces.
CAMSNumericEdit::CAMSNumericEdit(int nMaxWholeDigits /*= 9*/, int nMaxDecimalPlaces /*= 4*/) :
NumericBehavior(this, nMaxWholeDigits, nMaxDecimalPlaces)
{
}
BEGIN_MESSAGE_MAP(CAMSNumericEdit, CEdit)
//{{AFX_MSG_MAP(CAMSNumericEdit)
ON_WM_CHAR()
ON_WM_KEYDOWN()
ON_WM_KILLFOCUS()
ON_WM_MOUSEMOVE()
ON_WM_SETFOCUS()
ON_WM_NCPAINT()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CLEAR, OnClear)
ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()
CString CAMSNumericEdit::GetValidText() const
{
return _GetValidText();
}
void CAMSNumericEdit::OnChar(UINT uChar, UINT nRepCnt, UINT nFlags)
{
_OnChar(uChar, nRepCnt, nFlags);
}
void CAMSNumericEdit::OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags)
{
_OnKeyDown(uChar, nRepCnt, nFlags);
}
/////////////////////////////////////////////////////////////////////////////
// CAMSIntegerEdit window
// Constructs the object allowing the negative sign or not.
CAMSIntegerEdit::CAMSIntegerEdit(int nMaxWholeDigits /*= 9*/) :
CAMSNumericEdit(nMaxWholeDigits, 0)
{
}
BEGIN_MESSAGE_MAP(CAMSIntegerEdit, CEdit)
//{{AFX_MSG_MAP(CAMSIntegerEdit)
ON_WM_CHAR()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CLEAR, OnClear)
ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()
long CAMSIntegerEdit::GetTextAsLong() const
{
return _ttol(GetText());
}
/////////////////////////////////////////////////////////////////////////////
// CAMSNumericEdit window
// Constructs the object using the given numeric strMask.
CAMSCurrencyEdit::CAMSCurrencyEdit()
{
m_nDigitsInGroup = 3;
m_nMaxDecimalPlaces = 2;
//m_strPrefix = _T("$");
m_strPrefix = _T("");
// Get the system's current settings
TCHAR szValue[10];
// if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, szValue, 0))
// {
// ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, szValue, sizeof(szValue));
// m_strPrefix = szValue;
// }
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONGROUPING, szValue, 0))
{
::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONGROUPING, szValue, sizeof(szValue));
m_nDigitsInGroup = _ttoi(szValue);
}
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, szValue, 0))
{
::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, szValue, sizeof(szValue));
m_nMaxDecimalPlaces = _ttoi(szValue);
}
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONDECIMALSEP, &m_cDecimalPoint, 0))
::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONDECIMALSEP, &m_cDecimalPoint, sizeof(m_cDecimalPoint));
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, &m_cGroupSeparator, 0))
::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, &m_cGroupSeparator, sizeof(m_cGroupSeparator));
}
BEGIN_MESSAGE_MAP(CAMSCurrencyEdit, CEdit)
//{{AFX_MSG_MAP(CAMSCurrencyEdit)
ON_WM_CHAR()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CLEAR, OnClear)
ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAMSDateEdit
CAM
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -