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

绘图程序

开发平台:

C/C++

  1. #include"stdafx.h"
  2. #include"CPolygon.h"
  3. #include"EastDrawView.h"
  4. IMPLEMENT_SERIAL(CPolygon,CUnit,1)
  5. void CPolygon::Serialize(CArchive &ar)
  6. {
  7.  
  8.  CUnit::Serialize(ar);
  9.  if(ar.IsStoring())
  10.   {
  11.    ar<<m_PenColor<<m_PenStyle<<m_PenWidth;
  12.   }
  13.   else
  14.   {
  15.   ar>>m_PenColor>>m_PenStyle>>m_PenWidth;
  16.  
  17.   }
  18.   m_PointList->Serialize(ar);
  19. }
  20.  
  21. void CPolygon::Initial()
  22. {
  23.   CUnit::Initial();
  24.   m_PointList=new CArray<CPoint,CPoint>;
  25. }
  26. void CPolygon::DrawStatic(CDC *pDC)
  27. {
  28.   CPen m_pen;
  29.   CBrush m_brush;
  30.   int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  31.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  32.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  33.   LOGBRUSH brushlog;
  34.   brushlog.lbColor=m_BrushColor;
  35.   brushlog.lbHatch=m_BrushHatch;
  36.   brushlog.lbStyle=m_BrushStyle;
  37.   m_brush.CreateBrushIndirect(&brushlog);
  38.  
  39.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  40.  
  41.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  42.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  43.  m_FirstPoint=m_PointList->GetAt(0);
  44.  pDC->MoveTo(m_FirstPoint);
  45.  for(int i=0;i<m_PointList->GetSize();i++)
  46.  {
  47.    pDC->LineTo(m_PointList->GetAt(i));
  48.  }
  49.    pDC->LineTo(m_PointList->GetAt(0));
  50.    pDC->SelectObject(brush);
  51.    pDC->SelectObject(pen);
  52.    pDC->SetBkMode(OldBkMode);
  53.    pDC->SetBkColor(OldColor);
  54.    pDC->SetROP2(oldDrawingMode);
  55. }
  56. void CPolygon::DrawMask(CDC *pDC, CPoint point)
  57. {
  58.  CPen m_pen;
  59.   CBrush m_brush;
  60.   //int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  61.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  62.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  63.   LOGBRUSH brushlog;
  64.   brushlog.lbColor=m_BrushColor;
  65.   brushlog.lbHatch=m_BrushHatch;
  66.   brushlog.lbStyle=m_BrushStyle;
  67.   m_brush.CreateBrushIndirect(&brushlog);
  68.  
  69.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  70.  
  71.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  72.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  73.  pDC->MoveTo(m_PointList->GetAt(m_PointList->GetSize()-1));
  74.  pDC->LineTo(point);
  75.  pDC->SelectObject(brush);
  76.  pDC->SelectObject(pen);
  77.  pDC->SetBkMode(OldBkMode);
  78.  pDC->SetBkColor(OldColor);
  79.  //pDC->SetROP2(oldDrawingMode);
  80. }
  81. CPolygon::CPolygon()
  82. {
  83. Initial();
  84. }
  85. void CPolygon::DrawEnd(CDC*pDC,CPoint point)
  86. {
  87.  CPen m_pen;
  88.   CBrush m_brush;
  89.   int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  90.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  91.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  92.   LOGBRUSH brushlog;
  93.   brushlog.lbColor=m_BrushColor;
  94.   brushlog.lbHatch=m_BrushHatch;
  95.   brushlog.lbStyle=m_BrushStyle;
  96.   m_brush.CreateBrushIndirect(&brushlog);
  97.  
  98.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  99.  
  100.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  101.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  102.  pDC->MoveTo(point);
  103.  pDC->LineTo(m_PointList->GetAt(0));
  104.  pDC->SelectObject(brush);
  105.  pDC->SelectObject(pen);
  106.  pDC->SetBkMode(OldBkMode);
  107.  pDC->SetBkColor(OldColor);
  108.  pDC->SetROP2(oldDrawingMode);
  109. }
  110. int CPolygon::IsOnMarginPoint(CPoint point)
  111. {
  112.   CRect rect;
  113.   CPoint firstPoint;
  114.   CPoint secondPoint;
  115.   firstPoint=secondPoint=m_PointList->GetAt(0);
  116.   for(int i=0;i<m_PointList->GetSize();i++)
  117.   {
  118.    secondPoint=m_PointList->GetAt(i);
  119.    rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  120.    rect.InflateRect(4,4);
  121.    if(rect.PtInRect(point))
  122.   { 
  123.    m_FoundIndex=i;
  124.    m_FoundPoint=m_PointList->GetAt(i);
  125.    return i;
  126.       } 
  127.    rect.SetRect(CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2),CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2));
  128.    rect.InflateRect(4,4);
  129.    if(rect.PtInRect(point))
  130.       { 
  131.    m_FoundPoint=CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2);      
  132.        return -i-10;
  133.       }   
  134.    firstPoint=secondPoint;
  135.   }
  136.   return -1;
  137. }
  138. BOOL CPolygon::IsInRgn(CPoint point)
  139. {
  140.    CPoint point4[4];
  141.    CPoint FirstPoint;
  142.    CPoint SecondPoint;
  143.    m_FirstPoint=m_PointList->GetAt(m_PointList->GetSize()-1);
  144.    CRgn rgn;
  145. for(int i=0;i<m_PointList->GetSize();i++)
  146. {
  147.     m_SecondPoint=m_PointList->GetAt(i);
  148. if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)<0)
  149.   {
  150.    if(m_FirstPoint.x>m_SecondPoint.x)
  151.    { 
  152.    
  153.    FirstPoint=m_SecondPoint;
  154.        SecondPoint=m_FirstPoint;   
  155.    }
  156.    else
  157.    {
  158.        FirstPoint=m_FirstPoint;
  159.        SecondPoint=m_SecondPoint;
  160.    
  161.    }
  162.    FirstPoint.Offset(-4-m_PenWidth,4+m_PenWidth);
  163.    SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
  164.   point4[0].x=FirstPoint.x-3-m_PenWidth;
  165.   point4[0].y=FirstPoint.y-4-m_PenWidth;
  166.   point4[1].x=FirstPoint.x+3+m_PenWidth;
  167.   point4[1].y=FirstPoint.y+4+m_PenWidth;
  168.   point4[2].x=SecondPoint.x+3+m_PenWidth;
  169.   point4[2].y=SecondPoint.y+4+m_PenWidth;
  170.   point4[3].x=SecondPoint.x-3-m_PenWidth;
  171.   point4[3].y=SecondPoint.y-4-m_PenWidth;
  172.   
  173.   }
  174.   if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)>0)
  175.   {
  176.    if(m_FirstPoint.x>m_SecondPoint.x)
  177.    { 
  178.    
  179.    FirstPoint=m_FirstPoint;
  180.        SecondPoint=m_SecondPoint;   
  181.    }
  182.    else
  183.    {
  184.    FirstPoint=m_SecondPoint;
  185.        SecondPoint=m_FirstPoint; 
  186.    }
  187.     FirstPoint.Offset(-4-m_PenWidth,-4-m_PenWidth);
  188. SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
  189.   point4[0].x=FirstPoint.x+3+m_PenWidth;
  190.   point4[0].y=FirstPoint.y-4-m_PenWidth;
  191.   point4[1].x=FirstPoint.x-3-m_PenWidth;
  192.   point4[1].y=FirstPoint.y+4+m_PenWidth;
  193.   point4[2].x=SecondPoint.x-3-m_PenWidth;
  194.   point4[2].y=SecondPoint.y+4+m_PenWidth;
  195.   point4[3].x=SecondPoint.x+3+m_PenWidth;
  196.   point4[3].y=SecondPoint.y-4-m_PenWidth;
  197.   
  198.   }
  199. if((m_FirstPoint.x-m_SecondPoint.x)==0)
  200.   {
  201.   point4[0].x=m_FirstPoint.x-3-m_PenWidth;
  202.   point4[0].y=m_FirstPoint.y-4-m_PenWidth;
  203.   point4[1].x=m_FirstPoint.x+3+m_PenWidth;
  204.   point4[1].y=m_FirstPoint.y-4-m_PenWidth;
  205.   point4[2].x=m_SecondPoint.x+3+m_PenWidth;
  206.   point4[2].y=m_SecondPoint.y+4+m_PenWidth;
  207.   point4[3].x=m_SecondPoint.x-3-m_PenWidth;
  208.   point4[3].y=m_SecondPoint.y+4+m_PenWidth;
  209.   
  210.   }
  211. if((m_FirstPoint.y-m_SecondPoint.y)==0)
  212.   {
  213.   point4[0].x=m_FirstPoint.x-4-m_PenWidth;
  214.   point4[0].y=m_FirstPoint.y-3-m_PenWidth;
  215.   point4[1].x=m_FirstPoint.x-4-m_PenWidth;
  216.   point4[1].y=m_FirstPoint.y+3+m_PenWidth;
  217.   point4[2].x=m_SecondPoint.x+4+m_PenWidth;
  218.   point4[2].y=m_SecondPoint.y+3+m_PenWidth;
  219.   point4[3].x=m_SecondPoint.x+4+m_PenWidth;
  220.   point4[3].y=m_SecondPoint.y-3-m_PenWidth;
  221.   }
  222.   m_FirstPoint=m_SecondPoint;  
  223.   rgn.CreatePolygonRgn(point4,4,ALTERNATE);
  224.   if(rgn.PtInRegion(point))
  225.   return true;
  226.     rgn.DeleteObject();
  227. }
  228.   return false;
  229. }
  230. void CPolygon::ExchangeDraw(CDC *pDC, CPoint point)
  231. {
  232.  //m_FoundIndex
  233.  CPen m_pen;
  234.   CBrush m_brush;
  235.   int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  236.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  237.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  238.   LOGBRUSH brushlog;
  239.   brushlog.lbColor=m_BrushColor;
  240.   brushlog.lbHatch=m_BrushHatch;
  241.   brushlog.lbStyle=m_BrushStyle;
  242.   m_brush.CreateBrushIndirect(&brushlog);
  243.  
  244.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  245.  
  246.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  247.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  248.   
  249.  m_SecondPoint=m_PointList->GetAt((m_FoundIndex+1)%m_PointList->GetSize());
  250.  m_FirstPoint=m_PointList->GetAt(m_FoundIndex==0? m_PointList->GetSize()-1:m_FoundIndex-1);
  251.  
  252.  if(m_FoundIndex!=0)
  253.  {
  254.  pDC->MoveTo(m_FirstPoint);
  255.  pDC->LineTo(point);
  256.  pDC->MoveTo(point);
  257.  pDC->LineTo(m_SecondPoint);
  258.  }
  259.  if(m_FoundIndex==0)
  260.  {
  261.   pDC->MoveTo(m_SecondPoint);
  262.   pDC->LineTo(point);
  263.   pDC->MoveTo(point);
  264.   pDC->LineTo(m_FirstPoint);
  265.  }
  266.  m_PointList->SetAt(m_FoundIndex,point);
  267.  pDC->SelectObject(brush);
  268.  pDC->SelectObject(pen);
  269.  pDC->SetBkMode(OldBkMode);
  270.  pDC->SetBkColor(OldColor);
  271.  pDC->SetROP2(oldDrawingMode);
  272. }
  273. void CPolygon::ShowSelectPoint(CDC *pDC)
  274. {
  275.   CRect rect;
  276.   CPoint firstPoint;
  277.   CPoint secondPoint;
  278.   firstPoint=secondPoint=m_PointList->GetAt(0);
  279.   CBrush brush;
  280.   brush.CreateSolidBrush(RGB(0,0,255));
  281.   CPen m_pen;
  282.   m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  283.   CPen *OldPen=pDC->SelectObject(&m_pen);
  284.   int oldBkMode=pDC->SetBkMode(OPAQUE); 
  285.   CBrush *OldBrush=pDC->SelectObject(&brush);
  286.   int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  287.   for(int i=0;i<m_PointList->GetSize();i++)
  288.   {
  289.    rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  290.    rect.InflateRect(3,3);
  291.    pDC->Rectangle(rect);
  292.   }
  293.    
  294.   pDC->SelectObject(OldBrush);
  295.   brush.DeleteObject();
  296.   brush.CreateSolidBrush(RGB(255,0,0));
  297.   pDC->SelectObject(&brush);
  298.    firstPoint=m_PointList->GetAt(0);
  299.    //secondPoint=m_PointList->GetAt(1);
  300.   for(i=1;i<m_PointList->GetSize();i++)
  301.   {
  302.    secondPoint=m_PointList->GetAt(i);
  303.    rect.SetRect(CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2),CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2));
  304.    rect.InflateRect(3,3);
  305.    pDC->Rectangle(rect);
  306.    firstPoint=secondPoint;
  307.   }
  308.   
  309.   pDC->SelectObject(OldPen);
  310.   pDC->SetBkMode(oldBkMode); 
  311.   pDC->SelectObject(OldBrush);
  312.   pDC->SetROP2(oldDrawingMode);
  313. }
  314. void CPolygon::DrawActive(CDC *pDC,CPoint point)
  315. {
  316.   CPen m_pen;
  317.   CBrush m_brush;
  318.   
  319.   m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  320.   CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  321.   LOGBRUSH brushlog;
  322.   brushlog.lbColor=m_BrushColor;
  323.   brushlog.lbHatch=m_BrushHatch;
  324.   brushlog.lbStyle=m_BrushStyle;
  325.   m_brush.CreateBrushIndirect(&brushlog);
  326.  
  327.   CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  328.  
  329.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  330.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  331.  m_FirstPoint=m_PointList->GetAt(0);
  332.  pDC->MoveTo(m_FirstPoint);
  333.  for(int i=0;i<m_PointList->GetSize();i++)
  334.  {
  335.    pDC->LineTo(m_PointList->GetAt(i));
  336.  }
  337.    pDC->LineTo(m_PointList->GetAt(0));
  338.    pDC->SelectObject(brush);
  339.  pDC->SelectObject(pen);
  340.  pDC->SetBkMode(OldBkMode);
  341.  pDC->SetBkColor(OldColor);
  342.  
  343. }
  344. void CPolygon::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  345. {
  346.   if(m_HaveFindFirst)
  347.   {
  348.   pView->L_iPointCount=IsOnMarginPoint(point);
  349.   if(pView->L_iPointCount>=0)
  350.   {     
  351.         pView->m_bHaveFindSecond=true;
  352.         pView->m_CurrentDrawTool=Polygon_Tool;
  353.         pView->m_CurrentDrawStatus=Change_Status;
  354.   }
  355.   if(pView->L_iPointCount<-1)
  356.   {  
  357.  pView->m_bHaveFindSecond=true;
  358.      pView->m_CurrentDrawTool=Polygon_Tool;
  359.      pView->m_CurrentDrawStatus=Drag_Status;
  360.   }
  361. }//*******if(L_pPolygon->m_HaveFindFirst)
  362.   
  363.   if(!pView->m_bHaveFindSecond&&IsInRgn(point))
  364. {
  365.   if(!m_HaveFindFirst)
  366.   {
  367.     m_HaveFindFirst=true;
  368. pView->m_bHaveFindFirst=true;
  369.     ShowSelectPoint(pDC);
  370.   }
  371.   else
  372.   {
  373.     this->m_HaveFindFirst=false;
  374. pView->m_bHaveFindFirst=true;
  375. this->ShowSelectPoint(pDC);
  376.   }
  377.   }
  378.  if(pView->m_bHaveFindSecond)
  379.   {
  380.             pView->m_pCurrentUnit=this;
  381.         m_DrawingMode=pDC->GetROP2();
  382.         ShowSelectPoint(pDC);
  383. DrawOldReferencePoint(pDC,m_FoundPoint);
  384. DrawStatic(pDC);
  385.  }
  386. }
  387. void CPolygon::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  388. {
  389.    if(pView->m_CurrentDrawStatus==Draw_Status)
  390.    {
  391.       if(pView->m_LBDownTimes!=0)
  392.   {
  393.   pDC->SetROP2(R2_NOTXORPEN);
  394.   //m_DrawingMode=pDC->GetROP2();
  395.   DrawMask(pDC,pView->m_SecondPoint);
  396.       pView->m_SecondPoint=point;
  397.      pDC->SetROP2(this->m_DrawingMode);
  398.   DrawMask(pDC,pView->m_SecondPoint);
  399.   }
  400. }
  401.      if(pView->m_CurrentDrawStatus==Change_Status)
  402.  {
  403.    pDC->SetROP2(R2_NOTXORPEN);
  404.        ExchangeDraw(pDC,pView->m_SecondPoint);
  405.        pView->m_SecondPoint=point;
  406.        ExchangeDraw(pDC,pView->m_SecondPoint);
  407.      }
  408.   if(pView->m_CurrentDrawStatus==Drag_Status)
  409.   {
  410.   for(int i=0;i<m_PointList->GetSize();i++)
  411.   {
  412.    CPoint point=m_PointList->GetAt(i);
  413.    point.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  414.    m_PointList->SetAt(i,point);
  415.    }
  416.   
  417.   
  418.   }
  419. }