📄 clockview.cpp
字号:
// clockView.cpp : implementation of the CClockView class
//
#include "stdafx.h"
#include "clock.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_COMMAND(ID_TYPE_ANALOG, OnTypeAnalog)
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
//m_nClockType = CLOCK_ANALOG;
}
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
//创建矩形对象
RECT Rect;
GetClientRect( &Rect );
// 设置起始位置
int nCenterX = Rect.right / 2;
int nCenterY = Rect.bottom / 2;
// 获取当前时间
CTime Time = CTime::GetCurrentTime();
CString strDigits;
// int i, x, y;
CSize size;
/*
// switch( m_nClockType ){
//以下为圆盘格式显示时间的代码。
// case CLOCK_ANALOG:
{
// 圆盘钟的外圈为黄色
CPen Pen( PS_SOLID, 5, RGB( 255, 255, 0 ) );
// Select the new pen into the DC and remember the pen that was selected out.
CPen *pOldPen = pDC->SelectObject( &Pen );
// Draw the clock face border with the Ellipse function.
pDC->Ellipse( 5, 5, Rect.right - 5,
Rect.bottom - 5 );
double Radians;
// 时间数字为红色
pDC->SetTextColor( RGB( 255, 0, 0 ) );
for(int i=1; i<=12; i++ ){
// 格式化时钟对象
strDigits.Format( "%d", i );
size =pDC->GetTextExtent( strDigits,
strDigits.GetLength() );
Radians = (double) i * 6.28 / 12.0;
// x = nCenterX -
// ( size.cx / 2 ) +
// (int) ( (double) ( nCenterX - 20 ) *sin( Radians ) );
// y = nCenterY -
// ( size.cy / 2 ) -
// (int) ( (double) ( nCenterY - 20 ) *cos( Radians ) );
// Draw the text.
// pDC->TextOut( x, y, strDigits );
}
// Calculate the radians for the hour hand.
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 ) );
// Select the newly-created CPen object into the DC.
pDC->SelectObject( &HourPen );
// Move to the center of the clock, then draw the hour hand line.
// pDC->MoveTo( nCenterX, nCenterY );
//pDC->LineTo(
// nCenterX + (int) ( (double) ( nCenterX / 3 ) *
// sin( Radians ) ),
// nCenterY - (int) ( (double) ( nCenterY / 3 ) *
// cos( Radians ) ) );
// Calculate the radians for the minute hand.
Radians = (double) Time.GetMinute() +
(double) Time.GetSecond() / 60.0;
Radians *= 6.28 / 60.0;
// 分针为蓝色
CPen MinutePen( PS_SOLID, 3, RGB( 0, 0, 255 ) );
// Select the newly-created CPen object into the DC.
pDC->SelectObject( &MinutePen );
// Move the to center of the clock, then draw the minute hand line.
// pDC->MoveTo( nCenterX, nCenterY );
// pDC->LineTo(
// nCenterX + (int) ( (double) (
// ( nCenterX * 2 ) / 3 ) * sin( Radians ) ),
// nCenterY - (int) ( (double) (
// ( nCenterY * 2 ) / 3 ) * cos( Radians ) ) );
// Calculate the radians for the second hand.
Radians = (double) Time.GetSecond();
Radians *= 6.28 / 60.0;
// 秒针为黑色
CPen SecondPen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
// Select the newly-created CPen object into the DC.
pDC->SelectObject( &SecondPen );
// Move the to center of the clock, then draw the second hand line.
// pDC->MoveTo( nCenterX, nCenterY );
// pDC->LineTo(
// nCenterX + (int) ( (double) (
// ( nCenterX * 4 ) / 5 ) * sin( Radians ) ),
// nCenterY - (int) ( (double) (
// ( nCenterY * 4 ) / 5 ) * cos( Radians ) ) );
// Select the old CPen object back into the DC.
pDC->SelectObject( pOldPen );
}
//break;
*/
//以下为数字格式显示时间的代码。
//case CLOCK_DIGITAL:
{
// Begin by creating the text string that represents the digital time.
strDigits.Format( "%d:%02d:%02d",
Time.GetHour(),
Time.GetMinute(),
Time.GetSecond() );
CFont Font, *pOldFont;
int nWidth = 100;
int nHeight = 140;
do{
if( Font.GetSafeHandle() != NULL )
Font.DeleteObject();
Font.CreateFont( nHeight, nWidth, 0, 0,
FW_DONTCARE, 0, 0, 0, ANSI_CHARSET,
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
"Times New Roman" );
pOldFont = pDC->SelectObject( &Font );
// Get the text extent so we can decide if
// this font is too large.
size =
pDC->GetTextExtent( strDigits,
strDigits.GetLength() );
// Select the old font back into the DC.
pDC->SelectObject( pOldFont );
// If the text extent is too wide,
// reduce the font width.
if( size.cx > Rect.right )
nWidth -= 5;
// If the text extent is too high,
// reduce the font height.
if( size.cy > Rect.bottom )
nHeight -= 5;
} while( size.cx > Rect.right ||
size.cy > Rect.bottom );
// Select the final font into the
// DC and remember the CFont object
// that's selected out.
pOldFont = pDC->SelectObject( &Font );
// Draw the text.
pDC->TextOut( nCenterX - ( size.cx / 2 ),
nCenterY - ( size.cy / 2 ),
strDigits );
// Select the old font back into the DC.
pDC->SelectObject( pOldFont );
}
//break;
}
//int CClockView::OnCreate(LPCREATESTRUCT lpCreateStruct)
//{
//保证每隔一秒时钟会更新
// if (CView::OnCreate(lpCreateStruct) == -1)
// return -1;
// Kick off the timer.
// SetTimer( 1, 1000, NULL );
// return 0;
//}
/////////////////////////////////////////////////////////////////////////////
// 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
void CClockView::OnTypeAnalog()
{
// TODO: Add your command handler code here
}
BOOL CClockView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
void CClockView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
SetTimer(1,1000,NULL);
}
void CClockView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
// TODO: Add your specialized code here and/or call the base class
CView::CalcWindowRect(lpClientRect, nAdjustType);
}
void CClockView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Invalidate(TRUE);
UpdateWindow();
CView::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -