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

📄 b04view.cpp

📁 Visual C++课程设计案例精编--指针式时钟
💻 CPP
字号:
// B04View.cpp : implementation of the CB04View class
//

#include "stdafx.h"
#include "B04.h"
#include "math.h"
#include "B04Doc.h"
#include "B04View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CB04View

IMPLEMENT_DYNCREATE(CB04View, CView)

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

/////////////////////////////////////////////////////////////////////////////
// CB04View construction/destruction

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

}

CB04View::~CB04View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CB04View drawing

void CB04View::OnDraw(CDC* pDC)
{
	CB04Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//获取客户区
	RECT Rect;
	GetClientRect(&Rect);
	//计算椭圆中心位置
	int CenterX = Rect.right/2;
	int CenterY = Rect.bottom/2;
	//取当前时间
	CTime Time = CTime::GetCurrentTime();

	CString str;
	int i,x,y;
	CSize size;
	//创建一支黄色的笔,用来画椭圆
	CPen Pen(PS_SOLID,5,RGB(255,255,0));
	//设置当前画笔,并记下以前的画笔
	CPen *OldPen = pDC->SelectObject(&Pen);
	//绘制钟面椭圆
	pDC->Ellipse(5,5,Rect.right-5,Rect.bottom-5);

	double Radians;
	//设置字体颜色为红色
	pDC->SetTextColor(RGB(255,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);
	}
	//计算时钟指针的夹角
	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,255,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,255));
	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);
}

/////////////////////////////////////////////////////////////////////////////
// CB04View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CB04View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CB04View message handlers

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

void CB04View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	InvalidateRect(NULL,true);
	UpdateWindow();
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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