用MFC编写,会自动生成很多代码,这里为主要部分,还要绘图的
此源代码为主要函数部分:
// clock2Dlg.cpp : implementation file
#include "stdafx.h"
#include "clock2.h"
#include "clock2Dlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClock2Dlg dialog
CClock2Dlg::CClock2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CClock2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClock2Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClock2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClock2Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClock2Dlg, CDialog)
//{{AFX_MSG_MAP(CClock2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClock2Dlg message handlers
BOOL CClock2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
//定时器:
SetTimer(1,1000,NULL);
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CClock2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CClock2Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
//下面输入时钟程序代码:
CDC *pDC=this->GetDC();
CRect rc;
GetClientRect(&rc);//
int xStart=rc.right/2; //计算中心点
int yStart=rc.bottom/2;
CTime time=CTime::GetCurrentTime(); //得到当前的时间
CString strDigits;
int i,x,y;
CSize size;
CPen Pen(PS_SOLID,5,RGB(0,0,0));
CPen *pOldPen=pDC->SelectObject(&Pen);
pDC->Ellipse(5,5,rc.right-5,rc.bottom-5); //画圆盘
double Radians; //定义角度
pDC->SetTextColor(RGB(0,0,0)); //开始写入数字1-12
for(i=1;i<=12;i++)
{
strDigits.Format("%d",i);
size=pDC->GetTextExtent(strDigits,strDigits.GetLength());
Radians=(double)i*6.28/12.0;
x=xStart-(size.cx/2)+(int)((double)(xStart-20)*sin(Radians));//数字水平位置
y=yStart-(size.cy/2)-(int)((double)(yStart-20)*cos(Radians));//数字垂直位置
pDC->TextOut(x,y,strDigits);
}
//开始绘制时针
Radians=(double)time.GetHour()+(double)time.GetMinute()/60.00+(double)time.GetSecond()/3600.00;
Radians*=6.28/12.0;
CPen HourPen(PS_SOLID,5,RGB(0,0,0)); //定义时针线宽
pDC->SelectObject(HourPen);
pDC->MoveTo(xStart,yStart);
pDC->LineTo(xStart+(int)((double)(xStart/2)*sin(Radians)),yStart-(int)((double) (yStart/2)*cos(Radians)));
//开始绘制分针
Radians=(double)time.GetMinute()+(double)time.GetSecond()/60.00;
Radians*=6.28/60.0;
CPen MinutePen(PS_SOLID,3,RGB(0,0,0)); //定义分针线宽
pDC->SelectObject(MinutePen);
pDC->MoveTo(xStart,yStart);
pDC->LineTo(xStart+(int)((double)(xStart*2/3)*sin(Radians)),yStart-(int)((double) (yStart*2/3)*cos(Radians)));
//开始绘制秒针
Radians=(double)time.GetSecond();
Radians*=6.28/60.0;
CPen SecondPen(PS_SOLID,1,RGB(0,0,0)); //定义秒针线宽
pDC->SelectObject(SecondPen);
pDC->MoveTo(xStart,yStart);
pDC->LineTo(xStart+(int)((double)(xStart*4/5)*sin(Radians)),yStart-(int)((double)(yStart*4/5)*cos(Radians)));
pDC->SelectObject(pOldPen);
//时钟代码结束
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CClock2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//有关定时器:
void CClock2Dlg::OnTimer(UINT nIDEvent)
{
InvalidateRect(NULL,true);
UpdateWindow();
CDialog::OnTimer(nIDEvent);
}