/*************************************************************************** 致看到这些源代码的兄弟: 你好! 这本来是我为一个商业PDA产品开发的日历程序,最近移植于PC机上, 所以算法 和数据部分是用纯C++写的,不涉及MFC,所有的代码都是以短节省存储空间为主要目 的. 很高兴你对这些代码有兴趣,你可以随意复制和使用些代码,唯一有一点小小的 愿望:在你使用和复制给别人时,别忘注明这些代码作者:-)。程序代码也就罢了,后 面的数据可是我辛辛苦苦从万年历上找出来输进去的。 如果你有什么好的意见不妨Mail给我。 wangfei@hanwang.com.cn 或 wangfei@engineer.com.cn ****************************************************************************/ // CalendarFrm.cpp : implementation file // Download by http://www.codefans.net #include "stdafx.h" #include "ylgl.h" #include "CalendarFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define IDC_YEAREDIT 101 #define IDC_CALENDARFRM 106 #define IDC_CALENDAR 107 ///////////////////////////////////////////////////////////////////////////// // CCalendarFrm CCalendarFrm::CCalendarFrm() { } CCalendarFrm::~CCalendarFrm() { } #define IDC_MONTHSELECT 102 #define IDC_YEARSELECT 103 #define IDC_BUTTODAY 105 BEGIN_MESSAGE_MAP(CCalendarFrm, CWnd) //{{AFX_MSG_MAP(CCalendarFrm) ON_WM_PAINT() ON_WM_CREATE() //}}AFX_MSG_MAP ON_MESSAGE(UM_SELCHANGE, OnSelChange) END_MESSAGE_MAP() // ON_CBN_SELCHANGE(IDC_MONTHSELECT, OnMonthChange) // ON_EN_CHANGE(IDC_YEAREDIT, OnYearChange) // ON_BN_CLICKED(IDC_BUTTODAY, OnTodayClick) ///////////////////////////////////////////////////////////////////////////// // CCalendarFrm message handlers BOOL CCalendarFrm::Create(RECT &rect, CWnd * pParentWnd, UINT nID) { CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|CS_HREDRAW|CS_VREDRAW, 0, 0, 0); // rect.right = rect.left + 340; // rect.bottom = rect.top +214; //WS_EX_CLIENTEDGE|WS_EX_WINDOWEDGE //|WS_DLGFRAME|WS_THICKFRAME if(!CWnd::CreateEx( 0, szClassName, _T(""),WS_DLGFRAME|WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect, pParentWnd, nID, NULL)) return FALSE; UpdateWindow(); l_CreateDispFont(); return TRUE; } void CCalendarFrm::l_PaintToday(CPaintDC &dc) { } void CCalendarFrm::l_PaintBorder(CPaintDC &dc) { } void CCalendarFrm::l_PaintChineseInfo(CPaintDC &dc) { } void CCalendarFrm::OnPaint() { CPaintDC dc(this); // device context for painting dc.SelectObject(GetDispFont()); } int CCalendarFrm::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; //create calendar CRect rect; GetClientRect(&rect); /* rect.left = rect.CenterPoint().x - 64; rect.top += 2; */ m_oCalendar.m_rect = m_rect; if(!m_oCalendar.Create(m_rect, this, IDC_CALENDAR)) return -1; #define LEFT_X 2 #define TOP_Y 2 #define WIDTH 90 #define HEIGHT 22 //create select year spingbutton and buddy /* rect.SetRect(LEFT_X, TOP_Y, LEFT_X+WIDTH, TOP_Y+HEIGHT); if(!m_oYearEdit.Create(rect, this, IDC_YEAREDIT)) return -1; m_oYearEdit.SetLimitText(4); rect.SetRect(79, 3, 98, 23); if(!m_oYearSelect.Create(UDS_NOTHOUSANDS|WS_CHILD|WS_TABSTOP| WS_VISIBLE|UDS_SETBUDDYINT|UDS_ALIGNRIGHT, rect, this, IDC_YEARSELECT)) return -1; m_oYearSelect.SetBuddy(&m_oYearEdit); m_oYearSelect.SetRange(START_YEAR, END_YEAR); m_oYearSelect.SetPos(m_oCalendar.GetYear());*/ //create select month combobox /* rect.SetRect(LEFT_X, TOP_Y+HEIGHT+ 2, LEFT_X + WIDTH, TOP_Y+ 8*HEIGHT + 2); if(!m_oMonthSelect.Create(WS_VSCROLL|CBS_DROPDOWNLIST|WS_CHILD| WS_VISIBLE|WS_TABSTOP, rect, this, IDC_MONTHSELECT)) return -1;*/ #undef LEFT_X #undef TOP_Y #undef WIDTH #undef HEIGHT //create today button /* rect.SetRect(255, 184, 328, 206); if(!m_obutToday.Create("今天", WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect, this, IDC_BUTTODAY)) return -1; char text[10]; for(int i=0; i<12; i++) { CCalendar::FormatMonth(i+1, text, FALSE); m_oMonthSelect.AddString(text); } m_oMonthSelect.SetCurSel(m_oCalendar.GetMonth()-1); */ return 0; } #ifdef VC60 void CCalendarFrm::OnSelChange(LONG iYearMonth, LONG iDay) { Invalidate(FALSE); } #else LRESULT CCalendarFrm::OnSelChange(WPARAM wp, LPARAM lp) { Invalidate(FALSE); return 0; } #endif void CCalendarFrm::DateChange2(LONG iYear2, LONG iMonth2, LONG iDay2) { WORD iYear, iDay, iMonth; m_oCalendar.GetDate(iYear, iMonth, iDay); if(iMonth != iMonth2 || iYear != iYear2 || iDay !=iDay2) { if(iDay2 > CCalendar::MonthDays(iYear2, iMonth2)) iDay2 = CCalendar::MonthDays(iYear2, iMonth2); m_oCalendar.SetDate(iYear2, iMonth2, iDay2); Invalidate(); } } void CCalendarFrm::DateChange(LONG iYear2, LONG iMonth2) { WORD iYear, iDay, iMonth; m_oCalendar.GetDate(iYear, iMonth, iDay); if(iMonth != iMonth2 || iYear != iYear2) { if(iDay > CCalendar::MonthDays(iYear2, iMonth2)) iDay = CCalendar::MonthDays(iYear2, iMonth2); m_oCalendar.SetDate(iYear2, iMonth2, iDay); Invalidate(); } } void CCalendarFrm::OnMonthChange() { /* WORD iYear, iDay, iMonth; m_oCalendar.GetDate(iYear, iMonth, iDay); iMonth = m_oMonthSelect.GetCurSel()+1; if(iMonth != m_oCalendar.GetMonth()) { if(iDay > CCalendar::MonthDays(iYear, iMonth)) iDay = CCalendar::MonthDays(iYear, iMonth); m_oCalendar.SetDate(iYear, iMonth, iDay); Invalidate(); }*/ } void CCalendarFrm::OnYearChange() { /* CString text; m_oYearEdit.GetWindowText(text); if(text.GetLength()<4) return; WORD iYear, iMonth, iDay; m_oCalendar.GetDate(iYear, iMonth, iDay); iYear = m_oYearSelect.GetPos(); if(iYear != m_oCalendar.GetYear()) { if(iDay > CCalendar::MonthDays(iYear, iMonth)) iDay = CCalendar::MonthDays(iYear, iMonth); m_oCalendar.SetDate(iYear, iMonth, iDay); text.Format("%4d", iYear); m_oYearEdit.SetWindowText(text); Invalidate(); }*/ } void CCalendarFrm::OnTodayClick() { /* WORD iYear, iMonth, iDay; SYSTEMTIME systime; ::GetSystemTime(&systime); iYear = systime.wYear; iMonth = systime.wMonth; iDay = systime.wDay; if(iYear != m_oCalendar.GetYear() || iMonth != m_oCalendar.GetMonth() || iDay != m_oCalendar.GetDay()) { SetDate(iYear, iMonth, iDay); m_oYearSelect.SetPos(iYear); m_oMonthSelect.SetCurSel(iMonth -1); Invalidate(); }*/ } // Jeff:创建显示的字体; void CCalendarFrm::l_CreateDispFont() { static LOGFONT logFont; memset(&logFont, 0 ,sizeof(logFont)); #if 1// Jeff 这里设置的显示字体,只是星期一、二....、和号数; logFont.lfHeight = 16; logFont.lfWeight = FW_NORMAL; logFont.lfWidth = 0; logFont.lfCharSet = GB2312_CHARSET; logFont.lfOutPrecision = OUT_DEFAULT_PRECIS; logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS; logFont.lfQuality = PROOF_QUALITY; logFont.lfPitchAndFamily = VARIABLE_PITCH|FF_ROMAN; strcpy(logFont.lfFaceName, "Disp Font"); #else logFont.lfHeight = 24; logFont.lfWeight = FW_NORMAL; logFont.lfWidth = 0; logFont.lfCharSet = GB2312_CHARSET; logFont.lfOutPrecision = OUT_DEFAULT_PRECIS; logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS; logFont.lfQuality = PROOF_QUALITY; logFont.lfPitchAndFamily = VARIABLE_PITCH|FF_ROMAN; strcpy(logFont.lfFaceName, "Disp Font"); #endif m_oDispFont.CreateFontIndirect(&logFont); SetFont(&m_oDispFont, FALSE); }