[游戏学习28] MFC 时钟

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

[游戏学习28] MFC 时钟

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

>_<:TAO_CLOCK.h

 class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance ();
}; class CMainWindow : public CFrameWnd
{
protected: int m_nPrevSecond;
int m_nPrevMinute;
int m_nPrevHour; void Draw(CDC* pDC, int nLength, int nDegrees,
COLORREF clrColor);//绘制针
void DrawQ(CDC* pDC, int nLength, COLORREF clrColor);//圈遮盖 public:
CMainWindow (); protected:
afx_msg int OnCreate (LPCREATESTRUCT lpcs); //设置计时器
afx_msg void OnTimer (UINT nTimerID); //当WM_TIMER消息到达后就得到当前时间....
afx_msg void OnPaint ();
afx_msg void OnClose (); //清楚计时器 DECLARE_MESSAGE_MAP ()
};

>_<:TAO_CLOCK.cpp

 #include <afxwin.h>
#include <math.h>
#include "TAO_CLOCK.h" #define SQUARESIZE 20
#define ID_TIMER_CLOCK 1
#define kuan 60
#define miaoL 200
#define fenL miaoL-kuan
#define shiL fenL-kuan CMyApp myApp; /////////////////////////////////////////////////////////////////////////
// CMyApp member functions BOOL CMyApp::InitInstance ()
{
SetRegistryKey (_T ("Programming Windows with MFC"));
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
} /////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_CREATE ()
ON_WM_PAINT ()
ON_WM_TIMER ()
ON_WM_CLOSE ()
END_MESSAGE_MAP () CMainWindow::CMainWindow ()
{ //得到系统时间,然后初始化时分秒
CTime time = CTime::GetCurrentTime ();
m_nPrevSecond = time.GetSecond ();
m_nPrevMinute = time.GetMinute ();
m_nPrevHour = time.GetHour () % ; CString strWndClass = AfxRegisterWndClass (
CS_HREDRAW | CS_VREDRAW,
myApp.LoadStandardCursor (IDC_ARROW),
(HBRUSH) (COLOR_3DFACE + ),
NULL
); Create (strWndClass, _T ("Life_leving"));
} int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
{
if (CFrameWnd::OnCreate (lpcs) == -)
return -; //
// Set a timer to fire at 1-second intervals.
//
if (!SetTimer (ID_TIMER_CLOCK, , NULL)) {
MessageBox (_T ("SetTimer failed"), _T ("Error"),
MB_ICONSTOP | MB_OK);
return -;
} return ;
} void CMainWindow::OnClose ()
{
KillTimer (ID_TIMER_CLOCK);
CFrameWnd::OnClose ();
} //当WM_TIMER消息到达后,它就得到当前时间,
//他将时分秒分别与成员变量记录的时分秒比较
//如果相同不会执行任何操作,否则将记录新时间
//并移动Clock的指针
void CMainWindow::OnTimer (UINT nTimerID)
{
/*判断窗口当前是否最小化,最小化返回非0
可以减小CPU负担isZoomed()判断最大化
但是在任务栏鼠标靠近时不显示时钟走动效果
if (IsIconic ())
return;
*/ //获取当前时间并与记录时间比较,一样不做任何处理
CTime time = CTime::GetCurrentTime ();
int nSecond = time.GetSecond ();
int nMinute = time.GetMinute ();
int nHour = time.GetHour () % ; if ((nSecond == m_nPrevSecond) &&
(nMinute == m_nPrevMinute) &&
(nHour == m_nPrevHour))
return; //最小化时显示数字时钟
if(IsIconic()){
CString time;
time.Format(_T("%0.2d: %0.2d: %0.2d"),nHour,nMinute,nSecond);
SetWindowText(time);
return;
}
SetWindowText(_T("Life_leving")); //MM_ISOTROPIC:X轴和Y轴具有相同的逻辑单位(映射)
//设置每个方向上设置具有1000个单位
//把原点移至中心
CRect rect;
GetClientRect (&rect); CClientDC dc (this);
dc.SetMapMode (MM_ISOTROPIC);
dc.SetWindowExt (, );
dc.SetViewportExt (rect.Width (), -rect.Height ());
dc.SetViewportOrg (rect.Width () / , rect.Height () / ); //
// If minutes have changed, erase the hour and minute hands.
//
COLORREF clrColor = ::GetSysColor (COLOR_3DFACE); if (nMinute != m_nPrevMinute) {
m_nPrevMinute = nMinute;//更新时分
m_nPrevHour = nHour;
} //
// If seconds have changed, erase the second hand and redraw all hands.
//
if (nSecond != m_nPrevSecond) {
CRect rect(-,,,-);
CBrush brush(clrColor);
CBrush* pOldBrush=dc.SelectObject(&brush);
dc.Rectangle(rect);
dc.SelectObject(pOldBrush);
Draw(&dc,miaoL, nSecond * , RGB (, , ));//每秒走6度
Draw(&dc,shiL, (nHour * ) + (nMinute / ),//每小时30度+每分钟0.5度
RGB (, , ));
Draw(&dc, fenL, nMinute * , RGB (, , ));//每分钟6度
m_nPrevSecond = nSecond;//更新秒
} //if(nSecond==0)DrawQ(&dc,miaoL,clrColor);
//if(nMinute==0)DrawQ(&dc,fenL,clrColor);
//if(nHour==0)DrawQ(&dc,shiL,clrColor); } void CMainWindow::OnPaint ()
{
//重绘的时候也要重新设定窗口映射
CRect rect;
GetClientRect (&rect); CPaintDC dc (this);
dc.SetMapMode (MM_ISOTROPIC);
dc.SetWindowExt (, );
dc.SetViewportExt (rect.Width (), -rect.Height ());
dc.SetViewportOrg (rect.Width () / , rect.Height () / ); //画上钟面+时分秒针
Draw(&dc,shiL, (m_nPrevHour * ) +
(m_nPrevMinute / ), RGB (, , ));
Draw(&dc,fenL, m_nPrevMinute * , RGB (, , ));
Draw(&dc,miaoL, m_nPrevSecond * , RGB (, , )); COLORREF clrColor = ::GetSysColor (COLOR_3DFACE); } //画秒针
void CMainWindow::Draw (CDC* pDC, int nLength ,
int nDegrees, COLORREF clrColor)
{
CPoint point[];
CRect rect(-(nLength+kuan),(nLength+kuan),(nLength+kuan),-(nLength+kuan)); double nRadians = (double) nDegrees * 0.017453292;//转化为弧度制 point[].x = ;
point[].y = ;
point[].x = (int) ((nLength+) * sin (nRadians));//注意转换(坐标系变换)
point[].y = (int) ((nLength+) * cos (nRadians)); CPen pen (PS_SOLID,kuan, clrColor);
CPen* pOldPen = pDC->SelectObject (&pen); pDC->Arc(rect,point[],point[]); pDC->SelectObject (pOldPen);
} //画圈
void CMainWindow::DrawQ (CDC* pDC, int nLength, COLORREF clrColor){ CRect rect(-(nLength+kuan),(nLength+kuan),(nLength+kuan),-(nLength+kuan));
CPen pen (PS_SOLID,kuan, clrColor);
CBrush brush(clrColor);
CPen* pOldPen = pDC->SelectObject (&pen);
CBrush* pOldBrush = pDC->SelectObject (&brush);
pDC->Ellipse(rect);
Draw(pDC,shiL, (m_nPrevHour * ) + (m_nPrevMinute / ),//每小时30度+每分钟0.5度
RGB (, , ));
Draw(pDC, fenL, m_nPrevMinute * , RGB (, , ));//每分钟6度
pDC->SelectObject (pOldPen);
pDC->SelectObject (pOldBrush);
}
上一篇:cf


下一篇:ubuntu系统安装配置