課設題目
模擬時鐘程序
課設要求
①基於MFC的WINDOWS應用程序設計方法,編寫一個模擬時鐘程序;
②在屏幕中間有一個指針式時鐘;
③這個時鐘的下方或者上方以數字方式顯示時間,該時間應與指針顯示的時間一致;
用菜單選項打開的對話框,或者主界面裏的控件設置時間。時間不必與機器系統時間相同,可任意設置。
程序運行界面
核心代碼
// WeClockView.cpp : implementation of the CWeClockView class
//
#include "stdafx.h"
#include "WeClock.h"
#include <ctime>//
#include "WeClockDoc.h"
#include "WeClockView.h"
#include "SetTimeDlg.h"//
#include "math.h"//
#define PI 3.1415926//
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWeClockView
IMPLEMENT_DYNCREATE(CWeClockView, CView)
BEGIN_MESSAGE_MAP(CWeClockView, CView)
//{{AFX_MSG_MAP(CWeClockView)
ON_COMMAND(ID_START, OnStart)
ON_COMMAND(ID_STOP, OnStop)
ON_COMMAND(ID_SETTIME, OnSettime)
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWeClockView construction/destruction
CWeClockView::CWeClockView()
{
// TODO: add construction code here
//設定時間的初始值爲系統時間
SYSTEMTIME st;
GetLocalTime(&st);
day = st.wDay;
hour = st.wHour;
minute = st.wMinute;
month = st.wMonth;
second = st.wSecond;
year = st.wYear;
//設定畫筆/畫刷
m_HouPen.CreatePen (PS_SOLID,5,RGB (0,0,0));//時針畫筆,實心,寬度爲5,黑色
m_MinPen.CreatePen (PS_SOLID,3,RGB (0,0,0));//分針畫筆
m_SecPen.CreatePen (PS_SOLID,1,RGB (255,0,0));//秒針畫筆
m_LinePen.CreatePen (PS_SOLID,1,RGB (200,0,0));//矩形畫筆
m_MarkBrush.CreateSolidBrush (RGB (250,0,0));//刻度點畫筆
//設定表心位置
m_Center.x=500;
m_Center.y=150;
//設定時鐘半徑
m_Radius=125;
//計算指針位置
SetClock (hour,minute,second);
}
CWeClockView::~CWeClockView()
{
}
BOOL CWeClockView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CWeClockView drawing
CPoint CWeClockView::GetPoint(int nLenth,int nValue)
{
CPoint p;
double angle = nValue*PI/30-PI/2;
p.x=m_Center.x+(int) (nLenth*cos(angle));
p.y=m_Center.y+(int) (nLenth*sin(angle));
return p;
}
CWeClockDoc* CWeClockView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWeClockDoc)));
return (CWeClockDoc*)m_pDocument;
}
#endif //_DEBUG
void CWeClockView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent)//定時器發送過來消息
{
second++;//秒增加
if(second>59)
{
second=0;
minute++;//分增加
}
if(minute>59)
{
minute=0;
hour++;//小時增加
}
if(hour>23)
{
hour=0;
day++;//日增加
}
switch(month)
{
//大月
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day>31)
{
day=1;
month++;//月增加
}
break;
//小月
case 4:
case 6:
case 9:
case 11:
if(day>30)
{
day=1;
month++;//月增加
}
break;
case 2:
if((year%4==0&&year%100!=0&&day>29)||(year%400==0&&day>29))//閏二月
{
day=1;
month++;//月增加
}
if(year%4!=0||year%400!=0&&day>28)//二月
{
day=1;
month++;
}
break;
}
if (month>12)
{
year++;//年增加
month=1;
}
SetClock(hour,minute,second);
Invalidate(false);//重新繪製
}
CView::OnTimer(nIDEvent);
}
源碼及課設報告
需要的小夥伴請在海轟的微信公衆號:海轟Pro
回覆:海轟
O(∩_∩)O哈哈~