⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clockdlg.cpp

📁 vc编写的实现圆形时钟的程序,时钟显示系统时间
💻 CPP
字号:
// ClockDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Clock.h"
#include "ClockDlg.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClockDlg dialog

CClockDlg::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_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClockDlg message handlers

BOOL CClockDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	SetTimer(1,1000,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 rcangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rc;
		GetClientRect(&rc);
		int x = (rc.Width() - cxIcon + 1) / 2;
		int y = (rc.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));
	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.0+
			(double)time.GetSecond()/3600.0;
	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/3) *sin(Radians)),
		yStart-(int)((double)(yStart/3)*cos(Radians)));
	Radians=(double)time.GetMinute()+(double)time.GetSecond()/60.0;
	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 CClockDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CClockDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	Invalidate(TRUE);
 	UpdateWindow();
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -