CRoundArc.cpp
上传用户:qiye66671
上传日期:2009-12-10
资源大小:182k
文件大小:8k
源码类别:

绘图程序

开发平台:

C/C++

  1. #include"stdafx.h"
  2. #include"CRoundArc.h"
  3. #include"EastDrawView.h"
  4. IMPLEMENT_SERIAL(CRoundArc,CUnit,1)
  5. CRoundArc::CRoundArc()
  6. {
  7. }
  8. void CRoundArc::DrawStatic(CDC *pDC)
  9. {
  10.   CPen m_pen;
  11.   CBrush m_brush;
  12.   int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  13.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  14.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  15.   LOGBRUSH brushlog;
  16.   brushlog.lbColor=m_BrushColor;
  17.   brushlog.lbHatch=m_BrushHatch;
  18.   brushlog.lbStyle=m_BrushStyle;
  19.   m_brush.CreateBrushIndirect(&brushlog);
  20.  
  21.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  22.  
  23.   int OldBkMode=pDC->SetBkMode(m_BkMode);
  24.   COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  25.   CRect rect;
  26.   rect.SetRect(m_CenterPoint.x-m_Xr,m_CenterPoint.y-m_Xr,m_CenterPoint.x+m_Xr,m_CenterPoint.y+m_Xr);
  27.   pDC->Arc(rect,m_FirstPoint,m_ThirdPoint);
  28.   pDC->SelectObject(brush);
  29.   pDC->SelectObject(pen);
  30.   pDC->SetBkMode(OldBkMode);
  31.   pDC->SetBkColor(OldColor);
  32.   pDC->SetROP2(oldDrawingMode);
  33. }
  34. double CRoundArc::ComputeSloap1(CPoint firstpoint, CPoint secondpoint)
  35. {
  36.     if(firstpoint.y==secondpoint.y)
  37. {
  38.   m_Sloap1=-9999999999999.0;
  39. }
  40. else
  41. {
  42.   m_Sloap1=-double((firstpoint.x-secondpoint.x))/double((firstpoint.y-secondpoint.y));
  43. }
  44.    m_Sloap2=m_Sloap1;
  45.    m_CenterPoint=CPoint((firstpoint.x+secondpoint.x)/2,(firstpoint.y+secondpoint.y)/2);
  46.    m_Xr=0;//sqrt((firstpoint.x-secondpoint.x)*(firstpoint.x-secondpoint.x)+(firstpoint.y-firstpoint.y)*(firstpoint.y-firstpoint.y));
  47.    return m_Sloap1;
  48. }
  49. double CRoundArc::ComputeSloap2(CPoint firstpoint, CPoint secondpoint)
  50. {
  51. if(firstpoint.y==secondpoint.y)
  52. {
  53.   m_Sloap2=-999999999.0;
  54. }
  55. else
  56. {
  57.  m_Sloap2=-double((firstpoint.x-secondpoint.x))/double((firstpoint.y-secondpoint.y));
  58. }
  59.     
  60.     
  61. this->m_CenterPoint.x=((m_FirstPoint.y-m_ThirdPoint.y)/2-m_Sloap1*(m_FirstPoint.x+m_SecondPoint.x)/2+m_Sloap2*(m_SecondPoint.x+m_ThirdPoint.x)/2)/(m_Sloap2-m_Sloap1);
  62. this->m_CenterPoint.y=((m_FirstPoint.y+m_SecondPoint.y)/2*m_Sloap2-(m_SecondPoint.y+m_ThirdPoint.y)/2*m_Sloap1+m_Sloap1*m_Sloap2*(m_ThirdPoint.x-m_FirstPoint.x)/2)/(m_Sloap2-m_Sloap1);
  63. m_Xr=sqrt((m_CenterPoint.x-m_ThirdPoint.x)*(m_CenterPoint.x-m_ThirdPoint.x)+(m_CenterPoint.y-m_ThirdPoint.y)*(m_CenterPoint.y-m_ThirdPoint.y));
  64. return m_Sloap2;
  65. }
  66. void CRoundArc::DrawActive(CDC *pDC,CPoint point)
  67. {
  68.   
  69. ShowMovingLine(pDC,m_FirstPoint,m_SecondPoint);
  70. if(m_Xr==0)
  71.   return;
  72. ShowMovingLine(pDC,m_FirstPoint,m_CenterPoint);
  73. ShowMovingLine(pDC,m_SecondPoint,m_CenterPoint);
  74. ShowMovingLine(pDC,m_SecondPoint,m_ThirdPoint);
  75.   CPen m_pen;
  76.   CBrush m_brush;
  77.   
  78.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  79.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  80.   LOGBRUSH brushlog;
  81.   brushlog.lbColor=m_BrushColor;
  82.   brushlog.lbHatch=m_BrushHatch;
  83.   brushlog.lbStyle=m_BrushStyle;
  84.   m_brush.CreateBrushIndirect(&brushlog);
  85.  
  86.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  87.  
  88.   int OldBkMode=pDC->SetBkMode(m_BkMode);
  89.   COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  90.   CRect rect;
  91.   rect.SetRect(m_CenterPoint.x-m_Xr,m_CenterPoint.y-m_Xr,m_CenterPoint.x+m_Xr,m_CenterPoint.y+m_Xr);
  92.   pDC->Arc(rect,m_FirstPoint,m_ThirdPoint);
  93.   pDC->SelectObject(brush);
  94.  pDC->SelectObject(pen);
  95.  pDC->SetBkMode(OldBkMode);
  96.  pDC->SetBkColor(OldColor);
  97. }
  98. void CRoundArc::Serialize(CArchive &ar)
  99. {
  100. CUnit::Serialize(ar);
  101. if(ar.IsStoring())
  102. {
  103.  ar<<m_ThirdPoint<<m_Xr<<m_CenterPoint;
  104. }
  105. else
  106. {
  107.  ar>>m_ThirdPoint>>m_Xr>>m_CenterPoint;
  108. }
  109. }
  110. BOOL CRoundArc::IsInRgn(CPoint point)
  111. {
  112.  
  113.    if(abs(sqrt((point.x-m_CenterPoint.x)*(point.x-m_CenterPoint.x)+(point.y-m_CenterPoint.y)*(point.y-m_CenterPoint.y))-m_Xr)<=2)
  114.       return true;
  115.  
  116.    return false;
  117. }
  118. void CRoundArc::ShowSelectPoint(CDC *pDC)
  119. {
  120. CBrush brush;
  121. brush.CreateSolidBrush(RGB(0,0,255));
  122. CPen m_pen;
  123. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  124. CPen *OldPen=pDC->SelectObject(&m_pen);
  125. int oldBkMode=pDC->SetBkMode(OPAQUE); 
  126. CBrush *OldBrush=pDC->SelectObject(&brush);
  127. int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  128. CRect rect;
  129. rect.SetRect(m_FirstPoint,m_FirstPoint);
  130. rect.InflateRect(3,3);
  131. pDC->Rectangle(rect);
  132. rect.SetRect(m_SecondPoint,m_SecondPoint);
  133. rect.InflateRect(3,3);
  134. pDC->Rectangle(rect);
  135. rect.SetRect(m_ThirdPoint,m_ThirdPoint);
  136. rect.InflateRect(3,3);
  137. pDC->Rectangle(rect);
  138.   pDC->SelectObject(OldBrush);
  139.   brush.DeleteObject();
  140.   brush.CreateSolidBrush(RGB(255,0,0));
  141.   pDC->SelectObject(&brush);
  142.   rect.SetRect(this->m_CenterPoint,this->m_CenterPoint);
  143.   rect.InflateRect(3,3);
  144.   pDC->Rectangle(rect);
  145.   pDC->SelectObject(OldPen);
  146.   pDC->SetBkMode(oldBkMode); 
  147.   pDC->SelectObject(OldBrush);
  148.   pDC->SetROP2(oldDrawingMode);
  149. }
  150. int CRoundArc::IsOnMarginPoint(CPoint point)
  151. {
  152. CRect rect;
  153. rect.SetRect(point,point);
  154. rect.InflateRect(4,4);
  155. if(rect.PtInRect(m_FirstPoint))
  156. {   this->m_FoundPoint=m_FirstPoint;
  157. return 1;
  158. }
  159. if(rect.PtInRect(m_SecondPoint))
  160. {   this->m_FoundPoint=m_SecondPoint;
  161. return 2;
  162. }
  163. if(rect.PtInRect(m_ThirdPoint))
  164. {   this->m_FoundPoint=m_ThirdPoint;
  165. return 3;
  166. }
  167. if(rect.PtInRect(m_CenterPoint))
  168. {   this->m_FoundPoint=m_CenterPoint;
  169. return 4;
  170. }
  171. return 0;
  172. }
  173. void CRoundArc::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  174. {
  175.    
  176.   if(m_HaveFindFirst)
  177.    { 
  178.   pView->L_iPointCount=IsOnMarginPoint(point);
  179.      if(pView->L_iPointCount!=0)
  180.  {   
  181.  pView->m_bHaveFindSecond=true;
  182.          pView->m_pCurrentUnit=this;
  183.      m_DrawingMode=pDC->GetROP2();
  184.      DrawActive(pDC,point); 
  185.          ShowSelectPoint(pDC);
  186.  DrawOldReferencePoint(pDC,m_FoundPoint);
  187.  m_HaveFindFirst=true;
  188.  pView->m_CurrentDrawTool=RoundArc_Tool;
  189.          pView->m_CurrentDrawStatus=Change_Status;
  190.  pView->m_FirstPoint=m_FirstPoint;
  191.      pView->m_SecondPoint=m_SecondPoint;
  192.  
  193.    if(pView->L_iPointCount==1)
  194.    {
  195.     
  196.    }
  197.    if(pView->L_iPointCount==2)
  198.    {
  199.    
  200.    }
  201.    if(pView->L_iPointCount==3)
  202.    {
  203.    }
  204.    if(pView->L_iPointCount==4)
  205.    {
  206.     pView->m_CurrentDrawTool=RoundArc_Tool;
  207.         pView->m_CurrentDrawStatus=Drag_Status;
  208.    }
  209.  }//***** if(L_iPointCount!=0)
  210. }
  211.     if(!pView->m_bHaveFindSecond&&IsInRgn(point))
  212. {
  213. if(!m_HaveFindFirst)
  214. {
  215.          m_HaveFindFirst=true;
  216.  pView->m_bHaveFindFirst=true;
  217.      ShowSelectPoint(pDC);
  218. }
  219. else
  220. {
  221.   m_HaveFindFirst=false;
  222.   pView->m_bHaveFindFirst=true;
  223.   ShowSelectPoint(pDC);
  224. }
  225. }
  226.   
  227. }
  228. void CRoundArc::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  229. {
  230.   if(pView->m_CurrentDrawStatus==Drag_Status)
  231.   {
  232.   m_FirstPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  233.       m_SecondPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  234.   m_ThirdPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  235.       m_CenterPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  236.   }
  237.   else
  238.   {
  239.   pDC->SetROP2(R2_NOTXORPEN);
  240.       if(pView->m_LBDownTimes==1&&pView->m_CurrentDrawStatus!=Change_Status)
  241.   {
  242.   ShowMovingLine(pDC,pView->m_FirstPoint,pView->m_SecondPoint);
  243.   pView->m_SecondPoint=point;
  244.   ShowMovingLine(pDC,pView->m_FirstPoint,point);
  245.   }
  246.   if(pView->m_LBDownTimes==2&&pView->m_CurrentDrawStatus!=Change_Status)
  247.   {
  248.       DrawActive(pDC,point);
  249.   m_ThirdPoint=point;
  250.   ComputeSloap2(m_SecondPoint,m_ThirdPoint);
  251.           DrawActive(pDC,point);
  252.   }
  253.    if(pView->m_CurrentDrawStatus==Change_Status)
  254.   {
  255.     DrawActive(pDC,point);
  256.     if(pView->L_iPointCount==1)
  257. {
  258.   m_FirstPoint=point;
  259.   ComputeSloap1(m_FirstPoint,m_SecondPoint);
  260.           ComputeSloap2(m_SecondPoint,m_ThirdPoint);
  261. }
  262.     if(pView->L_iPointCount==2)
  263. {
  264.   m_SecondPoint=point;
  265.           ComputeSloap1(m_FirstPoint,m_SecondPoint);
  266.   ComputeSloap2(m_SecondPoint,m_ThirdPoint);
  267. }
  268. if(pView->L_iPointCount==3)
  269. {
  270.  m_ThirdPoint=point;
  271.  ComputeSloap2(m_SecondPoint,m_ThirdPoint);
  272. }
  273.      DrawActive(pDC,point);
  274.  pView->m_SecondPoint=point;
  275.    }//******if(m_CurrentDrawStatus==Exchange_Status)
  276.   }
  277.   }