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

绘图程序

开发平台:

C/C++

  1. #include "stdafx.h"
  2. #include "CCurve.h"
  3. #include"EastDrawView.h"
  4. IMPLEMENT_SERIAL(CCurve,CObject,1)
  5. void CCurve::DrawStatic(CDC*pDC)
  6. {
  7.  CPen m_pen;
  8.  m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  9.  CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  10.  int oldDrawingMode=pDC->SetROP2(m_DrawingMode);
  11.  
  12.     
  13. m_FirstPoint=m_PointList->GetAt(0);
  14.  pDC->MoveTo(m_FirstPoint);
  15.  for(int i=0;i<m_PointList->GetSize();i++)
  16.  {
  17.    pDC->LineTo(m_PointList->GetAt(i));
  18.    pDC->LineTo(m_PointList->GetAt(i));
  19.  }
  20.    pDC->LineTo(m_PointList->GetAt(i-1));
  21.    pDC->SelectObject(pen);
  22.    pDC->SetROP2(oldDrawingMode);
  23.   
  24. }
  25. int CCurve::IsOnMarginPoint(CPoint point)
  26. {
  27.   CRect rect;
  28.   for(int i=0;i<m_PointList->GetSize();i=i+10)
  29.   {
  30.    rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  31.    rect.InflateRect(4,4);
  32.    if(rect.PtInRect(point))
  33.    {
  34.    m_FoundPoint=m_PointList->GetAt(i);
  35.    return i+i;
  36.    }
  37.   }
  38.    return -1;
  39. }
  40. void CCurve::Initial()
  41. {
  42.  CUnit::Initial();
  43.  
  44.  m_PointList=new CArray<CPoint,CPoint>;
  45. }
  46. void CCurve::Serialize(CArchive &ar)
  47. {
  48. if(ar.IsStoring())
  49.   {
  50.    ar<<m_PenColor<<m_PenStyle<<m_PenWidth;
  51.   }
  52.   else
  53.   {
  54.   ar>>m_PenColor>>m_PenStyle>>m_PenWidth;
  55.  
  56.   }
  57.    m_PointList->Serialize(ar);
  58. }
  59. CCurve::CCurve()
  60. {
  61. Initial();
  62. }
  63. void CCurve::DrawMask(CDC*pDC,CPoint first,CPoint second)
  64. {
  65.  CPen m_pen;
  66.  m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  67.  CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  68.  
  69.  pDC->MoveTo(first);
  70.  pDC->LineTo(second);
  71.  pDC->SelectObject(pen);
  72. }
  73. BOOL CCurve::IsInRgn(CPoint point)
  74. {
  75.  CRect rect;
  76.   for(int i=0;i<m_PointList->GetSize();i++)
  77.   {
  78.    rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  79.    rect.InflateRect(9,9);
  80.    if(rect.PtInRect(point))
  81.    return i;
  82.   }
  83. return -1;
  84. }
  85. void CCurve::ShowSelectPoint(CDC *pDC)
  86. {
  87.   CRect rect;
  88.   CBrush brush;
  89. brush.CreateSolidBrush(RGB(255,0,0));
  90. CPen m_pen;
  91. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  92. CPen *OldPen=pDC->SelectObject(&m_pen);
  93. int oldBkMode=pDC->SetBkMode(OPAQUE); 
  94. CBrush *OldBrush=pDC->SelectObject(&brush);
  95. int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  96.   for(int i=0;i<m_PointList->GetSize();i=i+10)
  97.   {
  98.    rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  99.    rect.InflateRect(3,3);
  100.    pDC->Rectangle(rect);
  101.   }
  102.     pDC->SelectObject(OldPen);
  103.     pDC->SetBkMode(oldBkMode); 
  104.     pDC->SelectObject(OldBrush);
  105.     pDC->SetROP2(oldDrawingMode);
  106. }
  107. CRect CCurve::GetBoundingRect()
  108. {
  109.  m_MinPoint=m_MaxPoint=m_PointList->GetAt(0);
  110.  for(int i=0;i<m_PointList->GetSize();i++)
  111.   {
  112.    m_MinPoint.x=min(m_MinPoint.x,m_PointList->GetAt(i).x);
  113.    m_MinPoint.y=min(m_MinPoint.y,m_PointList->GetAt(i).y);
  114.    m_MaxPoint.x=max(m_MaxPoint.x,m_PointList->GetAt(i).x);
  115.    m_MaxPoint.y=max(m_MaxPoint.y,m_PointList->GetAt(i).y);
  116.  }
  117.  
  118.  this->m_BoundingRect.SetRect(this->m_MinPoint,this->m_MaxPoint);
  119.  return this->m_BoundingRect;
  120. }
  121. void CCurve::DrawActive(CDC *pDC,CPoint point)
  122. {
  123.  this->DrawStatic(pDC);
  124.   
  125.    
  126. }
  127. void CCurve::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  128. {
  129.     if(pView->m_CurrentDrawStatus==Drag_Status)
  130.   {
  131.     for(int i=0;i<m_PointList->GetSize();i++)
  132. {
  133.    CPoint point=m_PointList->GetAt(i);
  134.    point.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  135.    m_PointList->SetAt(i,point);
  136.    }
  137.   }
  138.  else
  139.   {
  140.   pDC->SetROP2(R2_COPYPEN);
  141.   pView->m_FirstPoint=pView->m_SecondPoint;
  142.   pView->m_SecondPoint=point;
  143.   m_PointList->Add(point);
  144.   //m_DrawingMode=pDC->GetROP2();
  145.       DrawMask(pDC,pView->m_FirstPoint,pView->m_SecondPoint);
  146.   }
  147.   
  148. }
  149. void CCurve::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  150. {
  151. if(m_HaveFindFirst)
  152. {   
  153. pView->L_iPointCount=IsOnMarginPoint(point);
  154.         if(pView->L_iPointCount!=-1)
  155. {
  156.         pView->m_bHaveFindSecond=true;
  157. pView->m_CurrentDrawStatus=Drag_Status;
  158. pView->m_CurrentDrawTool=Curve_Tool;
  159. pView->m_pCurrentUnit=this;
  160. m_DrawingMode=pDC->GetROP2();
  161.         ShowSelectPoint(pDC);
  162. DrawStatic(pDC);
  163. DrawOldReferencePoint(pDC,m_FoundPoint);
  164. }
  165. }
  166. if((!pView->m_bHaveFindSecond)&&IsInRgn(point)!=-1)
  167. {   
  168. if(!m_HaveFindFirst)
  169. {
  170.  m_HaveFindFirst=true;
  171.  pView->m_bHaveFindFirst=true;
  172.      ShowSelectPoint(pDC);
  173. }
  174.         else
  175. {
  176.  m_HaveFindFirst=false;
  177.  pView->m_bHaveFindFirst=true;
  178.  ShowSelectPoint(pDC);
  179. }
  180. }
  181. }