?? timeview.cpp
字號:
// TimeView.cpp : implementation of the CTimeView class
//
#include "stdafx.h"
#include "Time.h"
#include "TimeDoc.h"
#include "TimeView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTimeView
const int SHOW_CIRCLE_TIMER = 100;
IMPLEMENT_DYNCREATE(CTimeView, CView)
BEGIN_MESSAGE_MAP(CTimeView, CView)
//{{AFX_MSG_MAP(CTimeView)
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()
/////////////////////////////////////////////////////////////////////////////
// CTimeView construction/destruction
int CTimeView::m_iTimerCount = 0;
int CTimeView::m_iWhich = -1;
CRect CTimeView::m_rect(30,50,80,100);
CTimeView::CTimeView()
{
// TODO: add construction code here
/*
this->m_iTimerCount = 0;
this->m_rect.right = 150;
this->m_rect.bottom =200;
this->m_rect.left = 100;
this->m_rect.top = 150;
*/
}
CTimeView::~CTimeView()
{
}
BOOL CTimeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTimeView drawing
void CTimeView::OnDraw(CDC* pDC)
{
CTimeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//根據m_iWhich的值來顯示程序執行的路線
//m_iWhich = 1 代表每次時間到的時候,程序執行的是CTimeView::OnTimer()
//m_iWhich = 2 代表每次時間到的時候,程序執行的是CTimeView::MyTimerProc()
if(m_iWhich == 1)
{
pDC->TextOut(0,20,"CTimeView::OnTimer() is called!");
}
if(m_iWhich == 2)
{
pDC->TextOut(0,20,"CTimeView::MyTimerProc() is called!");
}
//通過計時器來實現圓的顏色的更換
int i;
i = m_iTimerCount % 4;
LOGBRUSH lb;
pDC->GetCurrentBrush()->GetLogBrush(&lb);
switch (i)
{
case 0:
lb.lbColor=RGB(245,167,242);
break;
case 1:
lb.lbColor=RGB(245,239,120);
break;
case 2:
lb.lbColor=RGB(103,227,81);
break;
case 3:
lb.lbColor=RGB(0,0,255);
break;
}
CBrush NewBrush1;
NewBrush1.CreateBrushIndirect(&lb);
CBrush* pOldBrush1=pDC->SelectObject(&NewBrush1);
pDC->Ellipse(this->m_rect);
pDC->SelectObject(pOldBrush1);
}
/////////////////////////////////////////////////////////////////////////////
// CTimeView printing
BOOL CTimeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTimeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTimeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTimeView diagnostics
#ifdef _DEBUG
void CTimeView::AssertValid() const
{
CView::AssertValid();
}
void CTimeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTimeDoc* CTimeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTimeDoc)));
return (CTimeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTimeView message handlers
int CTimeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//這里給出了文章中講到的兩種方法的設置計時器的方法
//并且給出了每種方法的SetTimer的各種調用方式,包括
//正確的和錯誤的
//您可以把放在correct code 注釋中的其中的某一種SetTimer的調用方式打開,
//來看程序的運行結果,都將是正確的,也就是說,這里給出了SetTimer的
//各種調用方法
/*****************************correct code**********************************/
/****************文章中講的的第一種方法********************/
/***********CTimeView::OnTimer() will be called***********/
//::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,NULL);
//this->SetTimer(SHOW_CIRCLE_TIMER,1000,NULL);
/***********CTimeView::OnTimer() will be called!**********/
/****************文章中講的的第二種方法********************/
/***********CTimeView::MyTimerProc() will be called!**********/
::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,(TIMERPROC)MyTimerProc);
//this->SetTimer(SHOW_CIRCLE_TIMER,1000,(TIMERPROC)MyTimerProc);
/***********CTimeView::MyTimerProc() will be called!**********/
/****************************correct code************************************/
//您可以把放在erro code 注釋中的其中的某一種SetTimer的調用方式打開,
//來看程序的運行結果,都將發生編譯錯誤,錯誤發生的具體原因,在文章
//中給出了詳細地解釋。
/****************************error code************************************/
//::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,MyTimerProc);
//::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,OnTimer);
//this->SetTimer(SHOW_CIRCLE_TIMER,1000,OnTimer);
//this->SetTimer(SHOW_CIRCLE_TIMER,1000,MyTimerProc);
/****************************error code***********************************/
return 0;
}
void CTimeView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_iTimerCount++;
m_iWhich = 1;
this->Invalidate();
CView::OnTimer(nIDEvent);
}
void CALLBACK CTimeView::MyTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
m_iTimerCount++;
m_iWhich = 2;
::InvalidateRect(hwnd,NULL,true);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -