www.pudn.com > Cartoon.rar > CartoonView.cpp
// CartoonView.cpp : implementation of the CCartoonView class
//
#include "stdafx.h"
#include "Cartoon.h"
#include "CartoonDoc.h"
#include "CartoonView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCartoonView
IMPLEMENT_DYNCREATE(CCartoonView, CView)
BEGIN_MESSAGE_MAP(CCartoonView, CView)
//{{AFX_MSG_MAP(CCartoonView)
ON_WM_TIMER()
ON_COMMAND(ID_MOVE, OnMove)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CCartoonView construction/destruction
CCartoonView::CCartoonView()
{
// TODO: add construction code here
m_Timer=0;
//m_Timer=SetTimer(1,500,NULL);
pos.x=20;
pos.y=20;
radius=10;
cxMove=2*radius;
cyMove=2*radius;
}
CCartoonView::~CCartoonView()
{
}
BOOL CCartoonView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCartoonView drawing
void CCartoonView::OnDraw(CDC* pDC)
{
CCartoonDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCartoonView printing
BOOL CCartoonView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCartoonView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCartoonView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCartoonView diagnostics
#ifdef _DEBUG
void CCartoonView::AssertValid() const
{
CView::AssertValid();
}
void CCartoonView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCartoonDoc* CCartoonView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCartoonDoc)));
return (CCartoonDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCartoonView message handlers
void CCartoonView::OnUpdate(CView* pSender,LPARAM lHint,CObject *pHint)
{
m_Timer=SetTimer(1,100,NULL);
}
void CCartoonView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
/* InvalidateRect(new CRect(pos.x-radius,pos.y-radius,pos.x+radius,pos.y+radius));
CRect rect;
GetClientRect(&rect);
pos.Offset(cxMove,cyMove);
if(pos.x+radius>=rect.right)
{
cxMove=-cxMove;
pos.x=rect.right-radius;
}
if(pos.x-radius<=0)
{
cxMove=-cxMove;
pos.x=radius;
}
if(pos.y+radius>=rect.bottom)
{
cyMove=-cyMove;
pos.y=rect.bottom-radius;
}
if(pos.y-radius<=0)
{
cyMove=-cyMove;
pos.y=radius;
}*/
dc.SelectObject(new CBrush(HS_DIAGCROSS,RGB(0,0,0)));
dc.Ellipse(new CRect(pos.x-radius,pos.y-radius,pos.x+radius,
pos.y+radius));
CView::OnTimer(nIDEvent);
}
BOOL CCartoonView::DestroyWindow()
{
KillTimer(m_Timer);
return CCartoonView::DestroyWindow();
}
void CCartoonView::OnMove()
{
// TODO: Add your command handler code here
CClientDC dc(this);
InvalidateRect(new CRect(pos.x-radius,pos.y-radius,pos.x+radius,pos.y+radius));
CRect rect;
GetClientRect(&rect);
pos.Offset(cxMove,cyMove);
if(pos.x+radius>=rect.right)
{
cxMove=-cxMove;
pos.x=rect.right-radius;
}
if(pos.x-radius<=0)
{
cxMove=-cxMove;
pos.x=radius;
}
if(pos.y+radius>=rect.bottom)
{
cyMove=-cyMove;
pos.y=rect.bottom-radius;
}
if(pos.y-radius<=0)
{
cyMove=-cyMove;
pos.y=radius;
}
dc.SelectObject(new CBrush(HS_DIAGCROSS,RGB(0,0,0)));
dc.Ellipse(new CRect(pos.x-radius,pos.y-radius,pos.x+radius,
pos.y+radius));
/*UINT nIDEvent;
CCartoonView::OnTimer(nIDEvent);*/
}