>_<:这是一个时钟小程序

>_<:通过调用获得系统时间然后经过计算得出当前时间,然后再以3个圆环表示时分秒。

>_<:TAO_CLOCK.h

  1. class CMyApp : public CWinApp
  2. {
  3. public:
  4. virtual BOOL InitInstance ();
  5. };
  6.  
  7. class CMainWindow : public CFrameWnd
  8. {
  9. protected:
  10.  
  11. int m_nPrevSecond;
  12. int m_nPrevMinute;
  13. int m_nPrevHour;
  14.  
  15. void Draw(CDC* pDC, int nLength, int nDegrees,
  16. COLORREF clrColor);//绘制针
  17. void DrawQ(CDC* pDC, int nLength, COLORREF clrColor);//圈遮盖
  18.  
  19. public:
  20. CMainWindow ();
  21.  
  22. protected:
  23. afx_msg int OnCreate (LPCREATESTRUCT lpcs); //设置计时器
  24. afx_msg void OnTimer (UINT nTimerID); //当WM_TIMER消息到达后就得到当前时间....
  25. afx_msg void OnPaint ();
  26. afx_msg void OnClose (); //清楚计时器
  27.  
  28. DECLARE_MESSAGE_MAP ()
  29. };

>_<:TAO_CLOCK.cpp

  1. #include <afxwin.h>
  2. #include <math.h>
  3. #include "TAO_CLOCK.h"
  4.  
  5. #define SQUARESIZE 20
  6. #define ID_TIMER_CLOCK 1
  7. #define kuan 60
  8. #define miaoL 200
  9. #define fenL miaoL-kuan
  10. #define shiL fenL-kuan
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19. SetRegistryKey (_T ("Programming Windows with MFC"));
  20. m_pMainWnd = new CMainWindow;
  21. m_pMainWnd->ShowWindow (m_nCmdShow);
  22. m_pMainWnd->UpdateWindow ();
  23. return TRUE;
  24. }
  25.  
  26. /////////////////////////////////////////////////////////////////////////
  27. // CMainWindow message map and member functions
  28.  
  29. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  30. ON_WM_CREATE ()
  31. ON_WM_PAINT ()
  32. ON_WM_TIMER ()
  33. ON_WM_CLOSE ()
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.  
  39. //得到系统时间,然后初始化时分秒
  40. CTime time = CTime::GetCurrentTime ();
  41. m_nPrevSecond = time.GetSecond ();
  42. m_nPrevMinute = time.GetMinute ();
  43. m_nPrevHour = time.GetHour () % ;
  44.  
  45. CString strWndClass = AfxRegisterWndClass (
  46. CS_HREDRAW | CS_VREDRAW,
  47. myApp.LoadStandardCursor (IDC_ARROW),
  48. (HBRUSH) (COLOR_3DFACE + ),
  49. NULL
  50. );
  51.  
  52. Create (strWndClass, _T ("Life_leving"));
  53. }
  54.  
  55. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  56. {
  57. if (CFrameWnd::OnCreate (lpcs) == -)
  58. return -;
  59.  
  60. //
  61. // Set a timer to fire at 1-second intervals.
  62. //
  63. if (!SetTimer (ID_TIMER_CLOCK, , NULL)) {
  64. MessageBox (_T ("SetTimer failed"), _T ("Error"),
  65. MB_ICONSTOP | MB_OK);
  66. return -;
  67. }
  68.  
  69. return ;
  70. }
  71.  
  72. void CMainWindow::OnClose ()
  73. {
  74. KillTimer (ID_TIMER_CLOCK);
  75. CFrameWnd::OnClose ();
  76. }
  77.  
  78. //当WM_TIMER消息到达后,它就得到当前时间,
  79. //他将时分秒分别与成员变量记录的时分秒比较
  80. //如果相同不会执行任何操作,否则将记录新时间
  81. //并移动Clock的指针
  82. void CMainWindow::OnTimer (UINT nTimerID)
  83. {
  84. /*判断窗口当前是否最小化,最小化返回非0
  85. 可以减小CPU负担isZoomed()判断最大化
  86. 但是在任务栏鼠标靠近时不显示时钟走动效果
  87. if (IsIconic ())
  88. return;
  89. */
  90.  
  91. //获取当前时间并与记录时间比较,一样不做任何处理
  92. CTime time = CTime::GetCurrentTime ();
  93. int nSecond = time.GetSecond ();
  94. int nMinute = time.GetMinute ();
  95. int nHour = time.GetHour () % ;
  96.  
  97. if ((nSecond == m_nPrevSecond) &&
  98. (nMinute == m_nPrevMinute) &&
  99. (nHour == m_nPrevHour))
  100. return;
  101.  
  102. //最小化时显示数字时钟
  103. if(IsIconic()){
  104. CString time;
  105. time.Format(_T("%0.2d: %0.2d: %0.2d"),nHour,nMinute,nSecond);
  106. SetWindowText(time);
  107. return;
  108. }
  109. SetWindowText(_T("Life_leving"));
  110.  
  111. //MM_ISOTROPIC:X轴和Y轴具有相同的逻辑单位(映射)
  112. //设置每个方向上设置具有1000个单位
  113. //把原点移至中心
  114. CRect rect;
  115. GetClientRect (&rect);
  116.  
  117. CClientDC dc (this);
  118. dc.SetMapMode (MM_ISOTROPIC);
  119. dc.SetWindowExt (, );
  120. dc.SetViewportExt (rect.Width (), -rect.Height ());
  121. dc.SetViewportOrg (rect.Width () / , rect.Height () / );
  122.  
  123. //
  124. // If minutes have changed, erase the hour and minute hands.
  125. //
  126. COLORREF clrColor = ::GetSysColor (COLOR_3DFACE);
  127.  
  128. if (nMinute != m_nPrevMinute) {
  129. m_nPrevMinute = nMinute;//更新时分
  130. m_nPrevHour = nHour;
  131. }
  132.  
  133. //
  134. // If seconds have changed, erase the second hand and redraw all hands.
  135. //
  136. if (nSecond != m_nPrevSecond) {
  137. CRect rect(-,,,-);
  138. CBrush brush(clrColor);
  139. CBrush* pOldBrush=dc.SelectObject(&brush);
  140. dc.Rectangle(rect);
  141. dc.SelectObject(pOldBrush);
  142. Draw(&dc,miaoL, nSecond * , RGB (, , ));//每秒走6度
  143. Draw(&dc,shiL, (nHour * ) + (nMinute / ),//每小时30度+每分钟0.5度
  144. RGB (, , ));
  145. Draw(&dc, fenL, nMinute * , RGB (, , ));//每分钟6度
  146. m_nPrevSecond = nSecond;//更新秒
  147. }
  148.  
  149. //if(nSecond==0)DrawQ(&dc,miaoL,clrColor);
  150. //if(nMinute==0)DrawQ(&dc,fenL,clrColor);
  151. //if(nHour==0)DrawQ(&dc,shiL,clrColor);
  152.  
  153. }
  154.  
  155. void CMainWindow::OnPaint ()
  156. {
  157. //重绘的时候也要重新设定窗口映射
  158. CRect rect;
  159. GetClientRect (&rect);
  160.  
  161. CPaintDC dc (this);
  162. dc.SetMapMode (MM_ISOTROPIC);
  163. dc.SetWindowExt (, );
  164. dc.SetViewportExt (rect.Width (), -rect.Height ());
  165. dc.SetViewportOrg (rect.Width () / , rect.Height () / );
  166.  
  167. //画上钟面+时分秒针
  168. Draw(&dc,shiL, (m_nPrevHour * ) +
  169. (m_nPrevMinute / ), RGB (, , ));
  170. Draw(&dc,fenL, m_nPrevMinute * , RGB (, , ));
  171. Draw(&dc,miaoL, m_nPrevSecond * , RGB (, , ));
  172.  
  173. COLORREF clrColor = ::GetSysColor (COLOR_3DFACE);
  174.  
  175. }
  176.  
  177. //画秒针
  178. void CMainWindow::Draw (CDC* pDC, int nLength ,
  179. int nDegrees, COLORREF clrColor)
  180. {
  181. CPoint point[];
  182. CRect rect(-(nLength+kuan),(nLength+kuan),(nLength+kuan),-(nLength+kuan));
  183.  
  184. double nRadians = (double) nDegrees * 0.017453292;//转化为弧度制
  185.  
  186. point[].x = ;
  187. point[].y = ;
  188. point[].x = (int) ((nLength+) * sin (nRadians));//注意转换(坐标系变换)
  189. point[].y = (int) ((nLength+) * cos (nRadians));
  190.  
  191. CPen pen (PS_SOLID,kuan, clrColor);
  192. CPen* pOldPen = pDC->SelectObject (&pen);
  193.  
  194. pDC->Arc(rect,point[],point[]);
  195.  
  196. pDC->SelectObject (pOldPen);
  197. }
  198.  
  199. //画圈
  200. void CMainWindow::DrawQ (CDC* pDC, int nLength, COLORREF clrColor){
  201.  
  202. CRect rect(-(nLength+kuan),(nLength+kuan),(nLength+kuan),-(nLength+kuan));
  203. CPen pen (PS_SOLID,kuan, clrColor);
  204. CBrush brush(clrColor);
  205. CPen* pOldPen = pDC->SelectObject (&pen);
  206. CBrush* pOldBrush = pDC->SelectObject (&brush);
  207. pDC->Ellipse(rect);
  208. Draw(pDC,shiL, (m_nPrevHour * ) + (m_nPrevMinute / ),//每小时30度+每分钟0.5度
  209. RGB (, , ));
  210. Draw(pDC, fenL, m_nPrevMinute * , RGB (, , ));//每分钟6度
  211. pDC->SelectObject (pOldPen);
  212. pDC->SelectObject (pOldBrush);
  213. }

