📄 mythreadview.cpp
字号:
// MyThreadView.cpp : implementation of the CMyThreadView class
//
#include "stdafx.h"
#include "MyThread.h"
#include "MyThreadDoc.h"
#include "MyThreadView.h"
#include "Car.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WidthOfRoad 100
CCriticalSection cs;
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView
IMPLEMENT_DYNCREATE(CMyThreadView, CView)
BEGIN_MESSAGE_MAP(CMyThreadView, CView)
//{{AFX_MSG_MAP(CMyThreadView)
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()
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView construction/destruction
CMyThreadView::CMyThreadView()
{
// TODO: add construction code here
}
CMyThreadView::~CMyThreadView()
{
}
BOOL CMyThreadView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView drawing
void CMyThreadView::OnDraw(CDC* pDC)
{
CMyThreadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
DrawRoad(pDC);
CRect ClientRect;
CPoint center;
GetClientRect(&ClientRect);
center=CPoint(ClientRect.right/2,ClientRect.bottom/2);
car1.Start(CPoint(0,center.y+20));
car2.Start(CPoint(0,center.y+60));
car3.Start(CPoint(center.x-60,0),CPoint(0,10));
car4.Start(CPoint(center.x-30,0),CPoint(0,10));
pDC->SetROP2(R2_XORPEN);
pDC->Rectangle(car1.m_CarPosition.x,car1.m_CarPosition.y,car1.m_CarPosition.x+50,car1.m_CarPosition.y+20);
//pDC->Rectangle(car2.m_CarPosition.x,car2.m_CarPosition.y,car2.m_CarPosition.x+50,car2.m_CarPosition.y+20);
//pDC->Rectangle(car3.m_CarPosition.x,car3.m_CarPosition.y,car3.m_CarPosition.x+20,car3.m_CarPosition.y+50);
//pDC->Rectangle(car4.m_CarPosition.x,car4.m_CarPosition.y,car4.m_CarPosition.x+20,car4.m_CarPosition.y+50);
SetTimer(1,200,NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView printing
BOOL CMyThreadView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyThreadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyThreadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView diagnostics
#ifdef _DEBUG
void CMyThreadView::AssertValid() const
{
CView::AssertValid();
}
void CMyThreadView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyThreadDoc* CMyThreadView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyThreadDoc)));
return (CMyThreadDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView message handlers
void CMyThreadView::DrawRoad(CDC *pDC)
{
CPen newPen1;
CPen newPen2;
CPen *pOldPen;
CRect ClientRect;
CPoint center;
newPen1.CreatePen(PS_DASHDOT,0,RGB(0,0,255));
newPen2.CreatePen(PS_SOLID,5,RGB(255,255,0));
pOldPen=pDC->SelectObject(&newPen1);
GetClientRect(&ClientRect);
center=CPoint(ClientRect.right/2,ClientRect.bottom/2);
pDC->MoveTo(0,center.y);
pDC->LineTo(ClientRect.right,center.y);
pDC->MoveTo(center.x,0);
pDC->LineTo(center.x,ClientRect.bottom);
pDC->SelectObject(&newPen2);
pDC->MoveTo(0,center.y-WidthOfRoad);
pDC->LineTo(center.x-WidthOfRoad,center.y-WidthOfRoad);
pDC->MoveTo(0,center.y+WidthOfRoad);
pDC->LineTo(center.x-WidthOfRoad,center.y+WidthOfRoad);
pDC->MoveTo(center.x-WidthOfRoad,0);
pDC->LineTo(center.x-WidthOfRoad,center.y-WidthOfRoad);
pDC->MoveTo(center.x+WidthOfRoad,0);
pDC->LineTo(center.x+WidthOfRoad,center.y-WidthOfRoad);
pDC->MoveTo(ClientRect.right,center.y-WidthOfRoad);
pDC->LineTo(center.x+WidthOfRoad,center.y-WidthOfRoad);
pDC->MoveTo(ClientRect.right,center.y+WidthOfRoad);
pDC->LineTo(center.x+WidthOfRoad,center.y+WidthOfRoad);
pDC->MoveTo(center.x+WidthOfRoad,ClientRect.bottom);
pDC->LineTo(center.x+WidthOfRoad,center.y+WidthOfRoad);
pDC->MoveTo(center.x-WidthOfRoad,ClientRect.bottom);
pDC->LineTo(center.x-WidthOfRoad,center.y+WidthOfRoad);
pDC->SelectObject(pOldPen);
}
void CMyThreadView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
cs.Lock();
pDC->SetROP2(R2_XORPEN);
pDC->Rectangle(car1.m_CarOld.x,car1.m_CarOld.y,car1.m_CarOld.x+50,car1.m_CarOld.y+20);
pDC->Rectangle(car2.m_CarOld.x,car2.m_CarOld.y,car2.m_CarOld.x+50,car2.m_CarOld.y+20);
pDC->Rectangle(car3.m_CarOld.x,car3.m_CarOld.y,car3.m_CarOld.x+20,car3.m_CarOld.y+50);
pDC->Rectangle(car4.m_CarOld.x,car4.m_CarOld.y,car4.m_CarOld.x+20,car4.m_CarOld.y+50);
pDC->Rectangle(car1.m_CarPosition.x,car1.m_CarPosition.y,car1.m_CarPosition.x+50,car1.m_CarPosition.y+20);
pDC->Rectangle(car2.m_CarPosition.x,car2.m_CarPosition.y,car2.m_CarPosition.x+50,car2.m_CarPosition.y+20);
pDC->Rectangle(car3.m_CarPosition.x,car3.m_CarPosition.y,car3.m_CarPosition.x+20,car3.m_CarPosition.y+50);
pDC->Rectangle(car4.m_CarPosition.x,car4.m_CarPosition.y,car4.m_CarPosition.x+20,car4.m_CarPosition.y+50);
cs.Unlock();
CView::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -