CCurve.cpp 4.2 KB

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