电子元器件系统_第1页
电子元器件系统_第2页
电子元器件系统_第3页
电子元器件系统_第4页
电子元器件系统_第5页
已阅读5页,还剩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 &Name);CString GetName();virtual vo

2、id 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();CComponent();virtual CCompone

3、nt();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;m_Color = RGB(0,0,255);CCompone

4、nt: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_IsSelected = IsSel;BOOL CCompo

5、nent: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:GetName()return m_str

6、Name;void CComponent:SetName(const CString &Name)m_strName = Name;void CComponent:Serialize(CArchive &ar)if (ar.IsStoring()ar<<m_Color<<m_iLineWidth<<m_strName<<m_RotateAngle;elsear>>m_Color>>m_iLineWidth>>m_strName>>m_RotateAngle;BOOL CCompone

7、nt:IsInList(int i, CObList *pNumList)/判断数字i是否在链表pNumList中POSITION pos = 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)/判断一个

8、字符串是否是数字,在各个派生类的CalculateName函数中/使用,系统提供的函数atoi()将类似于"12ert"之类的串转换为12,/这显然是不正确的,因此自己开发了这个小函数int len = str.GetLength();char ch;for (int i = 1; i<= len; i+)ch = str.GetAt(i-1);if (!(ch<='9' && ch>='0')return FALSE;return TRUE;2、 电阻类的定义与实现l 电阻类的定义#include &quo

9、t;Component.h"/包含基类的头文件typedef struct struc_Resistorchar m_strClassType20;/COLORREF m_color;/int m_iLineWidth;BOOL m_IsSelected;/float m_RotateAngle;char m_strName100;/char m_ReType100;/电阻的类型/char m_ReValue100;/电阻的阻值RESISTOR;class CResistor : public CComponent public:void CopyReToStruRe(RESISTO

10、R &Resistor);/将电阻对象拷贝到电阻结构中CResistor(RESISTOR Resistor);void GetNumInNameSer(CObList *pObList,CObList *pNumSer,int *pMax);/取得已绘制的电阻名称中的数字序列CResistor(CPoint *pt);DECLARE_SERIAL(CResistor)void Serialize(CArchive& ar);void PrepareDrawChipDC(CDC *pDC);void Draw(CDC *pDC);CString CalculateName(CO

11、bList *pObList);void SetReValue(CString &ReValue);void SetReType(CString &ReType);CString GetReValue();CString GetReType();CRect m_Rect;CRect m_RectLP;CRectTracker m_RectTracker;CResistor();virtual CResistor();private:CString m_ReType;CString m_ReValue;l 电阻类的实现CResistor:CResistor()/m_Rect =

12、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()CString CResistor:GetReType()return m_ReType;CString CResistor:GetReValue()return m_ReValue;void CResistor:

