myview.cpp

来自「VC面向对象的学习教程」· C++ 代码 · 共 204 行

CPP
204
字号
// MyView.cpp : implementation of the CMyView class
//

#include "stdafx.h"
#include "My.h"

#include "MyDoc.h"
#include "MyView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
	//{{AFX_MSG_MAP(CMyView)
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_END, OnEnd)
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	ON_WM_RBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction

CMyView::CMyView()
{
	// TODO: add construction code here

}

CMyView::~CMyView()
{
}

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

void CMyView::OnDraw(CDC* pDC)
{
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CBrush brushNewRed,brushNewBlue, *pbrushOld;
	brushNewRed.CreateSolidBrush(RGB(255,0,0));
	brushNewBlue.CreateSolidBrush(RGB(0,0,255));
    pbrushOld = pDC->SelectObject(&brushNewRed);
	for(int i=0; i<pDoc->m_nBubbleCount; i++)
		if( pDoc->m_rectBubble[i].bottom > -100)
		{
			if(pDoc->m_nColor[i] == 1)
			{
				pDC->SelectObject(&brushNewRed);
			}
			else
			{
				pDC->SelectObject(&brushNewBlue);
			}
			pDC->Ellipse(pDoc->m_rectBubble[i]);
		}
	pDC->SelectObject(pbrushOld);
 
}

/////////////////////////////////////////////////////////////////////////////
// CMyView printing

BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics

#ifdef _DEBUG
void CMyView::AssertValid() const
{
	CView::AssertValid();
}

void CMyView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
	return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers

void CMyView::OnStart() 
{
	// TODO: Add your command handler code here
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_nStart = 1;
	SetTimer(1,100,NULL);
}

void CMyView::OnEnd() 
{
	// TODO: Add your command handler code here
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_nStart = 0;
	KillTimer(1);
}

void CMyView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
    if(pDoc->m_nStart == 1)
		if(pDoc->m_nBubbleCount < MAX_BUBBLE)
		{
			int r = rand()%30+10;
			CRect rect(point.x-r, point.y-r, point.x+r, point.y+r);
			pDoc->m_rectBubble[pDoc->m_nBubbleCount] = rect;
			pDoc->m_nColor[pDoc->m_nBubbleCount] = 1;
			pDoc->m_nBubbleCount++;
			InvalidateRect(rect, FALSE);
		}	
	CView::OnLButtonDown(nFlags, point);
}

void CMyView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	for(int i=0; i<pDoc->m_nBubbleCount; i++)
	{
		if(pDoc->m_rectBubble[i].bottom  > 0)
		{
			InvalidateRect(pDoc->m_rectBubble[i], TRUE);
			pDoc->m_rectBubble[i].top -= 15;
			pDoc->m_rectBubble[i].bottom -= 15;
			InvalidateRect(pDoc->m_rectBubble[i], FALSE);
		}
		else
			pDoc->m_rectBubble[i].bottom = -100;
	}

	CView::OnTimer(nIDEvent);
}

void CMyView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
    if(pDoc->m_nStart == 1)
		if(pDoc->m_nBubbleCount < MAX_BUBBLE)
		{
			int r = rand()%30+10;
			CRect rect(point.x-r, point.y-r, point.x+r, point.y+r);
			pDoc->m_rectBubble[pDoc->m_nBubbleCount] = rect;
			pDoc->m_nColor[pDoc->m_nBubbleCount] = 2;
			pDoc->m_nBubbleCount++;
			InvalidateRect(rect, FALSE);
		}	
	
	CView::OnRButtonDown(nFlags, point);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?