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

绘图程序

开发平台:

C/C++

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