如果你非得要让我出洋相,我只好全贴出来了:// ClockDlg.h : header file //#if !defined(AFX_CLOCKDLG_H__0F131CD3_6D32_49BC_9423_383AEBA0A701__INCLUDED_) #define AFX_CLOCKDLG_H__0F131CD3_6D32_49BC_9423_383AEBA0A701__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 const float PI = 3.1415926535897932384626;///////////////////////////////////////////////////////////////////////////// // CClockDlg dialogclass CClockDlg : public CDialog { CRect wndRect; // Construction public: CClockDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data //{{AFX_DATA(CClockDlg) enum { IDD = IDD_CLOCK_DIALOG }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CClockDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CClockDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point); afx_msg void OnTimer(UINT nIDEvent); afx_msg void OnClose(); afx_msg void OnRButtonUp(UINT nFlags, CPoint point); afx_msg void OnExit(); //}}AFX_MSG DECLARE_MESSAGE_MAP() };//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CLOCKDLG_H__0F131CD3_6D32_49BC_9423_383AEBA0A701__INCLUDED_)
// ClockDlg.cpp : implementation file //#include "stdafx.h" #include "Clock.h" #include "ClockDlg.h" #include "winuser.h" #include "AboutDialog.h" #include "math.h" #include "time.h" #include "Panel.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog { public:// Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: //}}AFX_VIRTUAL// Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() };BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CClockDlg dialogCClockDlg::CClockDlg(CWnd* pParent /*=NULL*/) : CDialog(CClockDlg::IDD, pParent) { //{{AFX_DATA_INIT(CClockDlg) // 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 CClockDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CClockDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CClockDlg, CDialog) //{{AFX_MSG_MAP(CClockDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_LBUTTONDBLCLK() ON_WM_TIMER() ON_WM_CLOSE() ON_WM_RBUTTONUP() ON_COMMAND(MENU_EXIT, OnExit) //}}AFX_MSG_MAP END_MESSAGE_MAP()
谢谢大家, XunBaian(蓝水晶), 你的程序擦除好像有问题呀?
上面的没贴完,说不准超过三次,下面继续: ///////////////////////////////////////////////////////////////////////////// // CClockDlg message handlersBOOL CClockDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 GetWindowRect(wndRect); wndRect.top=50; wndRect.bottom=550; wndRect.left=100; wndRect.right=600; MoveWindow(wndRect); GetClientRect(wndRect); CRgn hRgn; hRgn.CreateEllipticRgn(wndRect.left,wndRect.top,wndRect.right,wndRect.bottom); SetWindowRgn(hRgn,TRUE); SetTimer(1,1000,NULL); return TRUE; // return TRUE unless you set the focus to a control }void CClockDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDialog 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 CClockDlg::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 { CPen CirClePen(PS_SOLID,1,RGB(255,255,255)); CPen PointPen(PS_SOLID,3,RGB(255,200,255)); CPen ScalePen(PS_SOLID,5,RGB(255,125,255)); CPen *pOldPen; CBrush MyBrush(RGB(0,0,0)); CBrush *pOldBrush; CRect CirCleRect; CDC *pDC=GetDC(); CirCleRect=wndRect; pOldBrush=pDC->SelectObject(&MyBrush); pDC->Ellipse(CirCleRect); pOldPen=pDC->SelectObject(&CirClePen); CPoint CentrePoint,NewPoint; float r; CentrePoint=CirCleRect.CenterPoint(); r=CirCleRect.Width()/2; for(int i=0;i<360;i++) { if (i%15==0) { pDC->SelectObject(&ScalePen); } else if (i%5==0) { pDC->SelectObject(&PointPen); } else { pDC->SelectObject(&CirClePen); } if(i%5==0) { NewPoint.x=CentrePoint.x+r*0.85*sin(PI*i/30); NewPoint.y=CentrePoint.y-r*0.85*cos(PI*i/30); pDC->MoveTo(NewPoint); } else { NewPoint.x=CentrePoint.x+r*0.9*sin(PI*i/30); NewPoint.y=CentrePoint.y-r*0.9*cos(PI*i/30); pDC->MoveTo(NewPoint); } NewPoint.x=CentrePoint.x+r*sin(PI*i/30); NewPoint.y=CentrePoint.y-r*cos(PI*i/30); pDC->LineTo(NewPoint); } pDC->SelectObject(pOldPen); pDC->SelectObject(pOldBrush); ReleaseDC(pDC); CClockDlg::OnTimer(1); CDialog::OnPaint(); } }// The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CClockDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; }void CClockDlg::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDialog::OnOK(); CDialog::OnLButtonDblClk(nFlags, point); }void CClockDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1) { CPoint NewPoint,CentrePoint=wndRect.CenterPoint(); CTime time; time=CTime::GetCurrentTime(); CDC *pDC=GetDC(); //时间指针 int iH=time.GetHour(); int iM=time.GetMinute(); int iS=time.GetSecond(); int r=wndRect.Width()/2; static int iiH,iiM,iiS; iH=iH*60*60+iM*60+iS; iM=iM*60+iS;
void CClockDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
CPoint NewPoint,CentrePoint=wndRect.CenterPoint();
CTime time;
time=CTime::GetCurrentTime();
CDC *pDC=GetDC();
//时间指针
int iH=time.GetHour();
int iM=time.GetMinute();
int iS=time.GetSecond();
int r=wndRect.Width()/2;
static int iiH,iiM,iiS;
iH=iH*60*60+iM*60+iS;
iM=iM*60+iS;
//时针,擦去前次时针
CPen *OldPen,HourPen(PS_SOLID,8,RGB(255,0,255)),OldHourPen(PS_SOLID,8,RGB(0,0,0));
OldPen=pDC->SelectObject(&OldHourPen);
NewPoint.x=CentrePoint.x-0.1*r*sin(PI*iiH/21600);
NewPoint.y=CentrePoint.y+0.1*r*cos(PI*iiH/21600);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.4*r*sin(PI*iiH/21600);
NewPoint.y=CentrePoint.y-0.4*r*cos(PI*iiH/21600);
iiH=iH;
pDC->LineTo(NewPoint);
//分针,擦去前次分针
CPen MinutePen(PS_SOLID,6,RGB(255,255,0)),OldMinutePen(PS_SOLID,6,RGB(0,0,0));
pDC->SelectObject(&OldMinutePen);
NewPoint.x=CentrePoint.x-0.2*r*sin(PI*iiM/1800);
NewPoint.y=CentrePoint.y+0.2*r*cos(PI*iiM/1800);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.6*r*sin(PI*iiM/1800);
NewPoint.y=CentrePoint.y-0.6*r*cos(PI*iiM/1800);
iiM=iM;
pDC->LineTo(NewPoint);
//秒针,擦去前次秒针
CPen SecondPen(PS_SOLID,2,RGB(255,0,0)),OldSecondPen(PS_SOLID,2,RGB(0,0,0));
pDC->SelectObject(&OldSecondPen);
NewPoint.x=CentrePoint.x-0.3*r*sin(PI*iiS/30);
NewPoint.y=CentrePoint.y+0.3*r*cos(PI*iiS/30);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.8*r*sin(PI*iiS/30);
NewPoint.y=CentrePoint.y-0.8*r*cos(PI*iiS/30);
iiS=iS;
pDC->LineTo(NewPoint);
//数字日期时间
CString strTime;
strTime=time.Format("%Y年%m月%d日 %H:%M:%S %A");
pDC->SetBkColor(RGB(0,0,0));
pDC->SetTextColor(RGB(200,255,255));
pDC->TextOut(wndRect.CenterPoint().x-100,wndRect.CenterPoint().y*1.35,strTime);
//时针
pDC->SelectObject(&HourPen);
NewPoint.x=CentrePoint.x-0.1*r*sin(PI*iH/21600);
NewPoint.y=CentrePoint.y+0.1*r*cos(PI*iH/21600);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.4*r*sin(PI*iH/21600);
NewPoint.y=CentrePoint.y-0.4*r*cos(PI*iH/21600);
pDC->LineTo(NewPoint);
//分针
pDC->SelectObject(&MinutePen);
NewPoint.x=CentrePoint.x-0.2*r*sin(PI*iM/1800);
NewPoint.y=CentrePoint.y+0.2*r*cos(PI*iM/1800);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.6*r*sin(PI*iM/1800);
NewPoint.y=CentrePoint.y-0.6*r*cos(PI*iM/1800);
pDC->LineTo(NewPoint);
//秒针
pDC->SelectObject(&SecondPen);
NewPoint.x=CentrePoint.x-0.3*r*sin(PI*iS/30);
NewPoint.y=CentrePoint.y+0.3*r*cos(PI*iS/30);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.8*r*sin(PI*iS/30);
NewPoint.y=CentrePoint.y-0.8*r*cos(PI*iS/30);
pDC->LineTo(NewPoint);
//中心柱
CRect CentreRect;
CPen CentrePen(PS_SOLID,5,RGB(255,0,0));
CBrush CentreBrush(RGB(100,100,0));
pDC->SelectObject(&CentreBrush);
pDC->SelectObject(&CentrePen);
CentreRect.left=wndRect.CenterPoint().x-8;
CentreRect.top=wndRect.CenterPoint().y-8;
CentreRect.right=wndRect.CenterPoint().x+8;
CentreRect.bottom=wndRect.CenterPoint().y+8;
pDC->Ellipse(CentreRect);
pDC->SelectObject(OldPen);
ReleaseDC(pDC);
}
CDialog::OnTimer(nIDEvent);
}
//#if !defined(AFX_CLOCKDLG_H__0F131CD3_6D32_49BC_9423_383AEBA0A701__INCLUDED_)
#define AFX_CLOCKDLG_H__0F131CD3_6D32_49BC_9423_383AEBA0A701__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
const float PI = 3.1415926535897932384626;/////////////////////////////////////////////////////////////////////////////
// CClockDlg dialogclass CClockDlg : public CDialog
{
CRect wndRect;
// Construction
public:
CClockDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
//{{AFX_DATA(CClockDlg)
enum { IDD = IDD_CLOCK_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CClockDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; // Generated message map functions
//{{AFX_MSG(CClockDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnClose();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnExit();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CLOCKDLG_H__0F131CD3_6D32_49BC_9423_383AEBA0A701__INCLUDED_)
//#include "stdafx.h"
#include "Clock.h"
#include "ClockDlg.h"
#include "winuser.h"
#include "AboutDialog.h"
#include "math.h"
#include "time.h"
#include "Panel.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CClockDlg dialogCClockDlg::CClockDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClockDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClockDlg)
// 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 CClockDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClockDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CClockDlg, CDialog)
//{{AFX_MSG_MAP(CClockDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDBLCLK()
ON_WM_TIMER()
ON_WM_CLOSE()
ON_WM_RBUTTONUP()
ON_COMMAND(MENU_EXIT, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
XunBaian(蓝水晶), 你的程序擦除好像有问题呀?
/////////////////////////////////////////////////////////////////////////////
// CClockDlg message handlersBOOL CClockDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // 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
GetWindowRect(wndRect);
wndRect.top=50;
wndRect.bottom=550;
wndRect.left=100;
wndRect.right=600;
MoveWindow(wndRect);
GetClientRect(wndRect);
CRgn hRgn;
hRgn.CreateEllipticRgn(wndRect.left,wndRect.top,wndRect.right,wndRect.bottom);
SetWindowRgn(hRgn,TRUE);
SetTimer(1,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}void CClockDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDialog 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 CClockDlg::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
{
CPen CirClePen(PS_SOLID,1,RGB(255,255,255));
CPen PointPen(PS_SOLID,3,RGB(255,200,255));
CPen ScalePen(PS_SOLID,5,RGB(255,125,255));
CPen *pOldPen;
CBrush MyBrush(RGB(0,0,0));
CBrush *pOldBrush;
CRect CirCleRect;
CDC *pDC=GetDC();
CirCleRect=wndRect;
pOldBrush=pDC->SelectObject(&MyBrush);
pDC->Ellipse(CirCleRect);
pOldPen=pDC->SelectObject(&CirClePen);
CPoint CentrePoint,NewPoint;
float r;
CentrePoint=CirCleRect.CenterPoint();
r=CirCleRect.Width()/2;
for(int i=0;i<360;i++)
{
if (i%15==0)
{
pDC->SelectObject(&ScalePen);
}
else if (i%5==0)
{
pDC->SelectObject(&PointPen);
}
else
{
pDC->SelectObject(&CirClePen);
}
if(i%5==0)
{
NewPoint.x=CentrePoint.x+r*0.85*sin(PI*i/30);
NewPoint.y=CentrePoint.y-r*0.85*cos(PI*i/30);
pDC->MoveTo(NewPoint);
}
else
{
NewPoint.x=CentrePoint.x+r*0.9*sin(PI*i/30);
NewPoint.y=CentrePoint.y-r*0.9*cos(PI*i/30);
pDC->MoveTo(NewPoint);
}
NewPoint.x=CentrePoint.x+r*sin(PI*i/30);
NewPoint.y=CentrePoint.y-r*cos(PI*i/30);
pDC->LineTo(NewPoint);
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
ReleaseDC(pDC);
CClockDlg::OnTimer(1); CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CClockDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CClockDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnOK();
CDialog::OnLButtonDblClk(nFlags, point);
}void CClockDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
CPoint NewPoint,CentrePoint=wndRect.CenterPoint();
CTime time;
time=CTime::GetCurrentTime();
CDC *pDC=GetDC();
//时间指针
int iH=time.GetHour();
int iM=time.GetMinute();
int iS=time.GetSecond();
int r=wndRect.Width()/2;
static int iiH,iiM,iiS;
iH=iH*60*60+iM*60+iS;
iM=iM*60+iS;
//时针,擦去前次时针
CPen *OldPen,HourPen(PS_SOLID,8,RGB(255,0,255)),OldHourPen(PS_SOLID,8,RGB(0,0,0));
OldPen=pDC->SelectObject(&OldHourPen);
NewPoint.x=CentrePoint.x-0.1*r*sin(PI*iiH/21600);
NewPoint.y=CentrePoint.y+0.1*r*cos(PI*iiH/21600);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.4*r*sin(PI*iiH/21600);
NewPoint.y=CentrePoint.y-0.4*r*cos(PI*iiH/21600);
iiH=iH;
pDC->LineTo(NewPoint);
//分针,擦去前次分针
CPen MinutePen(PS_SOLID,6,RGB(255,255,0)),OldMinutePen(PS_SOLID,6,RGB(0,0,0));
pDC->SelectObject(&OldMinutePen);
NewPoint.x=CentrePoint.x-0.2*r*sin(PI*iiM/1800);
NewPoint.y=CentrePoint.y+0.2*r*cos(PI*iiM/1800);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.6*r*sin(PI*iiM/1800);
NewPoint.y=CentrePoint.y-0.6*r*cos(PI*iiM/1800);
iiM=iM;
pDC->LineTo(NewPoint);
//秒针,擦去前次秒针
CPen SecondPen(PS_SOLID,2,RGB(255,0,0)),OldSecondPen(PS_SOLID,2,RGB(0,0,0));
pDC->SelectObject(&OldSecondPen);
NewPoint.x=CentrePoint.x-0.3*r*sin(PI*iiS/30);
NewPoint.y=CentrePoint.y+0.3*r*cos(PI*iiS/30);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.8*r*sin(PI*iiS/30);
NewPoint.y=CentrePoint.y-0.8*r*cos(PI*iiS/30);
iiS=iS;
pDC->LineTo(NewPoint);
//数字日期时间
CString strTime;
strTime=time.Format("%Y年%m月%d日 %H:%M:%S %A");
pDC->SetBkColor(RGB(0,0,0));
pDC->SetTextColor(RGB(200,255,255));
pDC->TextOut(wndRect.CenterPoint().x-100,wndRect.CenterPoint().y*1.35,strTime);
//时针
pDC->SelectObject(&HourPen);
NewPoint.x=CentrePoint.x-0.1*r*sin(PI*iH/21600);
NewPoint.y=CentrePoint.y+0.1*r*cos(PI*iH/21600);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.4*r*sin(PI*iH/21600);
NewPoint.y=CentrePoint.y-0.4*r*cos(PI*iH/21600);
pDC->LineTo(NewPoint);
//分针
pDC->SelectObject(&MinutePen);
NewPoint.x=CentrePoint.x-0.2*r*sin(PI*iM/1800);
NewPoint.y=CentrePoint.y+0.2*r*cos(PI*iM/1800);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.6*r*sin(PI*iM/1800);
NewPoint.y=CentrePoint.y-0.6*r*cos(PI*iM/1800);
pDC->LineTo(NewPoint);
//秒针
pDC->SelectObject(&SecondPen);
NewPoint.x=CentrePoint.x-0.3*r*sin(PI*iS/30);
NewPoint.y=CentrePoint.y+0.3*r*cos(PI*iS/30);
pDC->MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.8*r*sin(PI*iS/30);
NewPoint.y=CentrePoint.y-0.8*r*cos(PI*iS/30);
pDC->LineTo(NewPoint);
//中心柱
CRect CentreRect;
CPen CentrePen(PS_SOLID,5,RGB(255,0,0));
CBrush CentreBrush(RGB(100,100,0));
pDC->SelectObject(&CentreBrush);
pDC->SelectObject(&CentrePen);
CentreRect.left=wndRect.CenterPoint().x-8;
CentreRect.top=wndRect.CenterPoint().y-8;
CentreRect.right=wndRect.CenterPoint().x+8;
CentreRect.bottom=wndRect.CenterPoint().y+8;
pDC->Ellipse(CentreRect);
pDC->SelectObject(OldPen);
ReleaseDC(pDC);
}
CDialog::OnTimer(nIDEvent);
}void CClockDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
CDialog::OnClose();
}void CClockDlg::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMenu menu;
VERIFY(menu.LoadMenu(IDR_MENU1));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
CDialog::OnRButtonUp(nFlags, point);
}void CClockDlg::OnExit()
{
// TODO: Add your command handler code here
CClockDlg::OnOK();
}
在ontimer里只需调用一下 invalidate()即可呀.
void CWatchDlg::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();
CPoint NewPoint,CentrePoint=wndRect.CenterPoint();
CTime time;
time=CTime::GetCurrentTime();
CPaintDC dc(this);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CBitmap MemBitmap;
CBitmap *pMemBitmapBak = NULL;
MemBitmap.CreateCompatibleBitmap(&dc, wndRect.Width(), wndRect.Height());
pMemBitmapBak = MemDC.SelectObject(&MemBitmap);
//时间指针
int iH=time.GetHour();
int iM=time.GetMinute();
int iS=time.GetSecond();
int r=wndRect.Width()/2;
static int iiH,iiM,iiS;
iH=iH*60*60+iM*60+iS;
iM=iM*60+iS;
//时针,擦去前次时针
CPen *OldPen,HourPen(PS_SOLID,8,RGB(255,0,255)),OldHourPen(PS_SOLID,8,RGB(0,0,0));
OldPen=MemDC.SelectObject(&OldHourPen);
NewPoint.x=CentrePoint.x-0.1*r*sin(PI*iiH/21600);
NewPoint.y=CentrePoint.y+0.1*r*cos(PI*iiH/21600);
MemDC.MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.4*r*sin(PI*iiH/21600);
NewPoint.y=CentrePoint.y-0.4*r*cos(PI*iiH/21600);
iiH=iH;
MemDC.LineTo(NewPoint);
//分针,擦去前次分针
CPen MinutePen(PS_SOLID,6,RGB(255,255,0)),OldMinutePen(PS_SOLID,6,RGB(0,0,0));
MemDC.SelectObject(&OldMinutePen);
NewPoint.x=CentrePoint.x-0.2*r*sin(PI*iiM/1800);
NewPoint.y=CentrePoint.y+0.2*r*cos(PI*iiM/1800);
MemDC.MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.6*r*sin(PI*iiM/1800);
NewPoint.y=CentrePoint.y-0.6*r*cos(PI*iiM/1800);
iiM=iM;
MemDC.LineTo(NewPoint);
//秒针,擦去前次秒针
CPen SecondPen(PS_SOLID,2,RGB(255,0,0)),OldSecondPen(PS_SOLID,2,RGB(0,0,0));
MemDC.SelectObject(&OldSecondPen);
NewPoint.x=CentrePoint.x-0.3*r*sin(PI*iiS/30);
NewPoint.y=CentrePoint.y+0.3*r*cos(PI*iiS/30);
MemDC.MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.8*r*sin(PI*iiS/30);
NewPoint.y=CentrePoint.y-0.8*r*cos(PI*iiS/30);
iiS=iS;
MemDC.LineTo(NewPoint);
//数字日期时间
CString strTime;
strTime=time.Format("%Y年%m月%d日 %H:%M:%S %A");
MemDC.SetBkColor(RGB(0,0,0));
MemDC.SetTextColor(RGB(200,255,255));
MemDC.TextOut(wndRect.CenterPoint().x-100,wndRect.CenterPoint().y*1.35,strTime);
//时针
MemDC.SelectObject(&HourPen);
NewPoint.x=CentrePoint.x-0.1*r*sin(PI*iH/21600);
NewPoint.y=CentrePoint.y+0.1*r*cos(PI*iH/21600);
MemDC.MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.4*r*sin(PI*iH/21600);
NewPoint.y=CentrePoint.y-0.4*r*cos(PI*iH/21600);
MemDC.LineTo(NewPoint);
//分针
MemDC.SelectObject(&MinutePen);
NewPoint.x=CentrePoint.x-0.2*r*sin(PI*iM/1800);
NewPoint.y=CentrePoint.y+0.2*r*cos(PI*iM/1800);
MemDC.MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.6*r*sin(PI*iM/1800);
NewPoint.y=CentrePoint.y-0.6*r*cos(PI*iM/1800);
MemDC.LineTo(NewPoint);
//秒针
MemDC.SelectObject(&SecondPen);
NewPoint.x=CentrePoint.x-0.3*r*sin(PI*iS/30);
NewPoint.y=CentrePoint.y+0.3*r*cos(PI*iS/30);
MemDC.MoveTo(NewPoint);
NewPoint.x=CentrePoint.x+0.8*r*sin(PI*iS/30);
NewPoint.y=CentrePoint.y-0.8*r*cos(PI*iS/30);
MemDC.LineTo(NewPoint);
//中心柱
CRect CentreRect;
CPen CentrePen(PS_SOLID,5,RGB(255,0,0));
CBrush CentreBrush(RGB(100,100,0));
MemDC.SelectObject(&CentreBrush);
MemDC.SelectObject(&CentrePen);
CentreRect.left=wndRect.CenterPoint().x-8;
CentreRect.top=wndRect.CenterPoint().y-8;
CentreRect.right=wndRect.CenterPoint().x+8;
CentreRect.bottom=wndRect.CenterPoint().y+8;
MemDC.Ellipse(CentreRect);
dc.BitBlt(0, 0, wndRect.Width(),wndRect.Height(), &MemDC, 0, 0, SRCCOPY);
MemDC.SelectObject(OldPen);
}
}void CWatchDlg::OnTimer(UINT nIDEvent)
{
if ( nIDEvent == 1)
Invalidate();
}
//其他不变.