?? clockview.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 + -