modart.cpp

来自「学习VC的一些例子」· C++ 代码 · 共 175 行

CPP
175
字号
// ModArt.cpp: implementation of the CModArt class.
//
//////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include <time.h>

#include "stdafx.h"
#include "Line.h"
#include "ModArt.h"

//const COLORREF CModArt::m_crColor[8] = {
//	RGB(  0,   0,   0),
//	/RGB(  0,   0, 255),
//	RGB(  0, 255,   0),
//	RGB(  0, 255, 255),
//	RGB(255,   0,   0),
//	RGB(255,   0, 255),
//	RGB(255, 255,   0),
//	RGB(255, 255, 255)
//};/

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

CModArt::CModArt()
{
	srand((unsigned) time(NULL));
}

CModArt::~CModArt()
{

}

void CModArt::SetRect(CRect rDrawArea)
{
	m_rDrawArea = rDrawArea;
}

void CModArt::NewLine()
{
	int lNumLines;
	int lCurLine;
//	int nCurColor;
	UINT nCurWidth;
	CPoint pTo;
	CPoint pFrom;

	// Nonmalize the rectangle before determining the width and height
	m_rDrawArea.NormalizeRect();
	// get the area width and height
	int lWidth  = m_rDrawArea.Width();
	int lHeight = m_rDrawArea.Height();

//	COLORREF m_crColor[8] = {
//		RGB(  0,   0,   0),
//		RGB(  0,   0, 255),
//		RGB(  0, 255,   0),
//		RGB(  0, 255, 255),
//		RGB(255,   0,   0),
//		RGB(255,   0, 255),
//		RGB(255, 255,   0),
//		RGB(255, 255, 255)
//	};

	// Determine the number of parts to this squiggle
	lNumLines = rand() % 200;
	// Are there any partS to this Squiggle?
	if (lNumLines > 0)
	{
		// Determine the color
//		nCurColor = rand() % 8;
		int cRed   = rand() % 256;
		int cBlue  = rand() % 256;
		int cGreen = rand() % 256;
		// Determine the penwidth
		nCurWidth = (rand() % 8) + 1;
		// Determine the starting point for the Squiggle
		pFrom.x = (rand() % lWidth)  + m_rDrawArea.left;
		pFrom.y = (rand() % lHeight) + m_rDrawArea.top;
		// Loop through the number of segments
		for (lCurLine = 0; lCurLine < lNumLines; lCurLine++)
		{
			// Determine the end point of the segment
			pTo.x = ((rand() % 20) - 10) + pFrom.x;
			pTo.y = ((rand() % 20) - 10) + pFrom.y;
			// Create a new CLine object
			CLine *pLine = new CLine(pFrom, pTo, RGB(cRed, cGreen, cBlue),
				nCurWidth);

			try
			{
				// Add the new line to the object array
				m_oaLines.Add(pLine);
			}
			// Did we run into a memony exception?
			catch (CMemoryException* perr)
			{
				// Display a message for the user, giving him the
				// bad news
				AfxMessageBox("out of memory", MB_ICONSTOP | MB_OK);
				// Did we create a line object?
				if (pLine)
				{
					// Delete it
					delete pLine;
					pLine = NULL;
				}
				// Delete the exception object
				perr->Delete();
			}
			// Set the Starting point to the end point
			pFrom = pTo;
		}
	}
}

void CModArt::NewDrawing()
{
	int lNumLines;
	int lCurLine;
	
	// Determine how many lines to create
	lNumLines = rand() % 100;
	// Are there any lines to create?
	if (lNumLines > 0)
	{
		// Loop through the number of lines
		for (lCurLine = 0; lCurLine < lNumLines; lCurLine++)
		{
			// Create the new line
			NewLine();
		}
	}
}

void CModArt::Draw(CDC *pDC)
{
	// Get the number of lines in the object array
	int liCount = m_oaLines.GetSize();
	int liPos;
	
	// Are there any objects in the array?
	if (liCount)
	{
		// Loop through the array, drawing each object
		for (liPos = 0; liPos < liCount; liPos++)
			((CLine *) m_oaLines[liPos])->Draw(pDC);
	}
}

void CModArt::Serialize(CArchive &ar)
{
	m_oaLines.Serialize(ar);
}

void CModArt::ClearDrawing()
{
	// Get the number of lines in the object array
	int liCount = m_oaLines.GetSize();
	int liPos;

	// Are there any objects in the array?
	if (liCount)
	{
		// Loop through the array, deleting each object
		for (liPos = 0; liPos < liCount; liPos++)
			delete m_oaLines[liPos];
		// Reset the array
		m_oaLines.RemoveAll();
	}
}

⌨️ 快捷键说明

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