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

绘图程序

开发平台:

C/C++

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