《电子元器件系统.doc》由会员分享,可在线阅读,更多相关《电子元器件系统.doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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
22、RectTracker:resizeOutside;void CResistor: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 (
23、ar.IsStoring()/ 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:DeleteCo
24、ntents();三、 视图类主要函数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
25、),*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-LPtoDP(pResistor-m_Re
26、ctTracker.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()CScrollView:OnInitialUpdate();CSize sizeTotal(30000,30000);
27、/这里的值是逻辑坐标,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 nFlags, CPoint point) /point参数代表光标的坐标位置(设备坐标)/nFlags鼠标键与
28、键盘的组合标志,有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 = lpPoint-x;int y = lpPoint-y;CDrawChip1Doc *pDoc = GetDocument();C
29、ObList *pObList = &(pDoc-m_ChipObList);CObList *pTempObList = &(pDoc-m_ChipObList);CRect rectInvalidate;/存放更新的区域CString strName;CSize Textsize;/记录元器件对象名称字符的宽度和高度if (m_bResistor)/当前处于绘制电阻状态CResistor *pResistor = new CResistor(lpPoint);AddChipToList(pResistor,pObList);strName = pResistor-GetName();:Ge
30、tTextExtentPoint32(dc, (const char *)&strName, strName.GetLength(), &Textsize);/计算strName的大小rectInvalidate = pResistor-m_Rect;rectInvalidate.bottom = rectInvalidate.bottom - Textsize.cy;rectInvalidate.right = rectInvalidate.right + Textsize.cx;dc.LPtoDP(&rectInvalidate);pDoc-SetModifiedFlag(TRUE);In
31、validateRect(&rectInvalidate,TRUE);/局部 更新/*绘制电阻功能完成*/ TODO: Add your message handler code here and/or call default POSITION CurPos = NULL;/如果点按在元器件上,它才有意义,它就是这个被点按的元器件在链表中的位置BOOL IsSel = FALSE;/如果点按在元器件上,它才有意义,它就是这个被点按的元器件的IsSelected属性值BOOL IsInChip =FALSE;IsInChip = IsInChipRect(point,pObList,&CurP
32、os,&IsSel);/判断鼠标是否点按在器件上int iBeSelCount = BeSelectedNum(pObList);/求被选中的器件个数if (m_bNothing)/当前处于选择状态if (nFlags & MK_SHIFT)/shift按下if (!IsInChip)/点在空白处if (iBeSelCount = 0)goto End;/点在空白处,而且当前没有被选中的,所以不用更新屏幕elseSetAllChipNotIsSel(pObList);/使所有器件都处于非选中状态else /点在元器件处CComponent *pComponent;pComponent = (C
33、Component *)pObList-GetAt(CurPos);if (IsSel)/当前点按的元器件已经处于选中状态pComponent-SetIsSelected(FALSE);else /当前点按的元器件没有处于选中状态pComponent-SetIsSelected(TRUE);Invalidate();goto End; else/shift没有按下if (!IsInChip)/点在空白处if (iBeSelCount = 0)/点在空白处,而且没有被选中的,所以不用更新屏幕elseSetAllChipNotIsSel(pObList);Invalidate();/goto En
34、d;else /点在元器件处CResistor *pResistor;CComponent *pComponent;pComponent = (CComponent *)pObList-GetAt(CurPos);if (IsSel)/当前点按的元器件已经处于选中状态if (iBeSelCount=2)/当前链表中处于选中状态的有多个m_bjitiyidong=true;point_from=point;/做集体移动else /当前链表中处于选中状态的有一个/单个移动和放大或缩小的功能if (pComponent-IsKindOf(RUNTIME_CLASS(CResistor)pResist
35、or = (CResistor *)pComponent;/m_RectTracker.m_rect = pResistor-m_RectTracker.m_rect;/m_RectTracker.m_nStyle = CRectTracker:resizeInside;if (pResistor-m_RectTracker.Track(this,point,FALSE,NULL)pResistor-m_RectLP = pResistor-m_RectTracker.m_rect;dc.DPtoLP(pResistor-m_RectLP);pResistor-m_Rect = pResist
36、or-m_RectLP;pDoc-SetModifiedFlag(TRUE);Invalidate();goto End;else /当前点按的元器件没有处于选中状态,当前器件选中,其它非选中SetAllChipNotIsSel(pObList);pComponent-SetIsSelected(TRUE);Invalidate();goto End;End:CScrollView:OnLButtonDown(nFlags, point);delete lpPoint;5、 添加对象到列表void CDrawChip1View:AddChipToList(CObject *pObject, C
37、ObList *pObList) if (pObject-IsKindOf(RUNTIME_CLASS(CResistor)CResistor *pResistor = (CResistor*)pObject;CString name = pResistor-CalculateName(pObList);pResistor-SetName(name);pResistor-SetIsSelected(FALSE);/设置非选中状态pObList-AddHead(pResistor);6、 其它函数bool CDrawChip1View:IsInChipRect(CPoint point, COb
38、List *pObList, POSITION *pCurPos, BOOL *pIsSel)*pCurPos = NULL;*pIsSel = FALSE;POSITION pos = 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)pRes
39、istor = (CResistor *)pObject;ht = pResistor-m_RectTracker.HitTest(point);*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
40、;CResistor *pResistor;int num = 0;while(pos!=NULL)pObject = pObList-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 = pObLis
41、t-GetHeadPosition();CObject *pObject;CResistor *pResistor;while(pos!=NULL)pObject = pObList-GetAt(pos);if (pObject-IsKindOf(RUNTIME_CLASS(CResistor)pResistor = (CResistor *)pObject;pResistor-SetIsSelected(FALSE);pObList-GetNext(pos);void CDrawChip1View:OnCopy() CClientDC dc(this);OnPrepareDC(&dc);CD
42、rawChip1Doc *pDoc = GetDocument();CObList *pObList = &(pDoc-m_ChipObList);int iBeSelCount = BeSelectedNum(pObList);CResistor *pResistor;CObject *pObject;RESISTOR Resistor;HGLOBAL hMem;/4个字节长的整数值,句柄。LPVOID lpMem;/指向一个任何类型的指针POSITION pos;CRect rectInvalidate;CString strName;CSize Textsize;switch (iBeSelCount)case 1:/选中了一个:OpenClipboard(this-m_hW