UIDateTime.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "StdAfx.h"
  2. #include "UIDateTime.h"
  3. namespace DuiLib
  4. {
  5. //CDateTimeUI::m_nDTUpdateFlag
  6. #define DT_NONE 0
  7. #define DT_UPDATE 1
  8. #define DT_DELETE 2
  9. #define DT_KEEP 3
  10. class CDateTimeWnd : public CWindowWnd
  11. {
  12. public:
  13. CDateTimeWnd();
  14. void Init(CDateTimeUI* pOwner);
  15. RECT CalPos();
  16. LPCTSTR GetWindowClassName() const;
  17. LPCTSTR GetSuperClassName() const;
  18. void OnFinalMessage(HWND hWnd);
  19. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  20. protected:
  21. CDateTimeUI* m_pOwner;
  22. HBRUSH m_hBkBrush;
  23. bool m_bInit;
  24. bool m_bDropOpen;
  25. SYSTEMTIME m_oldSysTime;
  26. };
  27. CDateTimeWnd::CDateTimeWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false), m_bDropOpen(false)
  28. {
  29. }
  30. void CDateTimeWnd::Init(CDateTimeUI* pOwner)
  31. {
  32. m_pOwner = pOwner;
  33. m_pOwner->m_nDTUpdateFlag = DT_NONE;
  34. if (m_hWnd == NULL)
  35. {
  36. RECT rcPos = CalPos();
  37. UINT uStyle = WS_CHILD;
  38. Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
  39. SetWindowFont(m_hWnd, m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->hFont, TRUE);
  40. }
  41. // 使用系统当前时间
  42. if (m_pOwner->GetText().IsEmpty()) {
  43. ::GetLocalTime(&m_pOwner->m_sysTime);
  44. }
  45. // 显示格式
  46. CDuiString sTimeFormat = m_pOwner->GetTimeFormat();
  47. ::SendMessage(m_hWnd, DTM_SETFORMAT, 0, LPARAM(sTimeFormat.GetData()));
  48. memcpy(&m_oldSysTime, &m_pOwner->m_sysTime, sizeof(SYSTEMTIME));
  49. ::SendMessage(m_hWnd, DTM_SETSYSTEMTIME, 0, (LPARAM)&m_pOwner->m_sysTime);
  50. ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  51. ::SetFocus(m_hWnd);
  52. m_bInit = true;
  53. }
  54. RECT CDateTimeWnd::CalPos()
  55. {
  56. CDuiRect rcPos = m_pOwner->GetPos();
  57. //CDuiRect rcPadding = m_pOwner->GetTextPadding();
  58. //rcPos.left += rcPadding.left;
  59. //rcPos.top += rcPadding.top;
  60. //rcPos.right -= rcPadding.right;
  61. //rcPos.bottom -= rcPadding.bottom;
  62. CControlUI* pParent = m_pOwner;
  63. RECT rcParent;
  64. while( pParent = pParent->GetParent() ) {
  65. if( !pParent->IsVisible() ) {
  66. rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0;
  67. break;
  68. }
  69. rcParent = pParent->GetClientPos();
  70. if( !::IntersectRect(&rcPos, &rcPos, &rcParent) ) {
  71. rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0;
  72. break;
  73. }
  74. }
  75. return rcPos;
  76. }
  77. LPCTSTR CDateTimeWnd::GetWindowClassName() const
  78. {
  79. return _T("DateTimeWnd");
  80. }
  81. LPCTSTR CDateTimeWnd::GetSuperClassName() const
  82. {
  83. return DATETIMEPICK_CLASS;
  84. }
  85. void CDateTimeWnd::OnFinalMessage(HWND hWnd)
  86. {
  87. if( m_hBkBrush != NULL ) ::DeleteObject(m_hBkBrush);
  88. //m_pOwner->GetManager()->RemoveNativeWindow(hWnd);
  89. m_pOwner->m_pWindow = NULL;
  90. delete this;
  91. }
  92. LRESULT CDateTimeWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  93. {
  94. LRESULT lRes = 0;
  95. BOOL bHandled = TRUE;
  96. if (uMsg == WM_KEYDOWN && wParam == VK_ESCAPE) {
  97. memcpy(&m_pOwner->m_sysTime, &m_oldSysTime, sizeof(SYSTEMTIME));
  98. m_pOwner->m_nDTUpdateFlag = DT_UPDATE;
  99. m_pOwner->UpdateText();
  100. PostMessage(WM_CLOSE);
  101. return lRes;
  102. }
  103. else if(uMsg == OCM_NOTIFY) {
  104. NMHDR* pHeader=(NMHDR*)lParam;
  105. if(pHeader != NULL && pHeader->hwndFrom == m_hWnd) {
  106. if(pHeader->code == DTN_DATETIMECHANGE) {
  107. LPNMDATETIMECHANGE lpChage=(LPNMDATETIMECHANGE)lParam;
  108. ::SendMessage(m_hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&m_pOwner->m_sysTime);
  109. m_pOwner->m_nDTUpdateFlag = DT_UPDATE;
  110. m_pOwner->UpdateText();
  111. }
  112. else if(pHeader->code == DTN_DROPDOWN) {
  113. m_bDropOpen = true;
  114. }
  115. else if(pHeader->code == DTN_CLOSEUP) {
  116. ::SendMessage(m_hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&m_pOwner->m_sysTime);
  117. m_pOwner->m_nDTUpdateFlag = DT_UPDATE;
  118. m_pOwner->UpdateText();
  119. PostMessage(WM_CLOSE);
  120. m_bDropOpen = false;
  121. }
  122. }
  123. bHandled = FALSE;
  124. }
  125. else if(uMsg == WM_KILLFOCUS)
  126. {
  127. if(!m_bDropOpen) {
  128. PostMessage(WM_CLOSE);
  129. }
  130. bHandled = FALSE;
  131. }
  132. else bHandled = FALSE;
  133. if( !bHandled ) return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  134. return lRes;
  135. }
  136. //////////////////////////////////////////////////////////////////////////
  137. //
  138. IMPLEMENT_DUICONTROL(CDateTimeUI)
  139. CDateTimeUI::CDateTimeUI()
  140. {
  141. ::GetLocalTime(&m_sysTime);
  142. m_bReadOnly = false;
  143. m_sTimeFormat = _T("yyyy-MM-dd HH:mm:ss");
  144. m_pWindow = NULL;
  145. m_nDTUpdateFlag=DT_UPDATE;
  146. UpdateText();
  147. m_nDTUpdateFlag = DT_NONE;
  148. }
  149. LPCTSTR CDateTimeUI::GetClass() const
  150. {
  151. return _T("DateTimeUI");
  152. }
  153. LPVOID CDateTimeUI::GetInterface(LPCTSTR pstrName)
  154. {
  155. if( _tcscmp(pstrName, DUI_CTR_DATETIME) == 0 ) return static_cast<CDateTimeUI*>(this);
  156. return CLabelUI::GetInterface(pstrName);
  157. }
  158. SYSTEMTIME& CDateTimeUI::GetTime()
  159. {
  160. return m_sysTime;
  161. }
  162. void CDateTimeUI::SetTime(SYSTEMTIME* pst)
  163. {
  164. m_sysTime = *pst;
  165. Invalidate();
  166. m_nDTUpdateFlag = DT_UPDATE;
  167. UpdateText();
  168. m_nDTUpdateFlag = DT_NONE;
  169. }
  170. void CDateTimeUI::SetReadOny(bool bReadOnly)
  171. {
  172. m_bReadOnly = bReadOnly;
  173. Invalidate();
  174. }
  175. bool CDateTimeUI::IsReadOnly() const
  176. {
  177. return m_bReadOnly;
  178. }
  179. void CDateTimeUI::SetTimeFormat(LPCTSTR pstrFormat)
  180. {
  181. m_sTimeFormat = pstrFormat;
  182. Invalidate();
  183. m_nDTUpdateFlag = DT_UPDATE;
  184. UpdateText();
  185. m_nDTUpdateFlag = DT_NONE;
  186. }
  187. CDuiString CDateTimeUI::GetTimeFormat() const
  188. {
  189. return m_sTimeFormat;
  190. }
  191. void CDateTimeUI::UpdateText()
  192. {
  193. if (m_nDTUpdateFlag == DT_DELETE) {
  194. SetText(_T(""));
  195. }
  196. else if (m_nDTUpdateFlag == DT_UPDATE) {
  197. CDuiString sText;
  198. if(m_sTimeFormat.CompareNoCase(_T("yyyy-MM-dd")) == 0) {
  199. sText.SmallFormat(_T("%4d-%02d-%02d"), m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay);
  200. }
  201. else if(m_sTimeFormat.CompareNoCase(_T("HH:mm:ss")) == 0) {
  202. sText.SmallFormat(_T("%02d:%02d:%02d"), m_sysTime.wHour, m_sysTime.wMinute, m_sysTime.wSecond);
  203. }
  204. else {
  205. sText.SmallFormat(_T("%4d-%02d-%02d %02d:%02d:%02d"), m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay, m_sysTime.wHour, m_sysTime.wMinute, m_sysTime.wSecond);
  206. }
  207. SetText(sText);
  208. }
  209. }
  210. void CDateTimeUI::DoEvent(TEventUI& event)
  211. {
  212. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  213. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  214. else CLabelUI::DoEvent(event);
  215. return;
  216. }
  217. if( event.Type == UIEVENT_WINDOWSIZE )
  218. {
  219. if( m_pWindow != NULL ) m_pManager->SetFocusNeeded(this);
  220. }
  221. if( event.Type == UIEVENT_SCROLLWHEEL )
  222. {
  223. if( m_pWindow != NULL ) return;
  224. }
  225. if( event.Type == UIEVENT_SETFOCUS && IsEnabled() )
  226. {
  227. if( m_pWindow ) return;
  228. m_pWindow = new CDateTimeWnd();
  229. ASSERT(m_pWindow);
  230. m_pWindow->Init(this);
  231. m_pWindow->ShowWindow();
  232. }
  233. if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() )
  234. {
  235. Invalidate();
  236. }
  237. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
  238. {
  239. if( IsEnabled() ) {
  240. GetManager()->ReleaseCapture();
  241. if( IsFocused() && m_pWindow == NULL )
  242. {
  243. m_pWindow = new CDateTimeWnd();
  244. ASSERT(m_pWindow);
  245. }
  246. if( m_pWindow != NULL )
  247. {
  248. m_pWindow->Init(this);
  249. m_pWindow->ShowWindow();
  250. }
  251. }
  252. return;
  253. }
  254. if( event.Type == UIEVENT_MOUSEMOVE )
  255. {
  256. return;
  257. }
  258. if( event.Type == UIEVENT_BUTTONUP )
  259. {
  260. return;
  261. }
  262. if( event.Type == UIEVENT_CONTEXTMENU )
  263. {
  264. return;
  265. }
  266. if( event.Type == UIEVENT_MOUSEENTER )
  267. {
  268. return;
  269. }
  270. if( event.Type == UIEVENT_MOUSELEAVE )
  271. {
  272. return;
  273. }
  274. CLabelUI::DoEvent(event);
  275. }
  276. void CDateTimeUI::SetPos(RECT rc, bool bNeedInvalidate)
  277. {
  278. CControlUI::SetPos(rc, bNeedInvalidate);
  279. if( m_pWindow != NULL ) {
  280. RECT rcPos = m_pWindow->CalPos();
  281. ::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
  282. rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE);
  283. }
  284. }
  285. void CDateTimeUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  286. {
  287. if(lstrcmpi(pstrName, _T("timeformat")) == 0) SetTimeFormat(pstrValue);
  288. else if(lstrcmpi(pstrName, _T("readonly")) == 0) SetReadOny(lstrcmpi(pstrValue, _T("true")) == 0);
  289. else return CLabelUI::SetAttribute(pstrName, pstrValue);
  290. }
  291. }