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

📄 myclockview.cpp

📁 本例子可以让初学者更好的理解MFC如何的运作
💻 CPP
字号:
// myclockView.cpp : implementation of the CMyclockView class
//

#include "stdafx.h"
#include "myclock.h"
#include "math.h"
#include "myclockDoc.h"
#include "myclockView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyclockView

IMPLEMENT_DYNCREATE(CMyclockView, CView)

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

/////////////////////////////////////////////////////////////////////////////
// CMyclockView construction/destruction

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

}

CMyclockView::~CMyclockView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyclockView drawing

void CMyclockView::OnDraw(CDC* pDC)
{
	CMyclockDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	RECT Rect;
	GetClientRect(&Rect);
	int CenterX=Rect.right/2,CenterY=Rect.bottom/2;
	CTime Time=CTime::GetCurrentTime();
	CString str;
	int x,y;
	CSize size;
	CPen Pen(PS_SOLID,4,RGB(255,255,0));
	CPen *OldPen=pDC->SelectObject(&Pen);
	pDC->Ellipse((Rect.right-Rect.bottom)/2,5,Rect.right-(Rect.right-Rect.bottom)/2,Rect.bottom-5);
	double Radius;
	pDC->SetTextColor(RGB(255,0,0));
	for(int i=1;i<=12;i++)
	{
		str.Format("%d",i);
		size=pDC->GetTextExtent(str,str.GetLength());
		Radius=(double)i*2*3.14/12;
		x=CenterX-(size.cx/2)+(int)((double)(CenterX-(Rect.right-Rect.bottom)/2-20)*sin(Radius));
		y=CenterY-(size.cy/2)-(int)((double)(CenterY-20)*cos(Radius));
		pDC->TextOut(x,y,str);
	}
	Radius=(double)Time.GetHour()+(double)Time.GetMinute()/60.0+(double)Time.GetSecond()/3600.0;
	Radius=Radius*2*3.14/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(Radius)),CenterY-(int)((double)(CenterY/3)*cos(Radius)));

	Radius=(double)Time.GetMinute()+(double)Time.GetSecond()/60.0;
	Radius=Radius*2*3.14/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(Radius)),CenterY-(int)((double)(CenterY*2/3)*cos(Radius)));

	Radius=(double)Time.GetSecond();
	Radius=Radius*2*3.14/60.0;
	CPen SecondPen(PS_SOLID,2,RGB(255,0,0));
	pDC->SelectObject(SecondPen);
	pDC->MoveTo(CenterX,CenterY);
	pDC->LineTo(CenterX+(int)((double)(CenterX-(Rect.right-Rect.bottom)/2-20)*sin(Radius)),CenterY-(int)((double)(CenterY-20)*cos(Radius)));


}

/////////////////////////////////////////////////////////////////////////////
// CMyclockView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyclockView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyclockView message handlers

void CMyclockView::OnTimer(UINT nIDEvent) 
{
	InvalidateRect(NULL,true);
	UpdateWindow();
	
	CView::OnTimer(nIDEvent);
}

int CMyclockView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	SetTimer(1,1000,NULL);
	
	return 0;
}

⌨️ 快捷键说明

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