13、SetReType(CString &ReType)m_ReType = ReType;void CResistor:SetReValue(CString &ReValue)m_ReValue = ReValue;CString CResistor:CalculateName(CObList *pObList)/在绘制时系统给出的默认名称,这个名称是根据当前链表中/对象情况而定的,具体功能同VB等开发工具中拖拉控件时产生/名称的情况CObList *pNumSer = new CObList;int max;char buf10;GetNumInNameSer(pObList,

14、pNumSer,&max);/分析数字序列for (int i=1;i<=max;i+)if (IsInList(i,pNumSer)/在基类中有定义elsebreak;itoa(i,buf,10);/itoa(i,buf,10);/删除链表/TRACE("pNumSer->Count() = %dn", pNumSer->GetCount();while (pNumSer->GetHeadPosition()delete (int *)pNumSer->RemoveHead();delete pNumSer;return "

15、R"+ CString(buf);void CResistor:Draw(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

16、+height*0.5);pDC->LineTo(int)(m_Rect.left +width*0.3),(int)(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

17、_Rect.top + height * 0.5);pDC->LineTo(int)(m_Rect.left + width) ,(int)(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 CRes

18、istor:Serialize(CArchive &ar)CComponent:Serialize(ar); /基类的序列化函数/需要序列化m_RectLPif (ar.IsStoring()ar<<m_Rect<<m_RectLP<<m_RectTracker.m_nStyle<<m_ReType<<m_ReValue;/<<m_RectTracker.m_rectelsear>>m_Rect>>m_RectLP>>m_RectTracker.m_nStyle>>m

19、_ReType>>m_ReValue;/>>m_RectTracker.m_rectCResistor:CResistor(CPoint *pt)/*由于需要把新生成的元器件对象的m_Rect左上角坐标设置为*/*鼠标点按处的坐标转换成逻辑坐标后的值,并且初始化m_Rect的右下角坐标*/*所以将这一系列的初始化操作加到了CResistor(CPoint *pt)构造函数中*/m_Rect.left = pt->x;m_Rect.top = pt->y;/m_Rect.right = m_Rect.left + 100;/m_Rect.bottom = m_

20、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(CObList *pObList,CObList *pNumSer,int *pMax)/取得链表中电阻名称是以R或r开头,后面为数字的名称中的数字序列/其中pNumSer存

21、放数字序列,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(RUNTIME_CLASS(CResistor)pResistor = (CResistor*)pObject;strName = pResist

22、or->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);if (i>(*pMax)*pMax = i;else/余下的字符不全是数字/

23、忽略不记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 = CRectTracker:resizeOutside;void CRe

24、sistor:CopyReToStruRe(RESISTOR &Resistor)/*把对象的数据赋值给结构,在复制功能时用到*/lstrcpy(Resistor.m_strClassType, "Resistor");/字符串的复制 Resistor.m_IsSelected = this->GetIsSelected();lstrcpy(Resistor.m_strName, this->GetName();二、 文档类函数1、 串行化函数void CDrawChip1Doc:Serialize(CArchive& ar)if (ar.IsS

25、toring()/ TODO: add storing code hereelse/ TODO: add loading code herem_ChipObList.Serialize(ar);2、 销毁对象函数void CDrawChip1Doc:DeleteContents() while(m_ChipObList.GetHeadPosition()delete m_ChipObList.RemoveHead();/ TODO: Add your specialized code here and/or call the base classCDocument:DeleteContents

26、();三、 视图类主要函数1、绘图函数void CDrawChip1View:OnDraw(CDC* pDC)CDrawChip1Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data herePOSITION pos = pDoc->m_ChipObList.GetHeadPosition();CObject *pObject;CResistor *pResistor;CComponent *pComponent;CBrush fillBrush(RGB(255,0,255),

27、*oldBrush;oldBrush = pDC->SelectObject(&fillBrush);while (pos != NULL)pObject = pDoc->m_ChipObList.GetAt(pos);pComponent = (CComponent *)pObject;if (pObject->IsKindOf(RUNTIME_CLASS(CResistor)pResistor = (CResistor *)pObject;pResistor->m_RectTracker.m_rect = pResistor->m_RectLP;pDC

28、->LPtoDP(pResistor->m_RectTracker.m_rect);if (pResistor->GetIsSelected()pResistor->m_RectTracker.Draw(pDC);/画框/pResistor->Draw(pDC);/画电阻pComponent->Draw(pDC);/画电阻 pDoc->m_ChipObList.GetNext(pos);pDC->SelectObject(oldBrush);3、 初始化视图类函数void CDrawChip1View:OnInitialUpdate()CScro

29、llView:OnInitialUpdate();CSize sizeTotal(30000,30000);/这里的值是逻辑坐标,30cm,30cmCSize sizePage(sizeTotal.cx/5,sizeTotal.cy/6);CSize sizeLine(sizeTotal.cx/50,sizeTotal.cy/50);/MM_HIMETRIC映射模式的y轴方向是向下减的SetScrollSizes(MM_HIMETRIC,sizeTotal,sizePage,sizeLine);4、 鼠标事件函数void CDrawChip1View:OnLButtonDown(UINT nF

30、lags, CPoint point) /point参数代表光标的坐标位置(设备坐标)/nFlags鼠标键与键盘的组合标志,有MK_LBUTTON,MK_RBUTTON,MK_MBUTTON,MK_SHIFT,MK_CONTROLCClientDC dc(this);OnPrepareDC(&dc);/lpPoint存放当前鼠标点按点的逻辑坐标CPoint *lpPoint = new CPoint(point); lpPoint->x = point.x;lpPoint->y = point.y;dc.DPtoLP(lpPoint);/设备坐标转换为逻辑坐标int x =

31、 lpPoint->x;int y = lpPoint->y;CDrawChip1Doc *pDoc = GetDocument();CObList *pObList = &(pDoc->m_ChipObList);CObList *pTempObList = &(pDoc->m_ChipObList);CRect rectInvalidate;/存放更新的区域CString strName;CSize Textsize;/记录元器件对象名称字符的宽度和高度if (m_bResistor)/当前处于绘制电阻状态CResistor *pResistor =

32、 new CResistor(lpPoint);AddChipToList(pResistor,pObList);strName = pResistor->GetName();:GetTextExtentPoint32(dc, (const char *)&strName, strName.GetLength(), &Textsize);/计算strName的大小rectInvalidate = pResistor->m_Rect;rectInvalidate.bottom = rectInvalidate.bottom - Textsize.cy;rectInva

33、lidate.right = rectInvalidate.right + Textsize.cx;dc.LPtoDP(&rectInvalidate);pDoc->SetModifiedFlag(TRUE);InvalidateRect(&rectInvalidate,TRUE);/局部 更新/*绘制电阻功能完成*/ TODO: Add your message handler code here and/or call default POSITION CurPos = NULL;/如果点按在元器件上,它才有意义,它就是这个被点按的元器件在链表中的位置BOOL IsS

34、el = FALSE;/如果点按在元器件上,它才有意义,它就是这个被点按的元器件的IsSelected属性值BOOL IsInChip =FALSE;IsInChip = IsInChipRect(point,pObList,&CurPos,&IsSel);/判断鼠标是否点按在器件上int iBeSelCount = BeSelectedNum(pObList);/求被选中的器件个数if (m_bNothing)/当前处于选择状态if (nFlags & MK_SHIFT)/shift按下if (!IsInChip)/点在空白处if (iBeSelCount = 0)g

35、oto End;/点在空白处,而且当前没有被选中的,所以不用更新屏幕elseSetAllChipNotIsSel(pObList);/使所有器件都处于非选中状态else /点在元器件处CComponent *pComponent;pComponent = (CComponent *)pObList->GetAt(CurPos);if (IsSel)/当前点按的元器件已经处于选中状态pComponent->SetIsSelected(FALSE);else /当前点按的元器件没有处于选中状态pComponent->SetIsSelected(TRUE);Invalidate()

36、;goto End; else/shift没有按下if (!IsInChip)/点在空白处if (iBeSelCount = 0)/点在空白处,而且没有被选中的,所以不用更新屏幕elseSetAllChipNotIsSel(pObList);Invalidate();/goto End;else /点在元器件处CResistor *pResistor;CComponent *pComponent;pComponent = (CComponent *)pObList->GetAt(CurPos);if (IsSel)/当前点按的元器件已经处于选中状态if (iBeSelCount>=

37、2)/当前链表中处于选中状态的有多个m_bjitiyidong=true;point_from=point;/做集体移动else /当前链表中处于选中状态的有一个/单个移动和放大或缩小的功能if (pComponent->IsKindOf(RUNTIME_CLASS(CResistor)pResistor = (CResistor *)pComponent;/m_RectTracker.m_rect = pResistor->m_RectTracker.m_rect;/m_RectTracker.m_nStyle = CRectTracker:resizeInside;if (pR

38、esistor->m_RectTracker.Track(this,point,FALSE,NULL)pResistor->m_RectLP = pResistor->m_RectTracker.m_rect;dc.DPtoLP(pResistor->m_RectLP);pResistor->m_Rect = pResistor->m_RectLP;pDoc->SetModifiedFlag(TRUE);Invalidate();goto End;else /当前点按的元器件没有处于选中状态,当前器件选中,其它非选中SetAllChipNotIsSel

39、(pObList);pComponent->SetIsSelected(TRUE);Invalidate();goto End;End:CScrollView:OnLButtonDown(nFlags, point);delete lpPoint;5、 添加对象到列表void CDrawChip1View:AddChipToList(CObject *pObject, CObList *pObList) if (pObject->IsKindOf(RUNTIME_CLASS(CResistor)CResistor *pResistor = (CResistor*)pObject;C

40、String name = pResistor->CalculateName(pObList);pResistor->SetName(name);pResistor->SetIsSelected(FALSE);/设置非选中状态pObList->AddHead(pResistor);6、 其它函数bool CDrawChip1View:IsInChipRect(CPoint point, CObList *pObList, POSITION *pCurPos, BOOL *pIsSel)*pCurPos = NULL;*pIsSel = FALSE;POSITION po

41、s = pObList->GetTailPosition();int ht = CRectTracker:hitNothing;CObject *pObject;CResistor *pResistor;while(pos!=NULL)*pCurPos = pos;pObject = pObList->GetAt(pos);if (pObject->IsKindOf(RUNTIME_CLASS(CResistor)pResistor = (CResistor *)pObject;ht = pResistor->m_RectTracker.HitTest(point);*

42、pIsSel = pResistor->GetIsSelected();if (ht != CRectTracker:hitNothing)return TRUE;elsepObList->GetPrev(pos);return FALSE;int CDrawChip1View:BeSelectedNum(CObList *pObList)POSITION pos = pObList->GetHeadPosition();CObject *pObject;CResistor *pResistor;int num = 0;while(pos!=NULL)pObject = pO

43、bList->GetAt(pos);if (pObject->IsKindOf(RUNTIME_CLASS(CResistor)pResistor = (CResistor *)pObject;if (pResistor->GetIsSelected()num+;pObList->GetNext(pos);return num;void CDrawChip1View:SetAllChipNotIsSel(CObList *pObList)POSITION pos = pObList->GetHeadPosition();CObject *pObject;CResistor *pResistor;while(pos!=NULL)pObject = pObList->GetAt(pos);if (pObject->IsKindOf(RUNTIME_CLASS(CResistor)p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论