[游戏学习28] MFC 时钟的更多相关文章

  1. [游戏学习22] MFC 井字棋 双人对战

    >_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也 ...

  2. [游戏学习26] MFC 时间函数 画图形

    >_<:这里第一次介绍MFC的时间函数,功能和Win32里的计时器类似. >_<:这里还介绍了MFC的图形绘制函数,和Win32有一点区别 >_<:ABC.h #d ...

  3. [游戏学习23] MFC 画尺子

    >_<:这是个简单的MFC程序,有利于了解MFC的框架结构 >_<:Ruler.h #include<afxwin.h> class CMyApp:public C ...

  4. [游戏学习24] MFC 各种绘图 字体学习

    >_<:这里包含字体设置及各种绘图,只要稍微看一下代码就能理解,这里不多介绍 >_<:Hello.h #include<afxwin.h> class CMyApp ...

  5. [游戏学习27] MFC 匀速运动

    >_<:理解上一个时间函数的概念和用法,本节的实现也比较简单 >_<:就是简单的绘图+时间函数 >_<:TicTac.h #define EX 1 //该点左鼠标 ...

  6. [游戏学习25] MFC 橡皮筋画线效果

    >_<:这是给出窗口内外不同情况的处理展示的例子. >_<:MouseCap.h #include<afxwin.h> class CMyApp :public C ...

  7. 我的游戏学习日志3——三国志GBA

    我的游戏学习日志3——三国志GBA 三国志GBA由日本光荣公司1991~1995所推出<三国志>系列游戏,该作是光荣在GBA上推出的<三国志>系列作品的第一款.本游戏登场武将总 ...

  8. 催希凡javaweb 学习28天

    看到这样的博客,自己也在看传智播客的视频,收藏一下 催希凡javaweb 学习28天 http://www.cnblogs.com/Prozhu/category/824899.html

  9. Libgdx游戏学习(1)——环境配置及demo运行

    原文: Libgdx游戏学习(1)--环境配置及demo运行 - Stars-One的杂货小窝 Libgdx游戏是基于Java的一款游戏引擎,可以发布Android,桌面端,Html,IOS等游戏,出 ...

