博客园精华区 原文 http://www.cnblogs.com/sunsmile/archive/2013/03/04/ningmengxiu1.html
最近应领导要求,再搞一个基于安全的办公自动化系统,基于一些考虑,需要使用C/S架构实现,估考虑使用Duilib这一免费开源的利器,在业务功能实现上,办公自动化少不了日历控件的使用,在网上寻了半天,没有好的开源的日历控件,更多的是web版的日历控件,估决定参考web版的日历控件,使用duilib实现一个winform版的日历,在网上找到的比较炫的一个日历如下:
做web开发的朋友可以在这里找到此控件:
http://www.open-open.com/ajax/ajax20091109225817.htm
下面我们使用Duilib实现winform版的日历控件:
a、将其web版的资源文件,主要是图片,下载到本地;
b、编写duilib需要使用到的xml文件,Calendar.xml;
View Code
1 <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 2 <Window size="429,470" sizebox="4,4,6,6" caption="0,0,0,30"> 3 <VerticalLayout name="VerticalLayoutUI1" pos="205,186,265,216" width="60" height="30" bkimage="body.png" bordercolor="#009DABCE"> 4 <Container name="ContainerUI1" pos="0,0,429,50" width="429" height="50"> 5 <Button name="BTN_UP_MONTH" text="<<" float="true" pos="18,14,57,36" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" /> 6 <Button name="BTN_UP_YEAR" text="<<" float="true" pos="276,15,313,40" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" /> 7 <Button name="BTN_DOWN_YEAR" text=">>" float="true" pos="324,15,361,40" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" /> 8 <Combo name="CMB_MONTH" float="true" pos="75,15,175,37" droptype="droplist" width="100" height="22" textpadding="4,1,1,1" normalimage="file='combo.png' source='0,0,100,22' corner='2,2,20,2'" hotimage="file='combo.png' source='0,22,100,44' corner='2,2,22,2'" pushedimage="file='combo.png' source='0,44,100,66' corner='2,2,22,2'"> 9 <ListLabelElement text="1月" selected="true" height="23"/>10 <ListLabelElement text="2月" height="23" />11 <ListLabelElement text="3月" height="23" />12 <ListLabelElement text="4月" height="23" />13 <ListLabelElement text="5月" height="23" />14 <ListLabelElement text="6月" height="23" />15 <ListLabelElement text="7月" height="23" />16 <ListLabelElement text="8月" height="23" />17 <ListLabelElement text="9月" height="23" />18 <ListLabelElement text="10月" height="23" />19 <ListLabelElement text="11月" height="23" />20 <ListLabelElement text="12月" height="23" />21 </Combo>22 <Edit name="TXT_YEAR" text="2013" float="true" pos="202,14,262,36" bkcolor="#FFFFFFFF" textpadding="4,3,4,3" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" />23 </Container>24 <Container name="ContainerUI2" pos="0,44,429,69" width="429" height="25">25 <Button name="ButtonUI40" text="星期日" float="true" pos="1,1,62,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />26 <Button name="ButtonUI41" text="星期一" float="true" pos="62,1,123,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />27 <Button name="ButtonUI42" text="星期二" float="true" pos="123,1,184,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />28 <Button name="ButtonUI43" text="星期五" float="true" pos="306,1,367,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />29 <Button name="ButtonUI44" text="星期四" float="true" pos="245,1,306,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />30 <Button name="ButtonUI45" text="星期三" float="true" pos="184,1,245,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />31 <Button name="ButtonUI46" text="星期六" float="true" pos="367,1,428,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />32 </Container>33 <Container name="ContainerUI3" pos="0,75,429,295" width="429" height="266" bordercolor="#00CEAB9D">34 <Button name="ButtonUI5" float="true" pos="1,1,62,46" bkimage="calpad.jpg" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />35 <Button name="ButtonUI6" float="true" pos="62,1,123,46" bkimage="calpad.jpg" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />36 <Button name="ButtonUI7" float="true" pos="123,1,184,46" bkimage="calpad.jpg" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />37 <Button name="ButtonUI8" float="true" pos="184,1,245,46" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />38 <Button name="ButtonUI9" float="true" pos="245,1,306,46" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />39 <Button name="ButtonUI10" float="true" pos="306,1,367,46" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />40 <Button name="ButtonUI11" float="true" pos="367,1,428,46" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />41 <Button name="ButtonUI12" float="true" pos="1,45,62,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />42 <Button name="ButtonUI13" float="true" pos="62,45,123,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />43 <Button name="ButtonUI14" float="true" pos="123,45,184,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />44 <Button name="ButtonUI15" float="true" pos="184,45,245,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />45 <Button name="ButtonUI28" float="true" pos="245,45,306,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />46 <Button name="ButtonUI29" float="true" pos="306,45,367,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />47 <Button name="ButtonUI30" float="true" pos="367,45,428,90" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />48 <Button name="ButtonUI16" float="true" pos="1,89,62,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />49 <Button name="ButtonUI17" float="true" pos="62,89,123,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />50 <Button name="ButtonUI18" float="true" pos="123,89,184,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />51 <Button name="ButtonUI19" float="true" pos="184,89,245,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />52 <Button name="ButtonUI34" float="true" pos="245,89,306,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />53 <Button name="ButtonUI35" float="true" pos="306,89,367,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />54 <Button name="ButtonUI36" float="true" pos="367,89,428,134" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />55 <Button name="ButtonUI20" float="true" pos="1,133,62,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />56 <Button name="ButtonUI21" float="true" pos="62,133,123,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />57 <Button name="ButtonUI22" float="true" pos="123,133,184,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />58 <Button name="ButtonUI23" float="true" pos="184,133,245,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />59 <Button name="ButtonUI31" float="true" pos="245,133,306,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />60 <Button name="ButtonUI32" float="true" pos="306,133,367,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />61 <Button name="ButtonUI33" float="true" pos="367,133,428,178" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />62 <Button name="ButtonUI24" float="true" pos="1,177,62,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />63 <Button name="ButtonUI25" float="true" pos="62,177,123,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />64 <Button name="ButtonUI26" float="true" pos="123,177,184,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />65 <Button name="ButtonUI27" float="true" pos="184,177,245,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />66 <Button name="ButtonUI37" float="true" pos="245,177,306,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />67 <Button name="ButtonUI38" float="true" pos="306,177,367,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />68 <Button name="ButtonUI39" float="true" pos="367,177,428,222" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />69 70 <Button name="ButtonUI24" float="true" pos="1,221,62,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />71 <Button name="ButtonUI25" float="true" pos="62,221,123,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />72 <Button name="ButtonUI26" float="true" pos="123,221,184,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />73 <Button name="ButtonUI27" float="true" pos="184,221,245,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />74 <Button name="ButtonUI37" float="true" pos="245,221,306,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />75 <Button name="ButtonUI38" float="true" pos="306,221,367,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />76 <Button name="ButtonUI39" float="true" pos="367,221,428,266" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />77 </Container>78 <Container name="ContainerUI4" pos="0,379,429,360" width="429" height="25">79 <Button name="ButtonUI47" text="星期日" float="true" pos="1,1,62,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />80 <Button name="ButtonUI48" text="星期一" float="true" pos="62,1,123,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />81 <Button name="ButtonUI49" text="星期二" float="true" pos="123,1,184,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />82 <Button name="ButtonUI50" text="星期三" float="true" pos="184,1,245,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />83 <Button name="ButtonUI51" text="星期四" float="true" pos="245,1,306,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />84 <Button name="ButtonUI52" text="星期五" float="true" pos="306,1,367,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />85 <Button name="ButtonUI53" text="星期六" float="true" pos="367,1,428,26" bkimage="file='cells.png' source='0,0,81,81'" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='cells.png' source='0,0,81,81'" hotimage="file='cells.png' source='0,81,81,162'" />86 </Container>87 </VerticalLayout>88 </Window>
c、代码实现如下:
View Code
1 // App.cpp : Defines the entry point for the application. 2 // 3 4 #include "stdafx.h" 5 //#include "systdate.h" 6 #include <ATLComTime.h> 7 8 class CFrameWindowWnd : public CWindowWnd, public INotifyUI 9 { 10 public: 11 CFrameWindowWnd() { }; 12 LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); }; 13 //UINT GetClassStyle() const { return UI_CLASSSTYLE_FRAME | CS_DBLCLKS; }; 14 UINT GetClassStyle() const { return CS_DBLCLKS; }; 15 void OnFinalMessage(HWND /*hWnd*/) { delete this; }; 16 17 void Init() {} 18 19 void OnPrepare() 20 { 21 ::GetLocalTime(&m_sysTime); 22 DrawCalendar(m_sysTime); 23 } 24 25 void Notify(TNotifyUI& msg) 26 { 27 if( msg.sType == _T("windowinit") ) OnPrepare(); 28 else if( msg.sType == _T("return") ) 29 { 30 m_sysTime.wYear = SetTxtYear(0); 31 m_sysTime.wMonth = GetCmbMonth(); 32 DrawCalendar(m_sysTime); 33 } 34 else if( msg.sType == _T("click") ) 35 { 36 CControlUI* pOne = static_cast<CControlUI*>(m_pm.FindControl(msg.ptMouse)); 37 if (_tcsicmp(pOne->GetClass(), _T("ButtonUI")) == 0) 38 { 39 //上一月 40 if (_tcsicmp(pOne->GetName(), _T("BTN_UP_MONTH")) == 0) 41 { 42 m_sysTime.wMonth = m_sysTime.wMonth-1 == 0 ? 12 : m_sysTime.wMonth-1; 43 DrawCalendar(m_sysTime); 44 } 45 //上一年 46 else if (_tcsicmp(pOne->GetName(), _T("BTN_UP_YEAR")) == 0) 47 { 48 m_sysTime.wYear = SetTxtYear(-1); 49 m_sysTime.wMonth = GetCmbMonth(); 50 DrawCalendar(m_sysTime); 51 } 52 //下一年 53 else if (_tcsicmp(pOne->GetName(), _T("BTN_DOWN_YEAR")) == 0) 54 { 55 m_sysTime.wYear = SetTxtYear(1); 56 m_sysTime.wMonth = GetCmbMonth(); 57 DrawCalendar(m_sysTime); 58 } 59 } 60 } 61 else if( msg.sType == _T("itemselect") ) { 62 if (_tcsicmp(msg.pSender->GetName(), _T("CMB_MONTH")) == 0) 63 { 64 m_sysTime.wMonth = GetCmbMonth(); 65 DrawCalendar(m_sysTime); 66 } 67 } 68 } 69 70 LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) 71 { 72 if( uMsg == WM_CREATE ) { 73 m_pm.Init(m_hWnd); 74 CDialogBuilder builder; 75 CControlUI* pRoot = builder.Create(_T("Calendar1.xml"), (UINT)0, NULL, &m_pm); 76 ASSERT(pRoot && "Failed to parse XML"); 77 m_pm.AttachDialog(pRoot); 78 m_pm.AddNotifier(this); 79 Init(); 80 return 0; 81 } 82 else if( uMsg == WM_DESTROY ) { 83 ::PostQuitMessage(0L); 84 } 85 else if( uMsg == WM_ERASEBKGND ) { 86 return 1; 87 } 88 LRESULT lRes = 0; 89 if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes; 90 return CWindowWnd::HandleMessage(uMsg, wParam, lParam); 91 } 92 93 //绘制日历 94 void DrawCalendar(SYSTEMTIME m_sysTime) 95 { 96 int iDay = 0; 97 char cDay[3] = {0}; 98 //int iStartDay = m_sysTime.wDayOfWeek; 99 m_sysTime.wDay = 1;100 int iStartDay = GetDayOfWeek(m_sysTime);101 //上月天数102 int iLastMonthStartDays = 31 - iStartDay;103 if(m_sysTime.wMonth>1)104 iLastMonthStartDays = GetMonthDays(m_sysTime.wYear,m_sysTime.wMonth - 1) - iStartDay;105 //本月天数106 int iMonthDays = GetMonthDays(m_sysTime.wYear,m_sysTime.wMonth);107 //下月天数108 int iNextMonthDays = 0;109 110 CControlUI* pButtonUI;111 CContainerUI* pControl = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUI3")));112 for(int i = 0;i < 42;i++)113 {114 pButtonUI = m_pm.FindSubControlByClass(pControl,_T("ButtonUI"),i);115 //上月116 if(i<iStartDay)117 {118 iLastMonthStartDays ++;119 sprintf(cDay,"%d",iLastMonthStartDays);120 pButtonUI->SetText(cDay);121 DrawBtnBackImage(pButtonUI,false);122 }123 else if(i>iStartDay-1&&iDay<iMonthDays)124 {125 iDay++;126 sprintf(cDay,"%d",iDay);127 pButtonUI->SetText(cDay);128 DrawBtnBackImage(pButtonUI,true);129 } 130 else 131 {132 iNextMonthDays ++;133 sprintf(cDay,"%d",iNextMonthDays);134 pButtonUI->SetText(cDay);135 DrawBtnBackImage(pButtonUI,false);136 }137 }138 }139 140 void DrawBtnBackImage(CControlUI* pButtonUI,bool isEnable)141 {142 if(isEnable)143 {144 pButtonUI->SetEnabled(true);145 pButtonUI->ApplyAttributeList(146 _T("normalimage=\"file='cells.png' source='0,0,81,81'\" ")\147 _T("hotimage=\"file='cells.png' source='0,81,81,162'\" "));148 }149 else150 {151 pButtonUI->SetBkImage(_T("calpad.jpg"));152 pButtonUI->ApplyAttributeList(153 _T("normalimage=\"file='calpad.jpg'\" ")\154 _T("hotimage=\"file='calpad.jpg'\" "));155 }156 }157 158 int GetMonthDays(int iY,int iM)159 {160 int iTotalDay = 31; 161 if(iM == 2) 162 {163 //闰年可以被4或者400整除 但是不能被100整除164 if(iY % 4 ==0 && iY % 100 !=0 || iY % 400 ==0) 165 iTotalDay= 29;166 else167 iTotalDay=28;168 }169 else if(iM==4 || iM==6 || iM==9||iM==11)170 iTotalDay = 30;171 return iTotalDay;172 }173 174 int GetDayOfWeek(SYSTEMTIME m_sysTime)175 {176 cTime.SetDate(m_sysTime.wYear,m_sysTime.wMonth,m_sysTime.wDay);177 return cTime.GetDayOfWeek()-1;178 }179 180 int GetCmbMonth()181 {182 CComboUI* pCmb = static_cast<CComboUI*>(m_pm.FindControl(_T("CMB_MONTH")));183 CDuiString cCurMonth = pCmb->GetText();184 return atoi(cCurMonth.Left(cCurMonth.GetLength()-1));185 }186 187 int SetTxtYear(int iY)188 {189 CEditUI* pTxt = static_cast<CEditUI*>(m_pm.FindControl(_T("TXT_YEAR")));190 CDuiString cCurYear = pTxt->GetText();191 int iCurYear= 0;192 iCurYear = atoi(cCurYear) + iY;193 cCurYear.Format("%d",iCurYear);194 pTxt->SetText(cCurYear);195 return iCurYear;196 }197 198 199 public:200 CPaintManagerUI m_pm;201 SYSTEMTIME m_sysTime;202 COleDateTime cTime;203 };204 205 206 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)207 {208 CPaintManagerUI::SetInstance(hInstance);209 CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());210 211 HRESULT Hr = ::CoInitialize(NULL);212 if( FAILED(Hr) ) return 0;213 214 CFrameWindowWnd* pFrame = new CFrameWindowWnd();215 if( pFrame == NULL ) return 0;216 // pFrame->Create(NULL, _T("日历控件测试"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);217 pFrame->Create(NULL, _T("日历控件测试"), UI_WNDSTYLE_DIALOG, WS_EX_STATICEDGE | WS_EX_APPWINDOW, 0, 0, 600, 320); //禁止双击放大218 pFrame->CenterWindow();219 pFrame->ShowWindow(true);220 CPaintManagerUI::MessageLoop();221 222 ::CoUninitialize();223 return 0;224 }
d、运行看一下效果吧!
暂时没有实现阴历的显示,待项目开发完毕,有时间实现完给大家开源。