}
else
{
ar>>m_Color>>m_iLineWidth>>m_strName>>m_RotateAngle;
}
}
BOOLCComponent:
:
IsInList(inti,CObList*pNumList)
//判断数字i是否在链表pNumList中
{
POSITIONpos=pNumList->GetHeadPosition();
CObject*pObject;
int*p;
while(pos!
=NULL)
{
pObject=pNumList->GetAt(pos);
p=(int*)pObject;
if(i==(*p))
{
returnTRUE;
}
pNumList->GetNext(pos);
}
returnFALSE;
}
BOOLCComponent:
:
IsNum(CStringstr)
//判断一个字符串是否是数字,在各个派生类的CalculateName函数中
//使用,系统提供的函数atoi()将类似于"12ert"之类的串转换为12,
//这显然是不正确的,因此自己开发了这个小函数
{
intlen=str.GetLength();
charch;
for(inti=1;i<=len;i++)
{
ch=str.GetAt(i-1);
if(!
(ch<='9'&&ch>='0'))
{
returnFALSE;
}
}
returnTRUE;
}
2、电阻类的定义与实现
●电阻类的定义
#include"Component.h"//包含基类的头文件
typedefstructstruc_Resistor
{
charm_strClassType[20];
//COLORREFm_color;
//intm_iLineWidth;
BOOLm_IsSelected;
//floatm_RotateAngle;
charm_strName[100];
//charm_ReType[100];//电阻的类型
//charm_ReValue[100];//电阻的阻值
}RESISTOR;
classCResistor:
publicCComponent
{
public:
voidCopyReToStruRe(RESISTOR&Resistor);
//将电阻对象拷贝到电阻结构中
CResistor(RESISTORResistor);
voidGetNumInNameSer(CObList*pObList,CObList*pNumSer,int*pMax);
//取得已绘制的电阻名称中的数字序列
CResistor(CPoint*pt);
DECLARE_SERIAL(CResistor)
voidSerialize(CArchive&ar);
voidPrepareDrawChipDC(CDC*pDC);
voidDraw(CDC*pDC);
CStringCalculateName(CObList*pObList);
voidSetReValue(CString&ReValue);
voidSetReType(CString&ReType);
CStringGetReValue();
CStringGetReType();
CRectm_Rect;
CRectm_RectLP;
CRectTrackerm_RectTracker;
CResistor();
virtual~CResistor();
private:
CStringm_ReType;
CStringm_ReValue;
};
●电阻类的实现
CResistor:
:
CResistor()
{
//m_Rect=CRect(0,0,100,60);
m_Rect=CRect(0,0,2000,-1000);
m_RectLP=m_Rect;
m_RectTracker.m_rect=m_Rect;
m_RectTracker.m_nStyle=CRectTracker:
:
resizeOutside;
SetName("R");
}
CResistor:
:
~CResistor()
{
}
CStringCResistor:
:
GetReType()
{
returnm_ReType;
}
CStringCResistor:
:
GetReValue()
{
returnm_ReValue;
}
voidCResistor:
:
SetReType(CString&ReType)
{
m_ReType=ReType;
}
voidCResistor:
:
SetReValue(CString&ReValue)
{
m_ReValue=ReValue;
}
CStringCResistor:
:
CalculateName(CObList*pObList)
//在绘制时系统给出的默认名称,这个名称是根据当前链表中
//对象情况而定的,具体功能同VB等开发工具中拖拉控件时产生
//名称的情况
{
CObList*pNumSer=newCObList;
intmax;
charbuf[10];
GetNumInNameSer(pObList,pNumSer,&max);
//分析数字序列
for(inti=1;i<=max;i++)
{
if(IsInList(i,pNumSer))//在基类中有定义
{
}
else
{
break;
}
}
itoa(i,buf,10);
//itoa(i,buf,10);
//删除链表
//TRACE("pNumSer->Count()=%d\n",pNumSer->GetCount());
while(pNumSer->GetHeadPosition())
{
delete(int*)pNumSer->RemoveHead();
}
deletepNumSer;
return"R"+CString(buf);
}
voidCResistor:
:
Draw(CDC*pDC)
{
/*根据元器件对象的颜色和线宽属性值绘制*/
CStringname=this->GetName();
intwidth=m_Rect.Width();
intheight=m_Rect.Height();//返回的是负值;
CPen