15#include "helper/STime.h"
20static char THIS_FILE[] = __FILE__;
28extern WORD gLunarMonthDay[];
29extern BYTE gLunarMonth[];
30extern BYTE gLunarHolDay[];
32#define REFTYPE_SUJIU 1
33#define REFTYPE_MEIYU 2
34#define REFTYPE_SANFU 3
36#define TITLE_HEIGHT 20.f
37#define FOOTER_HEIGHT 20.f
40#define NAME_BTN_TODAY "btn_today"
41#define NAME_LABEL_TODAY "label_today"
43WORD
const DayOrdinal[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
44WORD
const DayOrdinalLeap[13] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
48 if (wYear < 1600 || wYear >= 7000)
52 if (wYear % 4 == 0 && wYear % 100 != 0 || wYear % 400 == 0)
70 BOOL bLeapYear = FALSE;
75 if (bLeapYear == TRUE)
77 wDays = DayOrdinalLeap[wMonth - 1] + wDay - 1;
81 wDays = DayOrdinal[wMonth - 1] + wDay - 1;
93 BOOL bLeapYear = FALSE;
98 if (bLeapYear == TRUE)
114 for (i = 0; i < 12; i++)
116 if (bLeapYear == TRUE)
118 if (wDays >= DayOrdinalLeap[i] && wDays < DayOrdinalLeap[i + 1])
121 wDay = wDays - DayOrdinalLeap[i];
127 if (wDays >= DayOrdinal[i] && wDays < DayOrdinal[i + 1])
130 wDay = wDays - DayOrdinal[i];
140 if (wMonth > 12 || wMonth < 1)
143 if (wDay < 1 || wDay > 31)
146 if (wMonth == 4 || wMonth == 6 || wMonth == 9 || wMonth == 11)
153 else if (wMonth == 2)
155 BOOL bLeapYear = FALSE;
157 if (bLeapYear == FALSE)
183 unsigned int DayofWeek = 0;
186 DayofWeek = (wYear + wYear / 4 - wYear / 100 + wYear / 400 + uDayOrd) % 7;
196 WORD days1 = 0, days2 = 0;
215 return SStringT().Format(_T(
"%d"), iYear);
220 const TCHAR *szText[] = { _T(
"一"), _T(
"二"), _T(
"三"), _T(
"四"), _T(
"五"), _T(
"六"), _T(
"七"), _T(
"八"), _T(
"九"), _T(
"十"), _T(
"十一"), _T(
"十二") };
221 SASSERT(iMonth <= 12 && iMonth >= 1);
222 return szText[iMonth - 1];
227 return SStringT().Format(_T(
"%d"), iDay);
233 if (!
SetDate(iYeay, iMonth, iDay))
253 __baseCls::OnLanguageChanged();
254 for (UINT i = 0; i < 7; i++)
269 m_evtSet.addEvent(EVENTID(EventCalendarExChanged));
289 TCHAR sztext[][4] = { _T(
"日"), _T(
"一"), _T(
"二"), _T(
"三"), _T(
"四"), _T(
"五"), _T(
"六") };
290 for (
int i = 0; i < 7; i++)
301 for (
int i = 0; i < 7; i++)
344 WORD nFrontDayCount = 0;
350 for (
int i = 0; i < iWeek; ++i)
353 m_arrDays[i].iDay = nFrontDayCount - iWeek + i + 1;
358 for (
int i = 0; i < nDayCount; ++i)
365 for (
int i = iWeek + nDayCount; i < 42; ++i)
379 EventCalendarExChanged evt(
this);
383 evt.nBtnType = nBtnType;
398 for (WORD i = 0; i < 12; i++)
415 for (WORD i = 1; i <= 10; i++)
436 for (WORD i = 1; i <= 10; i++)
521 if (rect.IsRectEmpty())
526 DWORD dwPrevState = 0;
535 int n = (nHeight - si.cy) / 2;
538 rcItem.right = rcItem.left + si.cx;
540 rcItem.bottom = rcItem.top + si.cy;
541 m_pSkinPrev->DrawByIndex(pRT, rcItem, dwPrevState);
553 SStringT szYearMonth;
560 szYearMonth.Format(_T(
"%04d年"),
m_iYear);
566 szYearMonth.Format(_T(
"%04d-%04d"), decadeFirstYear, decadeFirstYear + 9);
572 szYearMonth.Format(_T(
"%04d-%04d"), centuryFirstYear, centuryFirstYear + 99);
575 pRT->
DrawText(szYearMonth, -1, rc, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
580 DWORD dwNextState = 0;
589 int n = (nHeight - si.cy) / 2;
592 rcItem.left = rcItem.right - si.cx;
594 rcItem.bottom = rcItem.top + si.cy;
596 m_pSkinNext->DrawByIndex(pRT, rcItem, dwNextState);
602 if (rect.IsRectEmpty())
608 int nWid = rect.Width() / 7;
610 rcItem.right = rcItem.left + nWid;
613 for (
int i = 0; i < 7; i++)
617 pRT->
DrawText(
m_strWeek[i].GetText(FALSE),
m_strWeek[i].GetText(FALSE).GetLength(), rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
618 rcItem.OffsetRect(nWid, 0);
625 if (nItem < 0 || nItem >= 42)
631 CPoint ptRound(1, 1);
635 if (0 != dayInfo.nType)
665 sDay.Format(_T(
"%d"), dayInfo.iDay);
666 pRT->
DrawText(sDay, -1, rcDay, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
673 if (rcDay.IsRectEmpty())
687 pRT->
DrawText(szToday, -1,
m_rcToday, DT_SINGLELINE | DT_VCENTER | DT_CENTER | DT_CALCRECT);
696 if (nItem < 0 || nItem >= 42)
702 CPoint ptRound(1, 1);
706 if (0 != monthYearInfo.nType)
740 sDay.Format(_T(
"%d月"), monthYearInfo.iMonthOrYear);
741 pRT->
DrawText(sDay, sDay.GetLength(), rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
745 sDay.Format(_T(
"%d"), monthYearInfo.iMonthOrYear);
746 pRT->
DrawText(sDay, sDay.GetLength(), rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
750 sDay.Format(_T(
"%d-\n%d"), monthYearInfo.iMonthOrYear, monthYearInfo.iMonthOrYear + 9);
751 CRect rcTxt = rcItem;
752 pRT->
DrawText(sDay, sDay.GetLength(), rcTxt, DT_CALCRECT);
753 rcItem.DeflateRect((rcItem.Width() - rcTxt.Width()) / 2, (rcItem.Height() - rcTxt.Height()) / 2);
754 pRT->
DrawText(sDay, sDay.GetLength(), rcItem, 0);
780 CRect rcYear(rcClient);
784 CRect rcWeek(rcClient);
785 rcWeek.top = rcYear.bottom;
795 for (
int i = 0; i < 42; ++i)
801 CRect rcToday(rcClient);
815 CRect rcYear(rcClient);
819 CRect rcMonth(rcClient);
820 rcMonth.top = rcYear.bottom;
824 int iWidth = rcMonth.Width() / 4;
825 int iHeight = rcMonth.Height() / 3;
826 for (
int i = 0; i < 12; i++)
828 rcItem.left = rcMonth.left + i % 4 * iWidth;
829 rcItem.top = rcMonth.top + i / 4 * iHeight;
830 rcItem.right = rcItem.left + iWidth;
831 rcItem.bottom = rcItem.top + iHeight;
836 CRect rcToday(rcClient);
837 rcToday.top = rcMonth.bottom;
845 __baseCls::OnLButtonDown(nFlags, point);
909 EventCalendarExChanged evt(
this);
910 evt.nBtnType = nItem;
911 evt.iNewDay = dayInfo.iDay;
912 if (dayInfo.nType < 0)
922 else if (dayInfo.nType > 0)
942 int tmpYear = 0, tmpMonth = 0;
947 tmpMonth = info.iMonthOrYear;
951 tmpYear = info.iMonthOrYear;
956 tmpYear = info.iMonthOrYear;
986 __baseCls::OnLButtonUp(nFlags, point);
1061 if ((pt.x -
m_rcDays.left) < nYearHei)
1063 else if ((
m_rcDays.right - pt.x) < nYearHei)
1081 int nRow = nDay_X / nWid;
1085 return nRow + (nDay_Y / nHei) * 7;
1093 if (pt.y < rcDays.top)
1096 if ((pt.x - rcDays.left) < nYearHei)
1098 else if ((rcDays.right - pt.x) < nYearHei)
1103 else if (pt.y > rcDays.bottom)
1111 int nDay_X = pt.x - rcDays.left;
1112 int nDay_Y = pt.y - rcDays.top;
1116 int nRow = nDay_X / nWid;
1120 return nRow + (nDay_Y / nHei) * 4;
1127 int nItemWidth =
m_rcDays.Width() / 7;
1128 int nItemHeight =
m_rcDays.Height() / 6;
1130 rcItem.left = (nItem % 7) * nItemWidth;
1131 rcItem.right = rcItem.left + nItemWidth;
1133 rcItem.top = (nItem / 7) * nItemHeight;
1134 rcItem.bottom = rcItem.top + nItemHeight;
1135 rcItem.OffsetRect(
m_rcDays.TopLeft());
#define SHOW_YEAR_CENTURY
static BOOL DateCheck(WORD wYear, WORD wMonth, WORD wDay)
Validates the year, month, and day.
static WORD GetDaysOfMonth(WORD wYear, WORD wMonth)
Returns the number of days in a specified month.
static BOOL GetDaysNumInYear(WORD wYear, WORD wMonth, WORD wDay, WORD &wDays)
Calculates the day number within the year (starting from 0, where 0 is January 1st).
static short GetDayOfWeek(WORD wYear, WORD wMonth, WORD wDay)
Returns the day of the week.
static SStringT FormatYear(WORD iYear)
Formats the year display.
static BOOL GetDateFromDays(WORD wYear, WORD wDays, WORD &wMonth, WORD &wDay)
Calculates the month and day from the day number within the year.
static SStringT FormatDay(WORD iDay)
Formats the day display.
static SStringT FormatMonth(WORD iMonth)
Formats the month display.
static BOOL IsLeapYear(WORD wYear, BOOL &bLeapYear)
Determines if a year is a leap year.
COLORREF m_crSelDayBack
Background color of the selected day.
void DrawToday(IRenderTarget *pRT, CRect &rcDay)
Draws the "Today" button.
void SetShowType(int showType)
Sets the calendar display type.
SAutoRefPtr< ISkinObj > m_pSkinNext
Skin for the next button.
void SetYearDecadeCentury()
Sets the year, decade, and century views.
int m_showTypeLbdown
Calendar display state when mouse button is down.
int m_nSelItem
Selected item.
SLayoutSize m_nFooterHeight
Height of the "Today" button.
SAutoRefPtr< ISkinObj > m_pSkinDay
Skin for the day.
void SetNextYearCentury()
Sets the next century.
SYSTEMTIME m_Today
Current date.
void SetNextYearDecade()
Sets the next decade.
void SetYearMonth(int iYear, int iMonth)
Sets the year and month.
int m_nHoverItem
Hover item.
SAutoRefPtr< ISkinObj > m_pSkinPrev
Skin for the previous button.
WORD m_iYear
Current year.
void Init()
Initializes the control.
CRect m_rcToday
"Today" button area.
int HitTest(const CPoint &pt)
Tests the mouse click position.
void SetNextYear()
Sets the next year.
void OnPaint(IRenderTarget *pRT)
Paints the control.
void DrawWeek(IRenderTarget *pRT, const CRect &rect)
Draws the week days.
void SetLastYear()
Sets the previous year.
void DrawYearMonth(IRenderTarget *pRT, const CRect &rect)
Draws the calendar header (year and month).
virtual HRESULT OnLanguageChanged()
Called when the language changes.
COLORREF m_crHoverText
Color of the hover text.
void OnPaintMonth(IRenderTarget *pRT)
Paints the month view.
BOOL SetDate(WORD iYear, WORD iMonth, WORD iDay, int nBtnType=-1, bool bNotify=false)
Sets the date.
void SetLastYearCentury()
Sets the previous century.
void OnPaintYearDecadeCentury(IRenderTarget *pRT)
Paints the year, decade, and century views.
void GetDate(WORD &iYear, WORD &iMonth, WORD &iDay)
Gets the current date.
int m_showType
Calendar display state.
void OnMouseMove(UINT nFlags, CPoint pt)
Handles mouse move event.
STrText m_strWeek[7]
Header text.
SCalendar()
Default constructor.
void SetLastMonth()
Sets the previous month.
SLayoutSize m_nYearMonthHeight
Height of the year and month.
void OnMouseLeave()
Handles mouse leave event.
void OnLButtonUp(UINT nFlags, CPoint point)
Handles left mouse button up event.
void OnLButtonDown(UINT nFlags, CPoint point)
Handles left mouse button down event.
void SetLastYearDecade()
Sets the previous decade.
wDayInfo m_arrDays[42]
Date information array.
COLORREF m_crSelText
Selected text color.
void SetNextMonth()
Sets the next month.
COLORREF m_crOtherDayText
Color of other days' text.
wMonthOrYearInfo m_arrMonthOrYear[12]
Month or year information array.
WORD GetMonth()
Gets the current month.
void GetItemRect(int nItem, CRect &rcItem)
Gets the item drawing area.
WORD m_iMonth
Current month.
SLayoutSize m_nWeekHeight
Height of the week.
BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
Handles mouse wheel event.
WORD GetDay()
Gets the current day.
SAutoRefPtr< ISkinObj > m_pSkinWeek
Skin for the week.
WORD GetYear()
Gets the current year.
void DrawYearDecadeCentury(IRenderTarget *pRT, const CRect &rect, int nItem)
Draws the year, decade, and century items.
void DrawDay(IRenderTarget *pRT, CRect &rcDay, int nItem)
Draws the day.
int OnCreate(LPVOID lp)
Called when the control is created.
void setMutedState(BOOL setting)
设置事件集的静音状态
Helper class for painting.
static STime GetCurrentTime()
获取当前时间
BOOL FireEvent(IEvtArgs *evt) OVERRIDE
Fires an event.
int GetScale() SCONST OVERRIDE
Retrieves the scale factor of the window.
int OnCreate(LPVOID)
Handles the creation of the window.
virtual CRect GetClientRect() const
Retrieves the client rectangle of the window.
virtual void BeforePaint(IRenderTarget *pRT, SPainter &painter)
Prepare rendering environment.
virtual void AfterPaint(IRenderTarget *pRT, SPainter &painter)
Restore rendering environment.
SEventSet * GetEventSet()
Retrieves the event set associated with the window.
void Invalidate() OVERRIDE
Invalidates the entire window.
const SwndStyle & GetStyle() const
Retrieves the style of the window.
Interface for rendering target objects.
HRESULT DrawText(LPCTSTR pszText, int cchLen, LPRECT pRc, UINT uFormat) PURE
Draw text within a rectangle.
HRESULT FillSolidRoundRect(LPCRECT pRect, POINT pt, COLORREF cr) PURE
Fill a rounded rectangle with a solid color.
HRESULT DrawRoundRect(LPCRECT pRect, POINT pt) PURE
Draw a rounded rectangle outline.
COLORREF GetTextColor() PURE
Retrieves the current text color.
COLORREF SetTextColor(COLORREF color) PURE
Sets the current text color.
Date information structure.
Month or year information structure.