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

绘图程序

开发平台:

C/C++

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