blob.cpp

来自「本光盘包含的是《实用Visual C++ 6.0教程》一书中所有程序的代码」· C++ 代码 · 共 74 行

CPP
74
字号
//**include the standard header
#include "stdafx.h"
#include "blob.h"

//**Add the implementation for serialization
IMPLEMENT_SERIAL(CBlob,CObject,1)


//**Implement the default constructor
CBlob::CBlob()
{
}

//**Implement the position constructor
CBlob::CBlob(CPoint ptPosition)
{
	//**Set the random seed
	srand(GetTickCount());

	//**Set the position to the specified position
	m_ptPosition=ptPosition;


	//**Set the attributes to random values
	m_crColor=RGB(rand()%255,rand()%255,rand()%255);
	m_nSize=10+rand()%30;
	m_nShape=rand();
}

void CBlob::Draw(CDC *pDC)
{
	//**Create and select a colored brush
	CBrush brDraw(m_crColor);
	CBrush *pOldBrush=pDC->SelectObject(&brDraw);
	CPen *pOldPen=
		(CPen *) pDC->SelectStockObject(NULL_PEN);
	//**See the random generator to the shape
	srand(m_nShape);
	for (int n=0;n<3;n++)
	{
		//**Set the blob position and random shift
		CPoint ptBlob(m_ptPosition);
		ptBlob+=CPoint(rand()%m_nSize,rand()%m_nSize);

		//**Create and draw a rectangle
		CRect rcBlob(ptBlob,ptBlob);
		rcBlob.InflateRect(m_nSize,m_nSize);
		pDC->Ellipse(rcBlob);
	}

	//**Reselect the GDI Objects
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
}

void CBlob::Serialize(CArchive& ar)
{
	CObject::Serialize(ar);
	if (ar.IsStoring())
	{
		ar<<m_ptPosition;
		ar<<m_crColor;
		ar<<m_nSize;
		ar<<m_nShape;
	}
	else
	{
		ar>>m_ptPosition;
		ar>>m_crColor;
		ar>>m_nSize;
		ar>>m_nShape;
	}
}

⌨️ 快捷键说明

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