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

绘图程序

开发平台:

C/C++

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