⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chip.cpp

📁 VC面向对象的学习教程
💻 CPP
字号:
// Chip.cpp: implementation of the CChip class.
//
//////////////////////////////////////////////////////////////////////

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

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

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

IMPLEMENT_SERIAL(CChip, CObject, 1)
// 设置拼图块参数
void CChip::SetChip(int type, POINT *ppointlist, int count)
{
	m_nType	= type;
	m_nPointCount	= count;
	for(int i=0; i<count; i++)
		m_pointList[i] = ppointlist[i];
}
// 绘出拼图块
void CChip::DrawChip(CDC *pDC)
{
	CPen	penNew, *ppenOld;
	CBrush	brushNew, *pbrushOld;
	switch(m_nType)
	{
	case 1:	
		brushNew.CreateSolidBrush(RGB(127, 127, 127));
		break;
	case 2:	
		brushNew.CreateSolidBrush(RGB(255, 0, 0));
		break;
	case 3:	
		brushNew.CreateSolidBrush(RGB(0, 255, 0));
		break;
	case 4:	
		brushNew.CreateSolidBrush(RGB(0, 0, 255));
		break;
	case 5:	
		brushNew.CreateSolidBrush(RGB(127, 127, 0));
		break;
	case 6:	
		brushNew.CreateSolidBrush(RGB(127, 0, 127));
		break;
	case 7:	
		brushNew.CreateSolidBrush(RGB(0, 127, 127));
		break;
	}
	penNew.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	ppenOld		= pDC->SelectObject(&penNew);
	pbrushOld	= pDC->SelectObject(&brushNew);
	pDC->Polygon(m_pointList, m_nPointCount);
	pDC->SelectObject(ppenOld);
	pDC->SelectObject(pbrushOld);
}
// 检测一点是否在拼图块中
BOOL CChip::PtInChip(POINT point)
{
	CRgn rgn;
	int x=rgn.CreatePolygonRgn(m_pointList, m_nPointCount, ALTERNATE);
	return rgn.PtInRegion(point);
}
// 取拼图块的包含矩形
LPCRECT CChip::GetRect()
{
	static RECT rect;
	CRgn rgn;
	rgn.CreatePolygonRgn(m_pointList, m_nPointCount, ALTERNATE);
	rgn.GetRgnBox(&rect);
	rect.right++;
	rect.bottom++;
	return &rect;
}
// 旋转拼图块
void CChip::Rotation()
{
	CRect rect;
	CRgn rgn;
	rgn.CreatePolygonRgn(m_pointList, m_nPointCount, ALTERNATE);
	rgn.GetRgnBox(&rect);
	double x = rect.left+rect.Width()/2;	// 计算旋转中心
	double y = rect.top+rect.Height()/2;
	double dx, dy;
	for(int i=0; i<m_nPointCount; i++)		// 旋转各点
	{
		dx = m_pointList[i].x-x;
		dy = m_pointList[i].y-y;
		m_pointList[i].x = (int)(x+dx*0.7071-dy*0.7071);
		m_pointList[i].y = (int)(y+dx*0.7071+dy*0.7071);
	}
}
// 移动拼图块
void CChip::MoveTo(CSize offset)
{
	for(int i=0; i<m_nPointCount; i++)
		m_pointList[i] = m_pointList[i]+offset;
}
// 序列化
void CChip::Serialize(CArchive &ar)
{
	if(ar.IsStoring())
	{
		ar << m_nType;
		ar << m_nPointCount;
		for(int i=0; i<m_nPointCount; i++)
			ar << m_pointList[i];
	}
	else
	{
		ar >> m_nType;
		ar >> m_nPointCount;
		for(int i=0; i<m_nPointCount; i++)
			ar >> m_pointList[i];
	}
}

⌨️ 快捷键说明

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