CalendarFrm.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /***************************************************************************
  2. 致看到这些源代码的兄弟:
  3. 你好!
  4. 这本来是我为一个商业PDA产品开发的日历程序,最近移植于PC机上, 所以算法
  5. 和数据部分是用纯C++写的,不涉及MFC,所有的代码都是以短节省存储空间为主要目
  6. 的.
  7. 很高兴你对这些代码有兴趣,你可以随意复制和使用些代码,唯一有一点小小的
  8. 愿望:在你使用和复制给别人时,别忘注明这些代码作者:-)。程序代码也就罢了,后
  9. 面的数据可是我辛辛苦苦从万年历上找出来输进去的。
  10. 如果你有什么好的意见不妨Mail给我。
  11. wangfei@hanwang.com.cn
  12. wangfei@engineer.com.cn
  13. ****************************************************************************/
  14. // CalendarFrm.cpp : implementation file
  15. // Download by http://www.codefans.net
  16. #include "stdafx.h"
  17. #include "ylgl.h"
  18. #include "CalendarFrm.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. #define IDC_YEAREDIT 101
  25. #define IDC_CALENDARFRM 106
  26. #define IDC_CALENDAR 107
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CCalendarFrm
  29. CCalendarFrm::CCalendarFrm()
  30. {
  31. }
  32. CCalendarFrm::~CCalendarFrm()
  33. {
  34. }
  35. #define IDC_MONTHSELECT 102
  36. #define IDC_YEARSELECT 103
  37. #define IDC_BUTTODAY 105
  38. BEGIN_MESSAGE_MAP(CCalendarFrm, CWnd)
  39. //{{AFX_MSG_MAP(CCalendarFrm)
  40. ON_WM_PAINT()
  41. ON_WM_CREATE()
  42. //}}AFX_MSG_MAP
  43. ON_MESSAGE(UM_SELCHANGE, OnSelChange)
  44. END_MESSAGE_MAP()
  45. // ON_CBN_SELCHANGE(IDC_MONTHSELECT, OnMonthChange)
  46. // ON_EN_CHANGE(IDC_YEAREDIT, OnYearChange)
  47. // ON_BN_CLICKED(IDC_BUTTODAY, OnTodayClick)
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CCalendarFrm message handlers
  50. BOOL CCalendarFrm::Create(RECT &rect, CWnd * pParentWnd, UINT nID)
  51. {
  52. CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|CS_HREDRAW|CS_VREDRAW, 0, 0, 0);
  53. // rect.right = rect.left + 340;
  54. // rect.bottom = rect.top +214;
  55. //WS_EX_CLIENTEDGE|WS_EX_WINDOWEDGE
  56. //|WS_DLGFRAME|WS_THICKFRAME
  57. if(!CWnd::CreateEx( 0, szClassName, _T(""),WS_DLGFRAME|WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect, pParentWnd, nID, NULL))
  58. return FALSE;
  59. UpdateWindow();
  60. l_CreateDispFont();
  61. return TRUE;
  62. }
  63. void CCalendarFrm::l_PaintToday(CPaintDC &dc)
  64. {
  65. }
  66. void CCalendarFrm::l_PaintBorder(CPaintDC &dc)
  67. {
  68. }
  69. void CCalendarFrm::l_PaintChineseInfo(CPaintDC &dc)
  70. {
  71. }
  72. void CCalendarFrm::OnPaint()
  73. {
  74. CPaintDC dc(this); // device context for painting
  75. dc.SelectObject(GetDispFont());
  76. }
  77. int CCalendarFrm::OnCreate(LPCREATESTRUCT lpCreateStruct)
  78. {
  79. if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;
  80. //create calendar
  81. CRect rect;
  82. GetClientRect(&rect);
  83. /* rect.left = rect.CenterPoint().x - 64;
  84. rect.top += 2; */
  85. m_oCalendar.m_rect = m_rect;
  86. if(!m_oCalendar.Create(m_rect, this, IDC_CALENDAR))
  87. return -1;
  88. #define LEFT_X 2
  89. #define TOP_Y 2
  90. #define WIDTH 90
  91. #define HEIGHT 22
  92. //create select year spingbutton and buddy
  93. /* rect.SetRect(LEFT_X, TOP_Y, LEFT_X+WIDTH, TOP_Y+HEIGHT);
  94. if(!m_oYearEdit.Create(rect, this, IDC_YEAREDIT))
  95. return -1;
  96. m_oYearEdit.SetLimitText(4);
  97. rect.SetRect(79, 3, 98, 23);
  98. if(!m_oYearSelect.Create(UDS_NOTHOUSANDS|WS_CHILD|WS_TABSTOP|
  99. WS_VISIBLE|UDS_SETBUDDYINT|UDS_ALIGNRIGHT,
  100. rect, this, IDC_YEARSELECT))
  101. return -1;
  102. m_oYearSelect.SetBuddy(&m_oYearEdit);
  103. m_oYearSelect.SetRange(START_YEAR, END_YEAR);
  104. m_oYearSelect.SetPos(m_oCalendar.GetYear());*/
  105. //create select month combobox
  106. /* rect.SetRect(LEFT_X, TOP_Y+HEIGHT+ 2, LEFT_X + WIDTH, TOP_Y+ 8*HEIGHT + 2);
  107. if(!m_oMonthSelect.Create(WS_VSCROLL|CBS_DROPDOWNLIST|WS_CHILD|
  108. WS_VISIBLE|WS_TABSTOP,
  109. rect, this, IDC_MONTHSELECT))
  110. return -1;*/
  111. #undef LEFT_X
  112. #undef TOP_Y
  113. #undef WIDTH
  114. #undef HEIGHT
  115. //create today button
  116. /* rect.SetRect(255, 184, 328, 206);
  117. if(!m_obutToday.Create("今天", WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect, this, IDC_BUTTODAY))
  118. return -1;
  119. char text[10];
  120. for(int i=0; i<12; i++)
  121. {
  122. CCalendar::FormatMonth(i+1, text, FALSE);
  123. m_oMonthSelect.AddString(text);
  124. }
  125. m_oMonthSelect.SetCurSel(m_oCalendar.GetMonth()-1); */
  126. return 0;
  127. }
  128. #ifdef VC60
  129. void CCalendarFrm::OnSelChange(LONG iYearMonth, LONG iDay)
  130. {
  131. Invalidate(FALSE);
  132. }
  133. #else
  134. LRESULT CCalendarFrm::OnSelChange(WPARAM wp, LPARAM lp)
  135. {
  136. Invalidate(FALSE);
  137. return 0;
  138. }
  139. #endif
  140. void CCalendarFrm::DateChange2(LONG iYear2, LONG iMonth2, LONG iDay2)
  141. {
  142. WORD iYear, iDay, iMonth;
  143. m_oCalendar.GetDate(iYear, iMonth, iDay);
  144. if(iMonth != iMonth2 || iYear != iYear2 || iDay !=iDay2)
  145. {
  146. if(iDay2 > CCalendar::MonthDays(iYear2, iMonth2))
  147. iDay2 = CCalendar::MonthDays(iYear2, iMonth2);
  148. m_oCalendar.SetDate(iYear2, iMonth2, iDay2);
  149. Invalidate();
  150. }
  151. }
  152. void CCalendarFrm::DateChange(LONG iYear2, LONG iMonth2)
  153. {
  154. WORD iYear, iDay, iMonth;
  155. m_oCalendar.GetDate(iYear, iMonth, iDay);
  156. if(iMonth != iMonth2 || iYear != iYear2)
  157. {
  158. if(iDay > CCalendar::MonthDays(iYear2, iMonth2))
  159. iDay = CCalendar::MonthDays(iYear2, iMonth2);
  160. m_oCalendar.SetDate(iYear2, iMonth2, iDay);
  161. Invalidate();
  162. }
  163. }
  164. void CCalendarFrm::OnMonthChange()
  165. {
  166. /* WORD iYear, iDay, iMonth;
  167. m_oCalendar.GetDate(iYear, iMonth, iDay);
  168. iMonth = m_oMonthSelect.GetCurSel()+1;
  169. if(iMonth != m_oCalendar.GetMonth())
  170. {
  171. if(iDay > CCalendar::MonthDays(iYear, iMonth))
  172. iDay = CCalendar::MonthDays(iYear, iMonth);
  173. m_oCalendar.SetDate(iYear, iMonth, iDay);
  174. Invalidate();
  175. }*/
  176. }
  177. void CCalendarFrm::OnYearChange()
  178. {
  179. /* CString text;
  180. m_oYearEdit.GetWindowText(text);
  181. if(text.GetLength()<4)
  182. return;
  183. WORD iYear, iMonth, iDay;
  184. m_oCalendar.GetDate(iYear, iMonth, iDay);
  185. iYear = m_oYearSelect.GetPos();
  186. if(iYear != m_oCalendar.GetYear())
  187. {
  188. if(iDay > CCalendar::MonthDays(iYear, iMonth))
  189. iDay = CCalendar::MonthDays(iYear, iMonth);
  190. m_oCalendar.SetDate(iYear, iMonth, iDay);
  191. text.Format("%4d", iYear);
  192. m_oYearEdit.SetWindowText(text);
  193. Invalidate();
  194. }*/
  195. }
  196. void CCalendarFrm::OnTodayClick()
  197. {
  198. /* WORD iYear, iMonth, iDay;
  199. SYSTEMTIME systime;
  200. ::GetSystemTime(&systime);
  201. iYear = systime.wYear;
  202. iMonth = systime.wMonth;
  203. iDay = systime.wDay;
  204. if(iYear != m_oCalendar.GetYear() || iMonth != m_oCalendar.GetMonth()
  205. || iDay != m_oCalendar.GetDay())
  206. {
  207. SetDate(iYear, iMonth, iDay);
  208. m_oYearSelect.SetPos(iYear);
  209. m_oMonthSelect.SetCurSel(iMonth -1);
  210. Invalidate();
  211. }*/
  212. }
  213. // Jeff:创建显示的字体;
  214. void CCalendarFrm::l_CreateDispFont()
  215. {
  216. static LOGFONT logFont;
  217. memset(&logFont, 0 ,sizeof(logFont));
  218. #if 1// Jeff 这里设置的显示字体,只是星期一、二....、和号数;
  219. logFont.lfHeight = 16;
  220. logFont.lfWeight = FW_NORMAL;
  221. logFont.lfWidth = 0;
  222. logFont.lfCharSet = GB2312_CHARSET;
  223. logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  224. logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  225. logFont.lfQuality = PROOF_QUALITY;
  226. logFont.lfPitchAndFamily = VARIABLE_PITCH|FF_ROMAN;
  227. strcpy(logFont.lfFaceName, "Disp Font");
  228. #else
  229. logFont.lfHeight = 24;
  230. logFont.lfWeight = FW_NORMAL;
  231. logFont.lfWidth = 0;
  232. logFont.lfCharSet = GB2312_CHARSET;
  233. logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  234. logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  235. logFont.lfQuality = PROOF_QUALITY;
  236. logFont.lfPitchAndFamily = VARIABLE_PITCH|FF_ROMAN;
  237. strcpy(logFont.lfFaceName, "Disp Font");
  238. #endif
  239. m_oDispFont.CreateFontIndirect(&logFont);
  240. SetFont(&m_oDispFont, FALSE);
  241. }