以下是用VC++做的时钟程序:
这是文件Clock.cpp
#include "Clock.h"
#include "math.h"
BEGIN_MESSAGE_MAP(CClockWnd,CFrameWnd)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_SIZE()
END_MESSAGE_MAP()
CClockWnd::CClockWnd()
{
fsecond=0;
fmin=0;
fhour=0;
struct tm* newtime;
long ltime;
time(<ime);
newtime=localtime(<ime);
fsecond=6*newtime->tm_sec;
fmin=6*newtime->tm_min;
fhour=30*(newtime->tm_hour%12);
}
void CClockWnd::OnSize(UINT nType,int cx,int cy)
{
CRect rc;
GetClientRect(&rc);
if(rc.Width()>rc.Height())
nradius=(float)(rc.Height()/2);
else
nradius=(float)(rc.Width()/2);
}
CClockWnd::~CClockWnd()
{
}
void CClockWnd::DrawSecond(CClientDC* pDC,double fsec,int ixo,int iyo)
{
point[0].x=ixo+(int)nradius*sin(fsec*3.1415926/180);
point[0].y=iyo-(int)nradius*cos(fsec*3.1415926/180);
pDC->MoveTo(ixo,iyo);
pDC->LineTo(point[0]);
}
void CClockWnd::DrawMin(CClientDC* pDC,double fmi,int ixo,int iyo)
{
point[1].x=ixo+(int)nradius*0.8*sin(fmi*3.1415926/180);
point[1].y=iyo-(int)nradius*0.8*cos(fmi*3.1415926/180);
pDC->MoveTo(ixo,iyo);
pDC->LineTo(point[1]);
}
void CClockWnd::DrawHour(CClientDC* pDC,double fhou,int ixo,int iyo)
{
point[2].x=ixo+(int)nradius*0.5*sin(fhou*3.1415926/180);
point[2].y=iyo-(int)nradius*0.5*cos(fhou*3.1415926/180);
pDC->MoveTo(ixo,iyo);
pDC->LineTo(point[2]);
}
void CClockWnd::Draw()
{
CRect rc;
CClientDC dc(this);
CPen pen,pen1,pen2,pen3,pen4;
CPen* oldpen;
GetClientRect(&rc);
CBrush brush(RGB(0,200,0));
CBrush* oldbrush;
oldbrush=dc.SelectObject(&brush);
//dc.SetBkMode(TRANSPARENT);
int ixo=(rc.left+rc.right)/2;
int iyo=(rc.top+rc.bottom)/2;
int i=0;
int xx,yy;
dc.Ellipse(ixo-nradius,iyo-nradius,ixo+nradius,iyo+nradius);
dc.Ellipse(ixo-nradius*0.85,iyo-nradius*0.85,ixo+nradius*0.85,iyo+nradius*0.85);
dc.Ellipse(ixo-nradius/20,iyo-nradius/20,ixo+nradius/20,iyo+nradius/20);
do
{
xx=ixo+(int)nradius*0.85*cos(i*3.1415926/180);
yy=iyo-(int)nradius*0.85*sin(i*3.1415926/180);
dc.MoveTo(xx,yy);
xx=ixo+(int)nradius*cos(i*3.1415926/180);
yy=iyo-(int)nradius*sin(i*3.1415926/180);
dc.LineTo(xx,yy);
i=i+6;
}while(i<=360);
i=0;
do
{
xx=ixo+(int)nradius*0.75*cos(i*3.1415926/180);
yy=iyo-(int)nradius*0.75*sin(i*3.1415926/180);
dc.MoveTo(xx,yy);
xx=ixo+(int)nradius*cos(i*3.1415926/180);
yy=iyo-(int)nradius*sin(i*3.1415926/180);
dc.LineTo(xx,yy);
i=i+30;
}while(i<=360);
pen.CreatePen(PS_DASH,2,RGB(0,0,255));
oldpen=dc.SelectObject(&pen);
DrawSecond(&dc,fsecond,ixo,iyo);
dc.SelectObject(&oldpen);
pen1.CreatePen(PS_DASH,6,RGB(0,255,0));
oldpen=dc.SelectObject(&pen1);
DrawMin(&dc,fmin,ixo,iyo);
dc.SelectObject(oldpen);
pen2.CreatePen(PS_DASH,10,RGB(255,0,0));
oldpen=dc.SelectObject(&pen2);
DrawHour(&dc,fhour,ixo,iyo);
dc.SelectObject(oldpen);
ShowTime(&dc,ixo,iyo);
ReleaseDC(&dc);
}
void CClockWnd::ShowTime(CClientDC* pDC,int ixo,int iyo)
{
struct tm* newtime;
long ltime;
time(<ime);
char sztime[30];
newtime=localtime(<ime);
strcpy(sztime,asctime(newtime));
sztime[24]='\0';
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(ixo*0.8,iyo*1.9,sztime);
}
UINT CClockWnd::GetRand(UINT nInput)
{
float nConst=(float)rand()/RAND_MAX;
float nRand=(float)nConst*nInput;
return (UINT)nRand;
}
COLORREF CClockWnd::GetRandColor()
{
return RGB(GetRand(255),GetRand(255),GetRand(255));
}
void CClockWnd::OnTimer(UINT nIDEvent)
{
struct tm* newtime;
long ltime;
time(<ime);
newtime=localtime(<ime);
fsecond=6*newtime->tm_sec;
fmin=6*newtime->tm_min;
fhour=30*(newtime->tm_hour%12);
OnPaint();
}
void CClockWnd::OnPaint()
{
CFrameWnd::OnPaint();
Draw();
}
BOOL CClockApp::InitInstance()
{
CClockWnd* pFrame=new CClockWnd;
pFrame->Create(0,"coding");
pFrame->ShowWindow(SW_SHOWNORMAL);
pFrame->UpdateWindow();
this->m_pMainWnd=pFrame;
pFrame->SetTimer(2,1000,NULL);
return TRUE;
}
CClockApp theApp;
//这是头文件
Clock.h
#include <afxwin.h>
#include <afxext.h>
class CClockApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CClockWnd:public CFrameWnd
{
public:
CPoint point[3];
int nradius;
double fsecond;
double fmin;
double fhour;
CClockWnd::CClockWnd();
CClockWnd::~CClockWnd();
void Draw();
void ShowTime(CClientDC* pDC,int ixo,int iyo);
void DrawSecond(CClientDC* pDC,double fsec,int ixo,int iyo);
void DrawMin(CClientDC* pDC,double fmi,int ixo,int iyo);
void DrawHour(CClientDC* pDC,double fhou,int ixo,int iyo);
UINT GetRand(UINT nInput);
COLORREF GetRandColor();
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType,int cx,int cy);
DECLARE_MESSAGE_MAP();
};