DigitalClock.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /********************************************************************************
  2. File: DigitalClock.h"
  3. Author: Mt.Hu (hmt-abc@163.com)
  4. Date: 2008/4/28
  5. Note: Please include the comment if you use the file in your project
  6. ********************************************************************************/
  7. // DigitalClock.cpp : implementation file
  8. //
  9. #include "stdafx.h"
  10. #include "DigitalClock.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CDigitalClock
  18. CDigitalClock::CDigitalClock()
  19. {
  20. m_crBk = ::GetSysColor(COLOR_BTNFACE);
  21. m_crText = ::GetSysColor(COLOR_BTNTEXT);
  22. m_nAlign = 1;
  23. m_nWidth = 0;
  24. m_nHour = 0;
  25. m_nMinute = 0;
  26. m_nSecond = 0;
  27. m_nSpace = 5;
  28. m_nXmargin = 10;
  29. m_nYmargin = 4;
  30. }
  31. CDigitalClock::~CDigitalClock()
  32. {
  33. }
  34. BEGIN_MESSAGE_MAP(CDigitalClock, CStatic)
  35. //{{AFX_MSG_MAP(CDigitalClock)
  36. ON_WM_PAINT()
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CDigitalClock message handlers
  41. void CDigitalClock::DrawSection1(int nLeft)
  42. {
  43. if (m_memDC.m_hDC!=NULL)
  44. {
  45. CPoint point[4];
  46. point[0].x=nLeft+(int)(0.1*m_nWidth);
  47. point[0].y=m_nYmargin;
  48. point[1].x=nLeft+(int)(0.9*m_nWidth);
  49. point[1].y=m_nYmargin;
  50. point[2].x=nLeft+(int)(0.7*m_nWidth);
  51. point[2].y=(int)(0.2*m_nWidth)+m_nYmargin;
  52. point[3].x=nLeft+(int)(0.3*m_nWidth);
  53. point[3].y=(int)(0.2*m_nWidth)+m_nYmargin;
  54. CBrush br(m_crText);
  55. CRgn rgn;
  56. rgn.CreatePolygonRgn(point,4,ALTERNATE);
  57. m_memDC.FillRgn(&rgn,&br);
  58. br.DeleteObject();
  59. rgn.DeleteObject();
  60. m_memDC.MoveTo(point[0]);
  61. m_memDC.LineTo(point[1]);
  62. m_memDC.MoveTo(point[1]);
  63. m_memDC.LineTo(point[2]);
  64. m_memDC.MoveTo(point[2]);
  65. m_memDC.LineTo(point[3]);
  66. m_memDC.MoveTo(point[3]);
  67. m_memDC.LineTo(point[0]);
  68. }
  69. }
  70. void CDigitalClock::DrawSection2(int nLeft)
  71. {
  72. if (m_memDC.m_hDC!=NULL)
  73. {
  74. CPoint point[5];
  75. point[0].x=nLeft+m_nWidth;
  76. point[0].y=(int)(0.1*m_nWidth)+m_nYmargin;
  77. point[1].x=nLeft+m_nWidth;
  78. point[1].y=(int)(0.8*m_nWidth)+m_nYmargin;
  79. point[2].x=nLeft+(int)(0.9*m_nWidth);
  80. point[2].y=(int)(0.9*m_nWidth)+m_nYmargin;
  81. point[3].x=nLeft+(int)(0.8*m_nWidth);
  82. point[3].y=(int)(0.8*m_nWidth)+m_nYmargin;
  83. point[4].x=nLeft+(int)(0.8*m_nWidth);
  84. point[4].y=(int)(0.3*m_nWidth)+m_nYmargin;
  85. CBrush br(m_crText);
  86. CRgn rgn;
  87. rgn.CreatePolygonRgn(point,5,ALTERNATE);
  88. m_memDC.FillRgn(&rgn,&br);
  89. br.DeleteObject();
  90. rgn.DeleteObject();
  91. m_memDC.MoveTo(point[0]);
  92. m_memDC.LineTo(point[1]);
  93. m_memDC.MoveTo(point[1]);
  94. m_memDC.LineTo(point[2]);
  95. m_memDC.MoveTo(point[2]);
  96. m_memDC.LineTo(point[3]);
  97. m_memDC.MoveTo(point[3]);
  98. m_memDC.LineTo(point[4]);
  99. m_memDC.MoveTo(point[4]);
  100. m_memDC.LineTo(point[0]);
  101. }
  102. }
  103. void CDigitalClock::DrawSection3(int nLeft)
  104. {
  105. if (m_memDC.m_hDC!=NULL)
  106. {
  107. CPoint point[5];
  108. point[0].x=nLeft+m_nWidth;
  109. point[0].y=(int)(1.1*m_nWidth)+m_nYmargin;
  110. point[1].x=nLeft+m_nWidth;
  111. point[1].y=(int)(1.8*m_nWidth)+m_nYmargin;
  112. point[2].x=nLeft+(int)(0.8*m_nWidth);
  113. point[2].y=(int)(1.6*m_nWidth)+m_nYmargin;
  114. point[3].x=nLeft+(int)(0.8*m_nWidth);
  115. point[3].y=(int)(1.1*m_nWidth)+m_nYmargin;
  116. point[4].x=nLeft+(int)(0.9*m_nWidth);
  117. point[4].y=m_nWidth+m_nYmargin;
  118. CBrush br(m_crText);
  119. CRgn rgn;
  120. rgn.CreatePolygonRgn(point,5,ALTERNATE);
  121. m_memDC.FillRgn(&rgn,&br);
  122. br.DeleteObject();
  123. rgn.DeleteObject();
  124. m_memDC.MoveTo(point[0]);
  125. m_memDC.LineTo(point[1]);
  126. m_memDC.MoveTo(point[1]);
  127. m_memDC.LineTo(point[2]);
  128. m_memDC.MoveTo(point[2]);
  129. m_memDC.LineTo(point[3]);
  130. m_memDC.MoveTo(point[3]);
  131. m_memDC.LineTo(point[4]);
  132. m_memDC.MoveTo(point[4]);
  133. m_memDC.LineTo(point[0]);
  134. }
  135. }
  136. void CDigitalClock::DrawSection4(int nLeft)
  137. {
  138. if (m_memDC.m_hDC!=NULL)
  139. {
  140. CPoint point[4];
  141. point[0].x=nLeft+(int)(0.1*m_nWidth);
  142. point[0].y=(int)(1.9*m_nWidth)+m_nYmargin;
  143. point[1].x=nLeft+(int)(0.9*m_nWidth);
  144. point[1].y=(int)(1.9*m_nWidth)+m_nYmargin;
  145. point[2].x=nLeft+(int)(0.7*m_nWidth);
  146. point[2].y=(int)(1.7*m_nWidth)+m_nYmargin;
  147. point[3].x=nLeft+(int)(0.3*m_nWidth);
  148. point[3].y=(int)(1.7*m_nWidth)+m_nYmargin;
  149. CBrush br(m_crText);
  150. CRgn rgn;
  151. rgn.CreatePolygonRgn(point,4,ALTERNATE);
  152. m_memDC.FillRgn(&rgn,&br);
  153. br.DeleteObject();
  154. rgn.DeleteObject();
  155. m_memDC.MoveTo(point[0]);
  156. m_memDC.LineTo(point[1]);
  157. m_memDC.MoveTo(point[1]);
  158. m_memDC.LineTo(point[2]);
  159. m_memDC.MoveTo(point[2]);
  160. m_memDC.LineTo(point[3]);
  161. m_memDC.MoveTo(point[3]);
  162. m_memDC.LineTo(point[0]);
  163. }
  164. }
  165. void CDigitalClock::DrawSection5(int nLeft)
  166. {
  167. if (m_memDC.m_hDC!=NULL)
  168. {
  169. CPoint point[5];
  170. point[0].x=nLeft;
  171. point[0].y=(int)(1.1*m_nWidth)+m_nYmargin;
  172. point[1].x=nLeft;
  173. point[1].y=(int)(1.8*m_nWidth)+m_nYmargin;
  174. point[2].x=nLeft+(int)(0.2*m_nWidth);
  175. point[2].y=(int)(1.6*m_nWidth)+m_nYmargin;
  176. point[3].x=nLeft+(int)(0.2*m_nWidth);
  177. point[3].y=(int)(1.1*m_nWidth)+m_nYmargin;
  178. point[4].x=nLeft+(int)(0.1*m_nWidth);
  179. point[4].y=m_nWidth+m_nYmargin;
  180. CBrush br(m_crText);
  181. CRgn rgn;
  182. rgn.CreatePolygonRgn(point,5,ALTERNATE);
  183. m_memDC.FillRgn(&rgn,&br);
  184. br.DeleteObject();
  185. rgn.DeleteObject();
  186. m_memDC.MoveTo(point[0]);
  187. m_memDC.LineTo(point[1]);
  188. m_memDC.MoveTo(point[1]);
  189. m_memDC.LineTo(point[2]);
  190. m_memDC.MoveTo(point[2]);
  191. m_memDC.LineTo(point[3]);
  192. m_memDC.MoveTo(point[3]);
  193. m_memDC.LineTo(point[4]);
  194. m_memDC.MoveTo(point[4]);
  195. m_memDC.LineTo(point[0]);
  196. }
  197. }
  198. void CDigitalClock::DrawSection6(int nLeft)
  199. {
  200. if (m_memDC.m_hDC!=NULL)
  201. {
  202. CPoint point[5];
  203. point[0].x=nLeft;
  204. point[0].y=(int)(0.1*m_nWidth)+m_nYmargin;
  205. point[1].x=nLeft;
  206. point[1].y=(int)(0.8*m_nWidth)+m_nYmargin;
  207. point[2].x=nLeft+(int)(0.1*m_nWidth);
  208. point[2].y=(int)(0.9*m_nWidth)+m_nYmargin;
  209. point[3].x=nLeft+(int)(0.2*m_nWidth);
  210. point[3].y=(int)(0.8*m_nWidth)+m_nYmargin;
  211. point[4].x=nLeft+(int)(0.2*m_nWidth);
  212. point[4].y=(int)(0.3*m_nWidth)+m_nYmargin;
  213. CBrush br(m_crText);
  214. CRgn rgn;
  215. rgn.CreatePolygonRgn(point,5,ALTERNATE);
  216. m_memDC.FillRgn(&rgn,&br);
  217. br.DeleteObject();
  218. rgn.DeleteObject();
  219. m_memDC.MoveTo(point[0]);
  220. m_memDC.LineTo(point[1]);
  221. m_memDC.MoveTo(point[1]);
  222. m_memDC.LineTo(point[2]);
  223. m_memDC.MoveTo(point[2]);
  224. m_memDC.LineTo(point[3]);
  225. m_memDC.MoveTo(point[3]);
  226. m_memDC.LineTo(point[4]);
  227. m_memDC.MoveTo(point[4]);
  228. m_memDC.LineTo(point[0]);
  229. }
  230. }
  231. void CDigitalClock::DrawSection7(int nLeft)
  232. {
  233. if (m_memDC.m_hDC!=NULL)
  234. {
  235. CPoint point[6];
  236. point[0].x=nLeft+(int)(0.3*m_nWidth);
  237. point[0].y=(int)(0.9*m_nWidth)+m_nYmargin;
  238. point[1].x=nLeft+(int)(0.7*m_nWidth);
  239. point[1].y=(int)(0.9*m_nWidth)+m_nYmargin;
  240. point[2].x=nLeft+(int)(0.8*m_nWidth);
  241. point[2].y=m_nWidth+m_nYmargin;
  242. point[3].x=nLeft+(int)(0.7*m_nWidth);
  243. point[3].y=(int)(1.1*m_nWidth)+m_nYmargin;
  244. point[4].x=nLeft+(int)(0.3*m_nWidth);
  245. point[4].y=(int)(1.1*m_nWidth)+m_nYmargin;
  246. point[5].x=nLeft+(int)(0.2*m_nWidth);
  247. point[5].y=m_nWidth+m_nYmargin;
  248. CBrush br(m_crText);
  249. CRgn rgn;
  250. rgn.CreatePolygonRgn(point,6,ALTERNATE);
  251. m_memDC.FillRgn(&rgn,&br);
  252. br.DeleteObject();
  253. rgn.DeleteObject();
  254. m_memDC.MoveTo(point[0]);
  255. m_memDC.LineTo(point[1]);
  256. m_memDC.MoveTo(point[1]);
  257. m_memDC.LineTo(point[2]);
  258. m_memDC.MoveTo(point[2]);
  259. m_memDC.LineTo(point[3]);
  260. m_memDC.MoveTo(point[3]);
  261. m_memDC.LineTo(point[4]);
  262. m_memDC.MoveTo(point[4]);
  263. m_memDC.LineTo(point[5]);
  264. m_memDC.MoveTo(point[5]);
  265. m_memDC.LineTo(point[0]);
  266. }
  267. }
  268. void CDigitalClock::Draw2Dot(int nLeft)
  269. {
  270. if (m_memDC.m_hDC!=NULL)
  271. {
  272. CBrush br(m_crText);
  273. CRect rect;
  274. rect.SetRect(nLeft+(int)(0.3*m_nWidth),(int)(0.4*m_nWidth)+m_nYmargin,
  275. nLeft+(int)(0.6*m_nWidth),(int)(0.7*m_nWidth)+m_nYmargin);
  276. m_memDC.Ellipse(rect);
  277. CRgn rgn1;
  278. rgn1.CreateEllipticRgn(rect.left,rect.top,rect.right,rect.bottom);
  279. m_memDC.FillRgn(&rgn1,&br);
  280. rect.OffsetRect(0,(int)(0.8*m_nWidth)+m_nYmargin);
  281. m_memDC.Ellipse(rect);
  282. CRgn rgn2;
  283. rgn2.CreateEllipticRgn(rect.left,rect.top,rect.right,rect.bottom);
  284. m_memDC.FillRgn(&rgn2,&br);
  285. br.DeleteObject();
  286. rgn1.DeleteObject();
  287. rgn2.DeleteObject();
  288. }
  289. }
  290. void CDigitalClock::SetBkColor(COLORREF crBk)
  291. {
  292. m_crBk = crBk;
  293. DrawTimer();
  294. }
  295. void CDigitalClock::SetTextColor(COLORREF crText)
  296. {
  297. m_crText = crText;
  298. DrawTimer();
  299. }
  300. void CDigitalClock::SetAlign(int nAlign)
  301. {
  302. m_nAlign = nAlign;
  303. DrawTimer();
  304. }
  305. COLORREF CDigitalClock::GetBkColor()
  306. {
  307. return m_crBk;
  308. }
  309. COLORREF CDigitalClock::GetTextColor()
  310. {
  311. return m_crText;
  312. }
  313. int CDigitalClock::GetAlign()
  314. {
  315. return m_nAlign;
  316. }
  317. void CDigitalClock::SetClock(int nHour, int nMinute, int nSecond)
  318. {
  319. m_nHour = nHour;
  320. m_nMinute = nMinute;
  321. m_nSecond = nSecond;
  322. DrawTimer();
  323. }
  324. void CDigitalClock::DrawHour()
  325. {
  326. int nLeft=m_nXmargin;
  327. if (m_nHour<10)
  328. {
  329. DrawSingleNumber(0,nLeft);
  330. nLeft+=m_nWidth+m_nSpace;
  331. DrawSingleNumber(m_nHour,nLeft);
  332. }
  333. else
  334. {
  335. TCHAR *c=new TCHAR[10];
  336. _itoa(m_nHour,c,10);
  337. int num1=c[0]-48;
  338. int num2=c[1]-48;
  339. DrawSingleNumber(num1,nLeft);
  340. nLeft+=m_nWidth+m_nSpace;
  341. DrawSingleNumber(num2,nLeft);
  342. delete []c;
  343. }
  344. nLeft+=m_nWidth;
  345. Draw2Dot(nLeft);
  346. }
  347. void CDigitalClock::DrawMinute()
  348. {
  349. int nLeft=m_nXmargin+3*m_nWidth+m_nSpace;
  350. if (m_nMinute<10)
  351. {
  352. DrawSingleNumber(0,nLeft);
  353. nLeft+=m_nWidth+m_nSpace;
  354. DrawSingleNumber(m_nMinute,nLeft);
  355. }
  356. else
  357. {
  358. TCHAR c[10]={0};
  359. _itoa(m_nMinute,c,10);
  360. int num1=c[0]-48;
  361. int num2=c[1]-48;
  362. DrawSingleNumber(num1,nLeft);
  363. nLeft+=m_nWidth+m_nSpace;
  364. DrawSingleNumber(num2,nLeft);
  365. }
  366. nLeft+=m_nWidth;
  367. Draw2Dot(nLeft);
  368. }
  369. void CDigitalClock::DrawSecond()
  370. {
  371. int nLeft=m_nXmargin+6*m_nWidth+2*m_nSpace;
  372. if (m_nSecond<10)
  373. {
  374. DrawSingleNumber(0,nLeft);
  375. nLeft+=(int)(1.4*m_nWidth);
  376. DrawSingleNumber(m_nSecond,nLeft);
  377. }
  378. else
  379. {
  380. TCHAR *c=new TCHAR[10];
  381. _itoa(m_nSecond,c,10);
  382. int num1=c[0]-48;
  383. int num2=c[1]-48;
  384. DrawSingleNumber(num1,nLeft);
  385. nLeft+=m_nWidth+m_nSpace;
  386. DrawSingleNumber(num2,nLeft);
  387. delete []c;
  388. }
  389. }
  390. void CDigitalClock::DrawTimer()
  391. {
  392. if (m_memDC.m_hDC!=NULL)
  393. {
  394. GetClientRect(&m_rect);
  395. m_nWidth = (int)(m_rect.Height()-2*m_nYmargin)/2;
  396. if (0 == m_nAlign)
  397. {
  398. m_nXmargin = m_nYmargin;
  399. }
  400. else if (2 == m_nAlign)
  401. {
  402. m_nXmargin = m_rect.Width()-8*m_nWidth-3*m_nSpace-m_nYmargin;
  403. }
  404. else
  405. {
  406. m_nXmargin = (int)(m_rect.Width()-8*m_nWidth-3*m_nSpace)/2;
  407. }
  408. m_memDC.FillSolidRect(m_rect,m_crBk);
  409. CPen pen(PS_SOLID,1,m_crText);
  410. CPen* pOldPen=m_memDC.SelectObject(&pen);
  411. DrawHour();
  412. DrawMinute();
  413. DrawSecond();
  414. Invalidate();
  415. m_memDC.SelectObject(pOldPen);
  416. pen.DeleteObject();
  417. }
  418. }
  419. void CDigitalClock::DrawSingleNumber(int nNum,int nLeft)
  420. {
  421. //return;
  422. switch (nNum)
  423. {
  424. case 0:
  425. DrawSection1(nLeft);
  426. DrawSection2(nLeft);
  427. DrawSection3(nLeft);
  428. DrawSection4(nLeft);
  429. DrawSection5(nLeft);
  430. DrawSection6(nLeft);
  431. break;
  432. case 1:
  433. DrawSection2(nLeft);
  434. DrawSection3(nLeft);
  435. break;
  436. case 2:
  437. DrawSection1(nLeft);
  438. DrawSection2(nLeft);
  439. DrawSection4(nLeft);
  440. DrawSection5(nLeft);
  441. DrawSection7(nLeft);
  442. break;
  443. case 3:
  444. DrawSection1(nLeft);
  445. DrawSection2(nLeft);
  446. DrawSection3(nLeft);
  447. DrawSection4(nLeft);
  448. DrawSection7(nLeft);
  449. break;
  450. case 4:
  451. DrawSection2(nLeft);
  452. DrawSection3(nLeft);
  453. DrawSection6(nLeft);
  454. DrawSection7(nLeft);
  455. break;
  456. case 5:
  457. DrawSection1(nLeft);
  458. DrawSection3(nLeft);
  459. DrawSection4(nLeft);
  460. DrawSection6(nLeft);
  461. DrawSection7(nLeft);
  462. break;
  463. case 6:
  464. DrawSection1(nLeft);
  465. DrawSection3(nLeft);
  466. DrawSection4(nLeft);
  467. DrawSection5(nLeft);
  468. DrawSection6(nLeft);
  469. DrawSection7(nLeft);
  470. break;
  471. case 7:
  472. DrawSection1(nLeft);
  473. DrawSection2(nLeft);
  474. DrawSection3(nLeft);
  475. break;
  476. case 8:
  477. DrawSection1(nLeft);
  478. DrawSection2(nLeft);
  479. DrawSection3(nLeft);
  480. DrawSection4(nLeft);
  481. DrawSection5(nLeft);
  482. DrawSection6(nLeft);
  483. DrawSection7(nLeft);
  484. break;
  485. case 9:
  486. DrawSection1(nLeft);
  487. DrawSection3(nLeft);
  488. DrawSection4(nLeft);
  489. DrawSection2(nLeft);
  490. DrawSection6(nLeft);
  491. DrawSection7(nLeft);
  492. break;
  493. default:
  494. ;
  495. }
  496. }
  497. void CDigitalClock::OnPaint()
  498. {
  499. CPaintDC dc(this); // device context for painting
  500. GetClientRect(&m_rect);
  501. if (m_memDC.m_hDC==NULL)
  502. {
  503. m_nYmargin = (int)(0.1*m_rect.Height());
  504. CBitmap bmp;
  505. CBitmap* pOldBmp;
  506. m_memDC.CreateCompatibleDC(&dc);
  507. bmp.CreateCompatibleBitmap(&dc,m_rect.Width(),m_rect.Height());
  508. pOldBmp=m_memDC.SelectObject(&bmp);
  509. }
  510. dc.BitBlt(0,0,m_rect.Width(),m_rect.Height(),&m_memDC,0,0,SRCCOPY);
  511. }
  512. void CDigitalClock::SetSpace(int nSpace)
  513. {
  514. m_nSpace = nSpace;
  515. DrawTimer();
  516. }
  517. int CDigitalClock::GetSpace()
  518. {
  519. return m_nSpace;
  520. }