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

📄 clockview.cpp

📁 从系统获取当前时间
💻 CPP
字号:
// ClockView.cpp : implementation of the CClockView class
//

#include "stdafx.h"
#include "Clock.h"
#include "math.h"
#include "ClockDoc.h"
#include "ClockView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClockView

IMPLEMENT_DYNCREATE(CClockView, CView)

BEGIN_MESSAGE_MAP(CClockView, CView)
	//{{AFX_MSG_MAP(CClockView)
	ON_WM_CREATE()
	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()

/////////////////////////////////////////////////////////////////////////////
// CClockView construction/destruction

CClockView::CClockView()
{
	// TODO: add construction code here

}

CClockView::~CClockView()
{
}

BOOL CClockView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CClockView drawing

void CClockView::OnDraw(CDC* pDC)
{
	CClockDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//获取客户区
 	CString str;
	int i,x,y;
	CSize Size;
	RECT Rect;
	double Radians;	

	GetClientRect(&Rect);
	int CenterX = Rect.right/2;	//计算钟面中心位置
	int CenterY = Rect.bottom/2;
	CPen Pen(PS_SOLID,4,RGB(0,0,0));//创建一支黑色的笔 
	CPen *OldPen = pDC->SelectObject(&Pen);//设置当前画笔,并保存以前的画笔
	pDC->Ellipse(7,7,Rect.right-7,Rect.bottom-7);	//绘制钟面



	pDC->SetTextColor(RGB(0,0,0));//设置数字颜色为黑色
	for(i = 1;i <= 12;i++)
	{
		str.Format("%d",i);//格式化数字值
		Size = pDC->GetTextExtent(str,str.GetLength());
		Radians = (double)i*6.28/12.0;//计算数字的位置
		x = CenterX - (Size.cx/2) + (int)((double)(CenterX - 20)*sin(Radians));
		y = CenterY - (Size.cy/2) - (int)((double)(CenterY - 20)*cos(Radians));
		pDC->TextOut(x,y,str);	//输出数字
	}


	CTime Time = CTime::GetCurrentTime();//获取当前时间
	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(255,0,0));  //创建时钟指针画笔
	pDC->SelectObject(&HourPen);
	pDC->MoveTo(CenterX,CenterY);
	pDC->LineTo(CenterX + (int)((double)(CenterX/3)*sin(Radians)),
		        CenterY - (int)((double)(CenterY/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,255)); //创建分钟指针画笔
	pDC->SelectObject(&MinutePen);
	pDC->MoveTo(CenterX,CenterY);
	pDC->LineTo(CenterX + (int)((double)(CenterX*2)/3)*sin(Radians),
		        CenterY - (int)((double)(CenterY*2/3)*cos(Radians)));//绘制分钟指针

	Radians = (double)Time.GetSecond();
	Radians *= 6.28/60.0;  //计算秒针的角度
	CPen SecondPen(PS_SOLID,1,RGB(0,255,0)); //创建秒钟指针画笔
	pDC->SelectObject(&SecondPen);
	pDC->MoveTo(CenterX,CenterY);
	pDC->LineTo(CenterX + (int)((double)(CenterX*4)/5)*sin(Radians),
		        CenterY - (int)((double)(CenterY*4)/5*cos(Radians)));//绘制秒钟指针

	pDC->SelectObject(OldPen);//恢复原画笔
 
}

/////////////////////////////////////////////////////////////////////////////
// CClockView printing

BOOL CClockView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CClockView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CClockView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CClockView diagnostics

#ifdef _DEBUG
void CClockView::AssertValid() const
{
	CView::AssertValid();
}

void CClockView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CClockDoc* CClockView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClockDoc)));
	return (CClockDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CClockView message handlers

int CClockView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,1000,NULL);
	return 0;
}

void CClockView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
/*	switch(nIDEvent)

{

case 1:
       InvalidateRect(NULL,true);
	;

break;

case 2:
	
	;

break;

case 3:
	
	;

break;

} 
*/
	InvalidateRect(NULL,true);
	UpdateWindow();
	CView::OnTimer(nIDEvent);
}

 

⌨️ 快捷键说明

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