VC++ 中 的 日 期 盒 通 用 类 的 实 现 ---- 本 人 在Visual C++ 的 应 用 实 践 中, 感 到 许 多 应 用 程 序 都 需 要 用 到 日 期 的 输 入, 为 了 提 高 编 程 的 速 度 和 效 率, 减 少 繁 重 的 不 必 要 的 重 复 编 程 工 作, 编 了 一 个 通 用 的 日 期 盒 类, 这 样 在 以 后 有 用 到 日 期 输 入 的 地 方 直 接 引 用 就 可 以 了, 大 大 提 高 了 工 作 效 率, 并 且 很 美 观 实 用。 ---- 以 下 是 日 期 盒 通 用 类 实 现 的 演 示。 ---- 以 下 是 日 期 盒 通 用 类 的 源 程 序。 // DateLBox.h : class CDateLBox : public CWnd { public: CDateLBox(); public: CTime m_time[2]; CRect m_rectPrev, m_rectNext, m_rect[31]; private: CComboBox* m_pComboBox; public: void SetComboBox(CComboBox* combo) {m_pComboBox=combo;} public: void SetupDate(); // ClassWizard //{{AFX_VIRTUAL(CDateLBox) //}}AFX_VIRTUAL public: virtual ~CDateLBox(); protected: //{{AFX_MSG(CDateLBox) afx_msg void OnPaint(); afx_msg void OnLButtonDown (UINT nFlags, CPoint point); afx_msg void OnWindowPosChanged (WINDOWPOS FAR* lpwndpos); afx_msg void OnLButtonDblClk (UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; // DateLBox.cpp : #include "stdafx.h" #include "DateBox.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif void CDateLBox::SetupDate() { m_time[0]=CTime(0); m_time[1]=CTime::GetCurrentTime(); CString str; m_pComboBox- >GetWindowText(str); int nYear, nMonth, nDay; if (sscanf((LPCSTR)str, "%d/%d/%d", &nYear, &nMonth, &nDay)==3) { if (nYear >=1971&&nYear<=2037&&nMonth>=1&& nMonth<=12&&nDay>=1&&nDay<=31) { CTime t(nYear, nMonth, nDay, 0, 0, 0); if (t.GetYear()="=nYear&&t.GetMonth()" { m_time[0]="t;m_time[1]=t;" } } } } CDateLBox::CDateLBox() { m_time[0]="CTime(0);" m_time[1]="CTime::GetCurrentTime();}" CDateLBox::~CDateLBox(){} BEGIN_MESSAGE_MAP(CDateLBox, CWnd) //{{AFX_MSG_MAP(CDateLBox) ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_WINDOWPOSCHANGED() ON_WM_LBUTTONDBLCLK() //}}AFX_MSG_MAP END_MESSAGE_MAP() // CDateLBox void CDateLBox::OnPaint() { CPaintDC dc(this); // dc.SetBkMode(TRANSPARENT); CRect rect; GetClientRect(rect); CFont* pFont="dc.SelectObject" (GetFocus()->GetFont()); dc.DrawText(m_time[1].Format("%Y%#m"), CRect(rect.left,rect.top,rect.right, rect.top+rect.Height()/8*1), DT_SINGLELINE| DT_CENTER|DT_VCENTER); m_rectPrev.SetRect(rect.left+rect.Width() /7*0,rect.top,rect.left+rect. Width()/7*1,rect.top+rect.Height()/8*1); dc.MoveTo(m_rectPrev.left+m_rectPrev.Width() /2-2,m_rectPrev.top+m_rectPrev.Height()/2-1); dc.LineTo(m_rectPrev.left+m_rectPrev.Width() /2-2, m_rectPrev.top+m_rectPrev.Height()/2); dc.MoveTo(m_rectPrev.left+m_rectPrev.Width() /2-1, m_rectPrev.top+m_rectPrev.Height()/2-2); dc.LineTo(m_rectPrev.left+m_rectPrev.Width() /2-1, m_rectPrev.top+m_rectPrev.Height()/2+1); dc.MoveTo(m_rectPrev.left+m_rectPrev.Width() /2, m_rectPrev.top+m_rectPrev.Height()/2-3); dc.LineTo(m_rectPrev.left+m_rectPrev.Width() /2, m_rectPrev.top+m_rectPrev.Height()/2+2); dc.MoveTo(m_rectPrev.left+m_rectPrev.Width() /2+1, m_rectPrev.top+m_rectPrev.Height()/2-4); dc.LineTo(m_rectPrev.left+m_rectPrev.Width() /2+1, m_rectPrev.top+m_rectPrev.Height()/2+3); m_rectNext.SetRect(rect.left+rect.Width() /7*6,rect.top,rect.left+rect.Width() /7*7,rect.top+rect.Height()/8*1); dc.MoveTo(m_rectNext.left+m_rectNext.Width() /2+1, m_rectNext.top+m_rectNext.Height()/2-1); dc.LineTo(m_rectNext.left+m_rectNext.Width() /2+1, m_rectNext.top+m_rectNext.Height()/2); dc.MoveTo(m_rectNext.left+m_rectNext.Width() /2, m_rectNext.top+m_rectNext.Height()/2-2); dc.LineTo(m_rectNext.left+m_rectNext.Width() /2, m_rectNext.top+m_rectNext.Height()/2+1); dc.MoveTo(m_rectNext.left+m_rectNext.Width() /2-1, m_rectNext.top+m_rectNext.Height()/2-3); dc.LineTo(m_rectNext.left+m_rectNext.Width() /2-1,m_rectNext.top+m_rectNext.Height()/2+2); dc.MoveTo(m_rectNext.left+m_rectNext.Width() /2-2, m_rectNext.top+m_rectNext.Height()/2-4); dc.LineTo(m_rectNext.left+m_rectNext.Width() /2-2,m_rectNext.top+m_rectNext.Height()/2+3); dc.SetTextColor(RGB(255,0,0)); dc.DrawText(" 日",-1,CRect(rect.left+rect.Width() /7*0,rect.top+rect.Height()/8*1, rect.left+rect.Width()/7*1,rect.top+rect.Height()/8*2), DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.SetTextColor(RGB(0,0,0)); dc.DrawText(" 一", -1, CRect(rect.left+rect.Width() /7*1, rect.top+rect.Height()/8*1, rect.left+rect.Width() /7*2,rect.top+rect.Height()/8*2), DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.DrawText(" 二", -1, CRect(rect.left+rect.Width() /7*2, rect.top+rect.Height()/8*1, rect.left+rect.Width() /7*3,rect.top+rect.Height()/8*2), DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.DrawText(" 三", -1, CRect(rect.left+rect.Width() /7*3, rect.top+rect.Height()/8*1, rect.left+rect.Width() /7*4,rect.top+rect.Height()/8*2), DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.DrawText(" 四", -1, CRect(rect.left+rect.Width() /7*4, rect.top+rect.Height()/8*1, rect.left+rect.Width() /7*5,rect.top+rect.Height()/8*2), DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.DrawText(" 五", -1, CRect(rect.left+rect.Width() /7*5, rect.top+rect.Height()/8*1, rect.left+rect.Width() /7*6,rect.top+rect.Height()/8*2) ,DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.SetTextColor(RGB(0,0,255)); dc.DrawText(" 六", -1, CRect(rect.left+rect.Width() /7*6, rect.top+rect.Height()/8*1, rect.left+rect.Width() /7*7,rect.top+rect.Height()/8*2), DT_SINGLELINE|DT_CENTER|DT_VCENTER); CSize size(rect.Width()/7, rect.Height()/8); CPoint point(0, rect.top+size.cy*2); for(Ctime t(m_time[1].GetYear(),m_time [1].GetMonth(),1,0,0, 0); t.GetMonth()==m_time[1].GetMonth(); t+=CTimeSpan(1, 0, 0, 0)) { point.x=rect.left+size.cx*(t.GetDayOfWeek()-1); m_rect[t.GetDay()-1]=CRect(point, size); if (t==m_time[0]) { dc.FillSolidRect(m_rect[t.GetDay()-1], ::GetSysColor(COLOR_HIGHLIGHT)); dc.SetTextColor(::GetSysColor (COLOR_HIGHLIGHTTEXT)); } else { CDateBox* combo=(CDateBox*)m_pComboBox; COLORREF ref=combo- >GetDateColor(t); dc.SetTextColor(ref); } dc.DrawText(t.Format("%#d"),m_rect[t.GetDay()-1], DT_SINGLELINE|DT_CENTER|DT_VCENTER); if (t.GetDayOfWeek()==7) {point.y+=size.cy;} } dc.SelectObject(pFont); } void CDateLBox::OnLButtonDown(UINT nFlags, CPoint point) { if (m_rectPrev.PtInRect(point)) { CTime t; if(m_time[1].GetMonth()==1) t=CTime(m_time[1].GetYear()-1,12, 1, 0, 0, 0); else t=CTime(m_time[1].GetYear(), m_time [1].GetMonth()-1, 1, 0, 0, 0); if (t.GetYear() >= 1971&&t.GetYear()<=2037) {m_time[1]="t;" Invalidate();} return; } if (m_rectNext.PtInRect(point)) { CTime t; if(m_time[1].GetMonth()="=12)" t="CTime(m_time[1].GetYear()+1,1," 1, 0, 0, 0); else t="CTime(m_time[1].GetYear()," m_time [1].GetMonth()+1, 1, 0, 0, 0); if (t.GetYear()>=1971&&t.GetYear()<=2037) { m_time[1]="t;Invalidate();}" return; } for (CTime t(m_time[1].GetYear(), m_time[1].GetMonth(),1,0,0, 0); t.GetMonth()="=m_time[1]." GetMonth();t+="CTimeSpan(1," 0, 0, 0)) { if (m_rect[t.GetDay()-1].PtInRect(point)) { m_pComboBox->SetFocus(); m_pComboBox- >SetWindowText (t.Format("%Y/%#m/%#d")); m_pComboBox- >SetEditSel(0, -1); SetupDate(); Invalidate(); m_pComboBox- >ShowDropDown(FALSE); CWnd* parent=m_pComboBox- >GetParent(); if (parent) { parent- >PostMessage(WM_COMMAND, MAKELONG(m_pComboBox- > GetDlgCtrlID(),CBN_SELCHANGE), (LPARAM)m_pComboBox- >m_hWnd); } return; } } CWnd::OnLButtonDown(nFlags, point); } void CDateLBox::OnWindowPosChanged (WINDOWPOS FAR* lpwndpos) { CWnd::OnWindowPosChanged(lpwndpos); SetupDate(); } void CDateLBox::OnLButtonDblClk (UINT nFlags, CPoint point) { OnLButtonDown(nFlags, point); } // DateBox.h : #include "DateLBox.h" // CDateBox class CDateBox : public CComboBox { public: CDateBox(); public: BOOL m_bSubclass; CDateLBox m_dlb; time_t m_time; public: operator time_t(); void SetupDate(CTime t); virtual COLORREF GetDateColor(CTime t) { return RGB(0,0,0); } // ClassWizard //{{AFX_VIRTUAL(CDateBox) //}}AFX_VIRTUAL public: virtual ~CDateBox(); protected: //{{AFX_MSG(CDateBox) afx_msg HBRUSH OnCtlColor (CDC* pDC, CWnd* pWnd, UINT nCtlColor); afx_msg void OnShowWindow (BOOL bShow, UINT nStatus); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; // DateBox.cpp : #include "stdafx.h" #include "DateBox.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // CDateBox CDateBox::CDateBox() { m_bSubclass=FALSE; } CDateBox::~CDateBox(){} BEGIN_MESSAGE_MAP(CDateBox, CComboBox) //{{AFX_MSG_MAP(CDateBox) ON_WM_CTLCOLOR() ON_WM_SHOWWINDOW() //}}AFX_MSG_MAP END_MESSAGE_MAP() // CDateBox HBRUSH CDateBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CComboBox::OnCtlColor (pDC, pWnd, nCtlColor); if (!m_bSubclass&&nCtlColor== CTLCOLOR_LISTBOX) { m_bSubclass=m_dlb.SubclassWindow (pWnd- >m_hWnd); m_dlb.SetComboBox(this); m_dlb.SetupDate(); m_dlb.Invalidate(); } return hbr; } CDateBox::operator time_t() { CString str; GetWindowText(str); int nYear, nMonth, nDay; if (sscanf((LPCSTR)str, "%d/%d/%d", &nYear, &nMonth, &nDay)==3) { if(nYear >=1971&&nYear< =2037&&nMonth >=1&& nMonth< =12&&nDay >=1&&nDay< =31) { CTime t(nYear, nMonth, nDay, 0, 0, 0); if (t.GetYear()==nYear&&t.GetMonth() ==nMonth&&t.GetDay()==nDay) { return(t.GetTime()); } } } return(0); } void CDateBox::SetupDate(CTime t) { CString tim; tim.Format("%04d/%02d/%02d", t.GetYear(), t.GetMonth(), t.GetDay()); SetWindowText(tim); if (IsWindow(m_dlb.m_hWnd)) { m_dlb.SetupDate(); } } void CDateBox::OnShowWindow(BOOL bShow, UINT nStatus) { CComboBox::OnShowWindow(bShow, nStatus); }