rectangleobj.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 133 行

CPP
133
字号
// RectangleObj.cpp: implementation of the CRectangleObj class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Ex090202.h"
#include "RectangleObj.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRectangleObj::CRectangleObj():m_rRect(0,0,20,20)
{
	m_eStata = DEVELOP ;
	m_nProgress = 0 ;
}

CRectangleObj::~CRectangleObj()
{

}

void CRectangleObj::Step(int rMax,int bMax)
{
	if(DIE == m_eStata)
		return ;
	else if(DEVELOP == m_eStata || DIEOUT == m_eStata )
	{
		m_nProgress ++ ;
		if( 5 == m_nProgress)
		{
			m_nProgress = 0 ;
			if(DEVELOP == m_eStata)
				m_eStata = NORMAL ;
			else if( DIEOUT == m_eStata )
				m_eStata = DIE ;
		}
		return ;
	}
	else if(NORMAL == m_eStata)
	{		
		m_rRect += CPoint(2,0);
		if(m_rRect.left == rMax )
			m_rRect -= CPoint(rMax,-2);
		m_nProgress ++ ;
	}

}

void CRectangleObj::Draw(CDC *pDC)
{
	if(DIE == m_eStata)
		return ;
	COLORREF crColor ;
	if(DEVELOP == m_eStata )
		crColor = RGB(0,0,255);
	else if( NORMAL == m_eStata )
		crColor = RGB(0,255,0);
	else if( DIEOUT == m_eStata )
		crColor = RGB(255,0,0);
	CBrush brush(crColor) ;
	pDC->SelectObject(&brush);
	pDC->Rectangle(m_rRect);
}

void CRectangleObj::DieOut()
{
	m_eStata = DIEOUT;
	m_nProgress = 0 ;
}

bool CRectangleObj::IsDie()
{
	return m_eStata == DIE ;
}

//=================================================//
CObjManage::CObjManage()
{

}

CObjManage::~CObjManage()
{
	for(int i = 0 ; i < m_Objs.GetSize() ; i++ )
	{
		delete m_Objs[i] ;
	}
}

bool CObjManage::AddAObject()
{
	CRectangleObj * pObj = new CRectangleObj ;
	if( NULL == pObj )
		return false ;
	m_Objs.Add(pObj);
	return true ;
}

bool CObjManage::DeleteAObject()
{
	//如果第一个矩形已经死了,就移除它
	if(m_Objs.GetSize() > 0 )
		if(m_Objs[0]->IsDie())
			m_Objs.RemoveAt(0);

	if(m_Objs.GetSize() == 0 )
		return false ;
	m_Objs[0]->DieOut();
	return true ;
}

void CObjManage::Draw(CDC *pDC)
{
	for(int i = 0 ; i < m_Objs.GetSize() ; i++ )
		m_Objs[i]->Draw(pDC);
}

void CObjManage::Step()
{
	for(int i = 0 ; i < m_Objs.GetSize() ; i++ )
		m_Objs[i]->Step(m_rMax,m_bMax);
}


⌨️ 快捷键说明

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