📄 drivecarview.cpp
字号:
// drivecarView.cpp : implementation of the CDrivecarView class
//
#include "stdafx.h"
#include "drivecar.h"
#include "drivecarDoc.h"
#include "drivecarView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrivecarView
IMPLEMENT_DYNCREATE(CDrivecarView, CView)
BEGIN_MESSAGE_MAP(CDrivecarView, CView)
//{{AFX_MSG_MAP(CDrivecarView)
ON_WM_PAINT()
ON_WM_TIMER()
ON_COMMAND(ID_BEGIN, OnBegin)
ON_WM_KEYDOWN()
ON_COMMAND(ID_RESET, OnReset)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDrivecarView construction/destruction
CDrivecarView::CDrivecarView()
{
// TODO: add construction code here
m_rect=CRect(160,350,180,370);
m_MeInfo=CRect(250,200,450,280);
m_YouInfo=CRect(280,250,450,330);
}
CDrivecarView::~CDrivecarView()
{
}
BOOL CDrivecarView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrivecarView drawing
void CDrivecarView::OnDraw(CDC* pDC)
{
CDrivecarDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CDrivecarView printing
BOOL CDrivecarView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrivecarView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrivecarView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrivecarView diagnostics
#ifdef _DEBUG
void CDrivecarView::AssertValid() const
{
CView::AssertValid();
}
void CDrivecarView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrivecarDoc* CDrivecarView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrivecarDoc)));
return (CDrivecarDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrivecarView message handlers
void CDrivecarView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CBrush pbrushNew,*pbrushOld;
pbrushNew.CreateHatchBrush(HS_DIAGCROSS,RGB(0,0,255));
pbrushOld=dc.SelectObject(&pbrushNew);
dc.Rectangle(100,100,600,400);
dc.SelectObject(pbrushOld);
dc.Rectangle(140,140,560,360);
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
CRect rect;
GetClientRect(&rect);
m_You.ShowYou(&dc,&MemDC,rect);
m_Me.ShowMe(&dc,&MemDC,rect);
CString s;
s.Format("我方所走圈数:%d",m_Me.m_MLoop);
dc.TextOut(m_MeInfo.left,m_MeInfo.top,s);
s.Format("对方所走圈数:%d",m_You.m_YLoop);
dc.TextOut(m_YouInfo.left,m_YouInfo.top,s);
// Do not call CView::OnPaint() for painting messages
}
void CDrivecarView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
InvalidateRect(m_You.GetYou());
if(!CatchUp(m_Me.GetMe(),m_You.GetYou()))
m_You.ChangePos();
else
{
if(m_Me.m_MLoop==m_You.m_YLoop)
KillTimer(1);
MessageBox("I have caught you!");
}
InvalidateRect(m_You.GetYou(),false);
if(m_rect.PtInRect(m_You.m_pointYou))
{
InvalidateRect(m_YouInfo);
m_You.m_YLoop++;
InvalidateRect(m_YouInfo);
}
CView::OnTimer(nIDEvent);
}
void CDrivecarView::OnBegin()
{
// TODO: Add your command handler code here
SetTimer(1,100,NULL);
}
void CDrivecarView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_LEFT)
{
InvalidateRect(m_Me.GetMe());
m_Me.ChangePos(0);
InvalidateRect(m_Me.GetMe(),FALSE);
}
else if(nChar==VK_RIGHT)
{
InvalidateRect(m_Me.GetMe());
m_Me.ChangePos(1);
InvalidateRect(m_Me.GetMe(),FALSE);
}
else if(nChar==VK_UP)
{
InvalidateRect(m_Me.GetMe());
m_Me.ChangePos(2);
InvalidateRect(m_Me.GetMe(),FALSE);
}
else if(nChar==VK_DOWN)
{
InvalidateRect(m_Me.GetMe());
m_Me.ChangePos(3);
InvalidateRect(m_Me.GetMe(),FALSE);
}
if(m_rect.PtInRect(m_Me.m_pointMe))
{
InvalidateRect(m_MeInfo);
m_Me.m_MLoop++;
InvalidateRect(m_MeInfo);
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CDrivecarView::CatchUp(CRect &body1,CRect &body2)
{
return body1.PtInRect(body2.TopLeft());
}
void CDrivecarView::OnReset()
{
// TODO: Add your command handler code here
KillTimer(1);
m_Me.Reset();
m_You.Reset();
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -