📄 bcgpdatetimectrl.cpp
字号:
int iDay = m_Date.GetDay ();
int iMonth = m_Date.GetMonth ();
int iYear = m_Date.GetYear ();
int iHour = m_Date.GetHour ();
int iMin = m_Date.GetMinute ();
const int iYearDigits = m_type2DigitsInYear ? 2 : 4;
int iNumber;
if (m_iPrevDigit == -1)
{
iNumber = iDigit;
m_iYearPos = 0;
}
else
{
iNumber = m_iPrevDigit * 10 + iDigit;
}
switch (m_CurrPartType)
{
case NO:
case CHECK_BOX:
case AMPM:
return;
case DAY:
iDay = iNumber;
break;
case HOUR:
if (!m_b24HoursFormat)
{
BOOL bPM = (iHour >= 12);
iHour = iNumber;
if (iHour > 12)
{
iHour = iDigit;
}
if (bPM)
{
iHour += 12;
}
if (iHour == 24)
{
iHour = 0;
}
if (iDigit != 1) // Only 10, 11 or 12 are allowed!
{
iDigit = -1;
}
}
else // Normal format
{
iHour = iNumber;
}
break;
case MIN:
iMin = iNumber;
break;
case MONTH:
if (m_monthFormat == 2 &&
iNumber >= 1 && iNumber <= 12)
{
iMonth = iNumber;
if (iDigit > 1 && m_iPrevDigit == -1)
{
m_iPrevDigit = 0; // Stimulate junp to the next part
}
}
break;
case YEAR:
if (m_type2DigitsInYear)
{
iYear = iYear / 100 * 100 + iNumber;
if (iYear < m_maxYear2Digits - 99)
{
// iYear += 100;
iYear = m_maxYear2Digits / 100 * 100 + iNumber;
}
if (iYear > m_maxYear2Digits)
{
// iYear -= 100;
iYear = (m_maxYear2Digits - 100) / 100 * 100 + iNumber;
}
}
else
{
ASSERT (m_iYearPos >= 0);
ASSERT (m_iYearPos < iYearDigits);
//------------------------------------------------
// Replace digit in position m_iYearPos to iDigit:
//------------------------------------------------
int iYearNew = 0;
for (int iPos = 0; iPos < iYearDigits; iPos ++)
{
int iTens = 1;
for (int i = 0; i < iYearDigits - iPos - 1; i ++)
{
iTens *= 10;
}
iYearNew *= 10;
if (iPos == m_iYearPos)
{
iYearNew += iDigit;
}
else
{
iYearNew += iYear / iTens;
}
iYear %= iTens;
}
iYear = iYearNew;
}
m_iYearPos ++;
break;
}
COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
BOOL bValidDate = IsDateValid (date);
if (bValidDate)
{
m_Date = date;
RedrawWindow (m_rectText);
OnDateChanged ();
}
if (m_iPrevDigit == -1) // First push
{
m_iPrevDigit = iDigit;
}
else
{
if (bValidDate && m_iPartNum < m_iPartsNumber - 1)
{
if (m_CurrPartType != YEAR || m_iYearPos == iYearDigits)
{
m_iPrevDigit = -1;
SelectNext ();
}
}
}
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::ChangeMonth (UINT uiMonthLetter)
{
ASSERT (m_monthFormat == 0 || m_monthFormat == 1);
CBCGPDefaultLocale dl;
int iDay = m_Date.GetDay ();
int iMonth = m_Date.GetMonth ();
int iYear = m_Date.GetYear ();
int iHour = m_Date.GetHour ();
int iMin = m_Date.GetMinute ();
BOOL bLastDayInMonth = (iDay == GetDaysInMonth (iMonth, iYear));
BOOL bFound = FALSE;
for (int i = iMonth + 1; i != iMonth; i ++)
{
if (i > 12)
{
i = 1;
}
if (i == iMonth)
{
break;
}
//--------------------------------------------------
// Compare manth 'i' first char with the typed char:
//--------------------------------------------------
CString strMonth = COleDateTime (iYear, i, 1, 0, 0, 0).
Format (m_monthFormat == 0 ? _T ("%b") : _T ("%B"));
if (strMonth.GetLength () > 1 &&
strMonth.GetAt (0) == (char) uiMonthLetter)
{
iMonth = i;
bFound = TRUE;
break;
}
}
if (bFound)
{
if (bLastDayInMonth ||
iDay > GetDaysInMonth (iMonth, iYear))
{
iDay = GetDaysInMonth (iMonth, iYear);
}
COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
if (IsDateValid (date))
{
m_Date = date;
RedrawWindow (m_rectText);
OnDateChanged ();
}
}
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::ChangeAmPm (UINT uiAmPm)
{
int iDay = m_Date.GetDay ();
int iMonth = m_Date.GetMonth ();
int iYear = m_Date.GetYear ();
int iHour = m_Date.GetHour ();
int iMin = m_Date.GetMinute ();
CString str = (char) uiAmPm;
str.MakeUpper ();
if (str == m_strPM [0] && iHour < 12)
{
iHour += 12;
}
if (str == m_strAM [0] && iHour >= 12)
{
iHour -= 12;
}
COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
if (IsDateValid (date) && m_Date != date)
{
m_Date = date;
RedrawWindow (m_rectText);
OnDateChanged ();
}
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::ToggleCheck ()
{
m_bIsChecked = !m_bIsChecked;
if (m_spinButton)
{
m_wndSpin.EnableWindow (m_bIsChecked);
}
COleDateTime emptyDate;
if (m_bIsChecked && m_Date == emptyDate)
{
m_Date = COleDateTime::GetCurrentTime ();
OnDateChanged ();
}
RedrawWindow ();
}
//*****************************************************************************************
BOOL CBCGPDateTimeCtrl::IsDateValid (COleDateTime& date) const
{
if (date.GetStatus () == COleDateTime::invalid)
{
return FALSE;
}
COleDateTime dateEmpty;
if (m_MinDate != dateEmpty && date < m_MinDate)
{
return FALSE;
}
if (m_MaxDate != dateEmpty && date > m_MaxDate)
{
return FALSE;
}
return TRUE;
}
//*****************************************************************************************
BOOL CBCGPDateTimeCtrl::IsDatePart (int iPart) const
{
PART_TYPE type = m_arPartsOrder [iPart];
return type == DAY || type == MONTH || type == YEAR;
}
//*****************************************************************************************
BOOL CBCGPDateTimeCtrl::IsTimePart (int iPart) const
{
PART_TYPE type = m_arPartsOrder [iPart];
return type == HOUR || type == MIN;
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::OnHideCalendarPopup ()
{
m_bDropButtonIsPressed = FALSE;
RedrawWindow (m_rectDropButton);
}
//****************************************************************************************
void CBCGPDateTimeCtrl::OnSetFocus(CWnd* pOldWnd)
{
CButton::OnSetFocus(pOldWnd);
if (m_iPartNum >= 0 && m_iPartNum < m_iPartsNumber)
{
m_CurrPartType = m_arPartsOrder [m_iPartNum];
}
m_bShowSelection = TRUE;
RedrawWindow ();
}
//****************************************************************************************
BOOL CBCGPDateTimeCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
if (wParam == iSpinID)
{
NM_UPDOWN* pNM = (NM_UPDOWN*) lParam;
ASSERT (pNM != NULL);
if (pNM->hdr.code == UDN_DELTAPOS)
{
ScrollCurrPart (pNM->iDelta < 0 ? 1 : -1);
}
SetFocus ();
}
return CButton::OnNotify(wParam, lParam, pResult);
}
//****************************************************************************************
void CBCGPDateTimeCtrl::OnKillFocus(CWnd* pNewWnd)
{
m_bShowSelection = FALSE;
m_iPrevDigit = -1;
RedrawWindow ();
if (m_pPopup == NULL)/* || pNewWnd == NULL ||
m_pPopup->m_hWnd != pNewWnd->m_hWnd)*/
{
// FireOnKillFocus (pNewWnd == NULL ? 0 : (long) pNewWnd->GetSafeHwnd ());
}
CButton::OnKillFocus(pNewWnd);
}
//****************************************************************************************
void CBCGPDateTimeCtrl::BuidWidestDate (CDC* pDC)
{
ASSERT_VALID (pDC);
CBCGPDefaultLocale dl;
//-----------------------
// Find the widest month:
//-----------------------
int iMaxMonth = 1;
if (m_monthFormat == 2) // Numeric
{
iMaxMonth = 9;
}
else
{
int iMaxMonthWidth = 0;
for (int iMonth = 1; iMonth <= 12; iMonth ++)
{
COleDateTime date (1998, iMonth, 1, 0, 0, 0);
CString strMonth = date.Format (m_monthFormat == 0 ? _T ("%b") : _T("%B"));
int iMonthWidth = pDC->GetTextExtent (strMonth).cx;
if (iMonthWidth > iMaxMonthWidth)
{
iMaxMonthWidth = iMonthWidth;
iMaxMonth = iMonth;
}
}
}
m_WidestDate = COleDateTime (2000, iMaxMonth, 20, 0, 0, 0);
}
//****************************************************************************************
void CBCGPDateTimeCtrl::SizeToContent()
{
CRect rectClient;
GetClientRect (rectClient);
AdjustControl (rectClient);
}
//*************************************************************************************
void CBCGPDateTimeCtrl::OnDateChanged ()
{
CWnd* pParent = GetParent ();
if (pParent != NULL)
{
pParent->SendMessage ( WM_COMMAND,
MAKEWPARAM (GetDlgCtrlID (), BN_CLICKED),
(LPARAM) m_hWnd);
}
}
void CBCGPDateTimeCtrl::SetFirstDayOfWeek(int nDay) // 0 - 6
{
m_weekStart = nDay + 1;
}
UINT CBCGPDateTimeCtrl::GetState () const
{
UINT stateFlags = 0;
if (m_spinButton)
stateFlags |= DTM_SPIN;
if (m_dropCalendar)
stateFlags |= DTM_DROPCALENDAR;
if (m_showDate)
stateFlags |= DTM_DATE;
if (m_b24HoursFormat)
stateFlags |= DTM_TIME24H;
if (m_checkButton)
stateFlags |= DTM_CHECKBOX;
if (m_showTime)
stateFlags |= DTM_TIME;
if (m_bIsChecked)
stateFlags |= DTM_CHECKED;
if (m_b24HoursByLocale)
stateFlags |= DTM_TIME24HBYLOCALE;
return stateFlags;
}
void CBCGPDateTimeCtrl::SetState (UINT stateFlags, UINT stateMask)
{
if(!(stateFlags & DTM_DATE) && !(stateFlags & DTM_TIME))
stateFlags |= (DTM_DATE | DTM_TIME);
if(stateMask & DTM_SPIN)
m_spinButton =
((stateFlags & DTM_SPIN) != 0);
if(stateMask & DTM_DROPCALENDAR)
m_dropCalendar =
((stateFlags & DTM_DROPCALENDAR) != 0);
if(stateMask & DTM_DATE)
m_showDate =
((stateFlags & DTM_DATE) != 0);
if(stateMask & DTM_TIME24H)
m_b24HoursFormat =
((stateFlags & DTM_TIME24H) != 0);
if(stateMask & DTM_CHECKBOX)
m_checkButton =
((stateFlags & DTM_CHECKBOX) != 0);
if(stateMask & DTM_TIME)
m_showTime =
((stateFlags & DTM_TIME) != 0);
if(stateMask & DTM_CHECKED)
m_bIsChecked =
((stateFlags & DTM_CHECKED) != 0);
if(stateMask & DTM_TIME24HBYLOCALE)
m_b24HoursByLocale =
((stateFlags & DTM_TIME24HBYLOCALE) != 0);
if(::IsWindow (m_hWnd))
{
SizeToContent();
}
}
void CBCGPDateTimeCtrl::PreSubclassWindow()
{
if (!m_bIsInitialized)
{
ModifyStyle (BS_DEFPUSHBUTTON, BS_OWNERDRAW | WS_BORDER);
}
else
{
ModifyStyle (BS_DEFPUSHBUTTON, BS_OWNERDRAW);
}
CButton::PreSubclassWindow();
}
BOOL CBCGPDateTimeCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= BS_OWNERDRAW;
cs.style &= ~BS_DEFPUSHBUTTON;
m_bIsInitialized = TRUE;
return CButton::PreCreateWindow(cs);
}
BOOL CBCGPDateTimeCtrl::OnEraseBkgnd(CDC* /*pDC*/)
{
return TRUE;
}
void CBCGPDateTimeCtrl::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
if (GetStyle() & WS_BORDER)
{
lpncsp->rgrc[0].left++;
lpncsp->rgrc[0].top++ ;
lpncsp->rgrc[0].right--;
lpncsp->rgrc[0].bottom--;
}
CButton::OnNcCalcSize(bCalcValidRects, lpncsp);
}
void CBCGPDateTimeCtrl::OnNcPaint()
{
if (GetStyle () & WS_BORDER)
{
CBCGPVisualManager::GetInstance ()->OnDrawControlBorder (this);
}
}
BOOL CBCGPDateTimeCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_bIsChecked)
{
CPoint ptCursor;
::GetCursorPos (&ptCursor);
ScreenToClient (&ptCursor);
if (m_rectText.PtInRect (ptCursor))
{
::SetCursor (AfxGetApp ()->LoadStandardCursor (IDC_IBEAM));
return TRUE;
}
}
return CButton::OnSetCursor(pWnd, nHitTest, message);
}
//*****************************************************************************
LRESULT CBCGPDateTimeCtrl::OnSetFont (WPARAM wParam, LPARAM lParam)
{
BOOL bRedraw = (BOOL) LOWORD (lParam);
m_hFont = (HFONT) wParam;
if (bRedraw)
{
Invalidate ();
UpdateWindow ();
}
return 0;
}
//*****************************************************************************
LRESULT CBCGPDateTimeCtrl::OnGetFont (WPARAM, LPARAM)
{
return (LRESULT) m_hFont;
}
//******************************************************************************
void CBCGPDateTimeCtrl::OnSize(UINT nType, int cx, int cy)
{
CButton::OnSize(nType, cx, cy);
CRect rectClient;
GetClientRect (rectClient);
AdjustControl (rectClient);
}
//*******************************************************************************
void CBCGPDateTimeCtrl::SetTextColor (COLORREF color, BOOL bRedraw)
{
m_colorText = color;
if (bRedraw && GetSafeHwnd () != NULL)
{
RedrawWindow ();
}
}
#endif // BCG_NO_CALENDAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -