ex20view.cpp
来自「一些VC++的经典实例」· C++ 代码 · 共 176 行
CPP
176 行
// ex20View.cpp : implementation of the CEx20View class
//
#include "stdafx.h"
#include "ex20.h"
#include "ex20Doc.h"
#include "ex20View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx20View
IMPLEMENT_DYNCREATE(CEx20View, CView)
BEGIN_MESSAGE_MAP(CEx20View, CView)
//{{AFX_MSG_MAP(CEx20View)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
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()
/////////////////////////////////////////////////////////////////////////////
// CEx20View construction/destruction
CEx20View::CEx20View()
{
// TODO: add construction code here
m_Ball.top=200;
m_Ball.bottom=m_Ball.top+40;
m_Ball.left=300;
m_Ball.right=m_Ball.left+40;
m_CurPos.x=(m_Ball.left+m_Ball.right)/2;
m_CurPos.y=(m_Ball.top+m_Ball.bottom)/2;
}
CEx20View::~CEx20View()
{
}
BOOL CEx20View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx20View drawing
void CEx20View::OnDraw(CDC* pDC)
{
CEx20Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush BlueBrush;
BlueBrush.CreateSolidBrush(RGB(0,0,255));
pDC->SelectObject(&BlueBrush);
pDC->Ellipse(&m_Ball);
pDC->TextOut(0,0,"点击鼠标左键引导小球移动");
pDC->TextOut(0,30,"点击鼠标右键停止小球移动");
}
/////////////////////////////////////////////////////////////////////////////
// CEx20View printing
BOOL CEx20View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx20View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx20View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx20View diagnostics
#ifdef _DEBUG
void CEx20View::AssertValid() const
{
CView::AssertValid();
}
void CEx20View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEx20Doc* CEx20View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx20Doc)));
return (CEx20Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx20View message handlers
void CEx20View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDocument *pDC=GetDocument();
if(!m_Ball.PtInRect(point))
{
m_MousePos=point;
pDC->UpdateAllViews(NULL);
m_nTimer=SetTimer(1,50,NULL);
}
CView::OnLButtonDown(nFlags, point);
}
void CEx20View::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDocument *pDC=GetDocument();
KillTimer(m_nTimer);
pDC->UpdateAllViews(NULL);
CView::OnRButtonDown(nFlags, point);
}
void CEx20View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDocument *pDC=GetDocument();
CRect rect;
GetClientRect(&rect);
m_CurPos.y+=sign(m_MousePos.y-m_CurPos.y);
m_CurPos.x+=sign(m_MousePos.x-m_CurPos.x);
if((m_Ball.left=m_CurPos.x-20)<rect.left)
m_Ball.left=rect.left;
if((m_Ball.right=m_CurPos.x+20)>rect.right)
m_Ball.right=rect.right;
if((m_Ball.top=m_CurPos.y-20)<rect.top)
m_Ball.top=rect.top;
if((m_Ball.bottom=m_CurPos.y+20)>rect.bottom)
m_Ball.bottom=rect.bottom;
pDC->UpdateAllViews(NULL);
CView::OnTimer(nIDEvent);
}
int CEx20View::sign(int a)
{
if(a>0)
return 1;
if(a<0)
return -1;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?