随机推荐

  1. Dictionary和IDictionary

    Dictionary<string> s = new Dictionary<string>(); 这个是s是Dictionary类型.是个类 类型,实现了接口,提供了更多的方法 ...

  2. oracle sql优化

    整理一下网上所看到sql优化方法 1.使用大写字母书写sql,因为oracle解释器会先将sql语句转换成大写后再解释 2    减少访问数据库的次数,多数情况下一条sql可以达到目的的,就不要使用多 ...

  3. 《深入理解Nginx》阅读与实践(四):简单的HTTP过滤模块

    一.Nginx的HTTP过滤模块特征 一个请求可以被任意个HTTP模块处理: 在普通HTTP模块处理请求完毕并调用ngx_http_send_header()发送HTTP头部或调用ngx_http_o ...

  4. 添加和删除hadoop集群中的节点

    参见 http://www.cnblogs.com/tommyli/p/3418273.html

  5. json传值以及ajax接收

    一开始进入公司,做起项目来比较不知所措,现在一个月过去了,越来越得心应手,下面是json向前端传值以及前端ajax接收,给自己记下也分享给大家. 这是两个类型不同的json与ajax的数据交互(集合. ...

  6. Linux系统编程-setitimer函数

    功能:linux系统编程中,setitimer是一个经常被使用的函数,可用来实现延时和定时的功能. 头文件:sys/time.h 函数原型: int setitimer(int which, cons ...

  7. temp--test audio micphone

    DWORD CALLBACK waveInProc(HWAVEIN hWaveIn, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwPara ...

  8. 判断闰年的方法以及如何获得单链表的倒数第K个元素

    今天很悲催,心中向往的公司,打电话过来面试,问到我两个问题,结果竟然都没有回答上,伤心了,记录下今天失败,希望以后不要被同样的问题给PASS. 问题1.如何判断是否为闰年 所谓闰年那就是:四年一闰,百 ...

  9. Xshell连接Linux下Oracle无法回退的解决办法

    使用Xshell 连接远程Linux 数据库服务器,当切换到sqlplus 控制台时,输入错误字符的时候,使用回退键修改时,显示^H. 解决方法: 在控制太命令中输入stty erase ^H 回车就 ...

  10. Leveldb之version与version_set详细对比

    version类包含的重要变量: VersionSet* vset_; // VersionSet to which this Version belongs Version* next_; // N ...