[游戏学习28] MFC 时钟

 

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

>_<:通过调用获得系统时间然后经过计算得出当前时间,然后再以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 
12     int m_nPrevSecond;        
13     int m_nPrevMinute;
14     int m_nPrevHour;
15 
16     void Draw(CDC* pDC, int nLength, int nDegrees,
17         COLORREF clrColor);//绘制针
18     void DrawQ(CDC* pDC, int nLength, COLORREF clrColor);//圈遮盖
19 
20 public:
21     CMainWindow ();
22 
23 protected:
24     afx_msg int OnCreate (LPCREATESTRUCT lpcs);       //设置计时器
25     afx_msg void OnTimer (UINT nTimerID);              //当WM_TIMER消息到达后就得到当前时间....
26     afx_msg void OnPaint ();
27     afx_msg void OnClose ();                           //清楚计时器
28 
29     DECLARE_MESSAGE_MAP ()
30 };

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