电子元器件系统.doc
《电子元器件系统.doc》由会员分享,可在线阅读,更多相关《电子元器件系统.doc(13页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流电子元器件系统.精品文档.电子元器件系统代码分析一、 类的定义与实现1、 器件类l 器件类定义class CComponent : public CObjectpublic:static COLORREF m_fillColor;/由类的所有对象共享BOOL IsNum(CString str);BOOL IsInList(int i,CObList *pNumList);DECLARE_SERIAL(CComponent)void Serialize(CArchive& ar);void SetName(const CString &Nam
2、e);CString GetName();virtual void Draw(CDC *pDC);virtual CString CalculateName(CObList *pList);float GetRotateAngle();void SetRotateAngle(float angle);BOOL GetIsSelected();void SetIsSelected(BOOL IsSel);int GetLineWidth();void SetLineWidth(int LWidth);void SetColor(COLORREF color);COLORREF GetColor(
3、);CComponent();virtual CComponent();private:float m_RotateAngle;BOOL m_IsSelected;int m_iLineWidth;COLORREF m_Color;CString m_strName;l 器件类实现COLORREF CComponent:m_fillColor = RGB(176,176,176);/在文件范围内初始化CComponent:CComponent()m_RotateAngle = 0;m_IsSelected = FALSE;/m_iLineWidth = 4;m_iLineWidth = 40;
4、m_Color = RGB(0,0,255);CComponent:CComponent()COLORREF CComponent:GetColor()return m_Color;void CComponent:SetColor(COLORREF color)m_Color = color;void CComponent:SetLineWidth(int LWidth)m_iLineWidth = LWidth;int CComponent:GetLineWidth()return m_iLineWidth;void CComponent:SetIsSelected(BOOL IsSel)m
5、_IsSelected = IsSel;BOOL CComponent:GetIsSelected()return m_IsSelected;void CComponent:SetRotateAngle(float angle)m_RotateAngle = angle;float CComponent:GetRotateAngle()return m_RotateAngle;CString CComponent:CalculateName(CObList *pObList)return 器件;void CComponent:Draw(CDC *pDC)CString CComponent:G
6、etName()return m_strName;void CComponent:SetName(const CString &Name)m_strName = Name;void CComponent:Serialize(CArchive &ar)if (ar.IsStoring()arm_Colorm_iLineWidthm_strNamem_Colorm_iLineWidthm_strNamem_RotateAngle;BOOL CComponent:IsInList(int i, CObList *pNumList)/判断数字i是否在链表pNumList中POSITION pos =
7、pNumList-GetHeadPosition();CObject *pObject;int *p;while(pos != NULL)pObject = pNumList-GetAt(pos);p = (int *)pObject;if (i = (*p)return TRUE;pNumList-GetNext(pos);return FALSE;BOOL CComponent:IsNum(CString str)/判断一个字符串是否是数字,在各个派生类的CalculateName函数中/使用,系统提供的函数atoi()将类似于12ert之类的串转换为12,/这显然是不正确的,因此自己开发
8、了这个小函数int len = str.GetLength();char ch;for (int i = 1; i= len; i+)ch = str.GetAt(i-1);if (!(ch=0)return FALSE;return TRUE;2、 电阻类的定义与实现l 电阻类的定义#include Component.h/包含基类的头文件typedef struct struc_Resistorchar m_strClassType20;/COLORREF m_color;/int m_iLineWidth;BOOL m_IsSelected;/float m_RotateAngle;ch
9、ar m_strName100;/char m_ReType100;/电阻的类型/char m_ReValue100;/电阻的阻值RESISTOR;class CResistor : public CComponent public:void CopyReToStruRe(RESISTOR &Resistor);/将电阻对象拷贝到电阻结构中CResistor(RESISTOR Resistor);void GetNumInNameSer(CObList *pObList,CObList *pNumSer,int *pMax);/取得已绘制的电阻名称中的数字序列CResistor(CPoint
10、*pt);DECLARE_SERIAL(CResistor)void Serialize(CArchive& ar);void PrepareDrawChipDC(CDC *pDC);void Draw(CDC *pDC);CString CalculateName(CObList *pObList);void SetReValue(CString &ReValue);void SetReType(CString &ReType);CString GetReValue();CString GetReType();CRect m_Rect;CRect m_RectLP;CRectTracker
11、m_RectTracker;CResistor();virtual CResistor();private:CString m_ReType;CString m_ReValue;l 电阻类的实现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:CR
12、esistor()CString CResistor:GetReType()return m_ReType;CString CResistor:GetReValue()return m_ReValue;void CResistor:SetReType(CString &ReType)m_ReType = ReType;void CResistor:SetReValue(CString &ReValue)m_ReValue = ReValue;CString CResistor:CalculateName(CObList *pObList)/在绘制时系统给出的默认名称,这个名称是根据当前链表中/
13、对象情况而定的,具体功能同VB等开发工具中拖拉控件时产生/名称的情况CObList *pNumSer = new CObList;int max;char buf10;GetNumInNameSer(pObList,pNumSer,&max);/分析数字序列for (int i=1;iCount() = %dn, pNumSer-GetCount();while (pNumSer-GetHeadPosition()delete (int *)pNumSer-RemoveHead();delete pNumSer;return R+ CString(buf);void CResistor:Dra
14、w(CDC *pDC)/*根据元器件对象的颜色和线宽属性值绘制*/CString name = this-GetName();int width = m_Rect.Width();int height = m_Rect.Height();/返回的是负值;CPen newpen(PS_SOLID,1,RGB(0,0,255);CPen *pOldPen = pDC-SelectObject(&newpen);pDC-MoveTo(m_Rect.left,(int)(m_Rect.top+height*0.5);pDC-LineTo(int)(m_Rect.left +width*0.3),(in
15、t)(m_Rect.top + height*0.5);pDC-Rectangle (int)(m_Rect.left + width * 0.3 ),(int)(m_Rect.top + height * 0.25), (int)(m_Rect.left + width * 0.7 ),(int)( m_Rect.top + height * 0.75);pDC-MoveTo (int)(m_Rect.left + width * 0.7) ,(int)(m_Rect.top + height * 0.5);pDC-LineTo(int)(m_Rect.left + width) ,(int
16、)(m_Rect.top + height * 0.5);/pDC-SetBkMode(TRANSPARENT);pDC-TextOut(int)(m_Rect.left + width * 0.4),(int)(m_Rect.top + height*0.75),name);pDC-SelectObject(pOldPen);void CResistor:PrepareDrawChipDC(CDC *pDC)void CResistor:Serialize(CArchive &ar)CComponent:Serialize(ar); /基类的序列化函数/需要序列化m_RectLPif (ar
17、.IsStoring()arm_Rectm_RectLPm_RectTracker.m_nStylem_ReTypem_ReValue;/m_Rectm_RectLPm_RectTracker.m_nStylem_ReTypem_ReValue;/m_RectTracker.m_rectCResistor:CResistor(CPoint *pt)/*由于需要把新生成的元器件对象的m_Rect左上角坐标设置为*/*鼠标点按处的坐标转换成逻辑坐标后的值,并且初始化m_Rect的右下角坐标*/*所以将这一系列的初始化操作加到了CResistor(CPoint *pt)构造函数中*/m_Rect.l
18、eft = pt-x;m_Rect.top = pt-y;/m_Rect.right = m_Rect.left + 100;/m_Rect.bottom = m_Rect.top + 60;m_Rect.right = m_Rect.left + 2000;m_Rect.bottom = m_Rect.top - 1000;m_RectLP = m_Rect;m_RectTracker.m_rect = m_Rect;m_RectTracker.m_nStyle = CRectTracker:resizeOutside;void CResistor:GetNumInNameSer(CObLi
19、st *pObList,CObList *pNumSer,int *pMax)/取得链表中电阻名称是以R或r开头,后面为数字的名称中的数字序列/其中pNumSer存放数字序列,pMax存放的是数字中的最大值*pMax = 0;POSITION pos = pObList-GetHeadPosition();CObject *pObject;CResistor *pResistor;CString strName;CString strLastName;while (pos != NULL)pObject = pObList-GetAt(pos);if (pObject-IsKindOf(RUN
20、TIME_CLASS(CResistor)pResistor = (CResistor*)pObject;strName = pResistor-GetName();if (strName.GetAt(0) = R) | (strName.GetAt(0) = r)/如果名称的第一个字母是R或rstrLastName = strName.Mid(1);/判断余下的字符是否数字if (IsNum(strLastName)/余下的字符全部都是数字int i;i = atoi(strLastName);int *p = new int(i);pNumSer-AddHead(CObject *)p);
21、if (i(*pMax)*pMax = i;else/余下的字符不全是数字/忽略不记else/名称的第一个字母不是R或relsepObList-GetNext(pos);CResistor:CResistor(RESISTOR Resistor)SetName(Resistor.m_strName);SetIsSelected(Resistor.m_IsSelected);m_Rect = CRect(1000,-1000,3000,-2000);m_RectLP = m_Rect;m_RectTracker.m_rect = m_Rect;m_RectTracker.m_nStyle = C
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 电子元器件 系统
限制150内