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

绘图程序

开发平台:

C/C++

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