123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- #include"stdafx.h"
- #include"CPolygon.h"
- #include"EastDrawView.h"
- IMPLEMENT_SERIAL(CPolygon,CUnit,1)
- void CPolygon::Serialize(CArchive &ar)
- {
-
- CUnit::Serialize(ar);
- if(ar.IsStoring())
- {
- ar<<m_PenColor<<m_PenStyle<<m_PenWidth;
- }
- else
- {
- ar>>m_PenColor>>m_PenStyle>>m_PenWidth;
-
- }
- m_PointList->Serialize(ar);
- }
-
- void CPolygon::Initial()
- {
- CUnit::Initial();
- m_PointList=new CArray<CPoint,CPoint>;
- }
- void CPolygon::DrawStatic(CDC *pDC)
- {
- CPen m_pen;
- CBrush m_brush;
- int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
- m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
- CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
- LOGBRUSH brushlog;
- brushlog.lbColor=m_BrushColor;
- brushlog.lbHatch=m_BrushHatch;
- brushlog.lbStyle=m_BrushStyle;
- m_brush.CreateBrushIndirect(&brushlog);
-
- CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
-
- int OldBkMode=pDC->SetBkMode(m_BkMode);
- COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
- m_FirstPoint=m_PointList->GetAt(0);
- pDC->MoveTo(m_FirstPoint);
- for(int i=0;i<m_PointList->GetSize();i++)
- {
- pDC->LineTo(m_PointList->GetAt(i));
- }
- pDC->LineTo(m_PointList->GetAt(0));
- pDC->SelectObject(brush);
- pDC->SelectObject(pen);
- pDC->SetBkMode(OldBkMode);
- pDC->SetBkColor(OldColor);
- pDC->SetROP2(oldDrawingMode);
- }
- void CPolygon::DrawMask(CDC *pDC, CPoint point)
- {
- CPen m_pen;
- CBrush m_brush;
- //int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
- m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
- CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
- LOGBRUSH brushlog;
- brushlog.lbColor=m_BrushColor;
- brushlog.lbHatch=m_BrushHatch;
- brushlog.lbStyle=m_BrushStyle;
- m_brush.CreateBrushIndirect(&brushlog);
-
- CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
-
- int OldBkMode=pDC->SetBkMode(m_BkMode);
- COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
- pDC->MoveTo(m_PointList->GetAt(m_PointList->GetSize()-1));
- pDC->LineTo(point);
- pDC->SelectObject(brush);
- pDC->SelectObject(pen);
- pDC->SetBkMode(OldBkMode);
- pDC->SetBkColor(OldColor);
- //pDC->SetROP2(oldDrawingMode);
- }
- CPolygon::CPolygon()
- {
- Initial();
- }
- void CPolygon::DrawEnd(CDC*pDC,CPoint point)
- {
- CPen m_pen;
- CBrush m_brush;
- int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
- m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
- CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
- LOGBRUSH brushlog;
- brushlog.lbColor=m_BrushColor;
- brushlog.lbHatch=m_BrushHatch;
- brushlog.lbStyle=m_BrushStyle;
- m_brush.CreateBrushIndirect(&brushlog);
-
- CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
-
- int OldBkMode=pDC->SetBkMode(m_BkMode);
- COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
- pDC->MoveTo(point);
- pDC->LineTo(m_PointList->GetAt(0));
- pDC->SelectObject(brush);
- pDC->SelectObject(pen);
- pDC->SetBkMode(OldBkMode);
- pDC->SetBkColor(OldColor);
- pDC->SetROP2(oldDrawingMode);
- }
- int CPolygon::IsOnMarginPoint(CPoint point)
- {
- CRect rect;
- CPoint firstPoint;
- CPoint secondPoint;
- firstPoint=secondPoint=m_PointList->GetAt(0);
- for(int i=0;i<m_PointList->GetSize();i++)
- {
- secondPoint=m_PointList->GetAt(i);
- rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
- rect.InflateRect(4,4);
- if(rect.PtInRect(point))
- {
- m_FoundIndex=i;
- m_FoundPoint=m_PointList->GetAt(i);
- return i;
- }
- rect.SetRect(CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2),CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2));
- rect.InflateRect(4,4);
- if(rect.PtInRect(point))
- {
- m_FoundPoint=CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2);
- return -i-10;
- }
- firstPoint=secondPoint;
- }
- return -1;
- }
- BOOL CPolygon::IsInRgn(CPoint point)
- {
- CPoint point4[4];
- CPoint FirstPoint;
- CPoint SecondPoint;
- m_FirstPoint=m_PointList->GetAt(m_PointList->GetSize()-1);
- CRgn rgn;
- for(int i=0;i<m_PointList->GetSize();i++)
- {
- m_SecondPoint=m_PointList->GetAt(i);
- if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)<0)
- {
- if(m_FirstPoint.x>m_SecondPoint.x)
- {
-
- FirstPoint=m_SecondPoint;
- SecondPoint=m_FirstPoint;
- }
- else
- {
- FirstPoint=m_FirstPoint;
- SecondPoint=m_SecondPoint;
-
- }
- FirstPoint.Offset(-4-m_PenWidth,4+m_PenWidth);
- SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
- point4[0].x=FirstPoint.x-3-m_PenWidth;
- point4[0].y=FirstPoint.y-4-m_PenWidth;
- point4[1].x=FirstPoint.x+3+m_PenWidth;
- point4[1].y=FirstPoint.y+4+m_PenWidth;
- point4[2].x=SecondPoint.x+3+m_PenWidth;
- point4[2].y=SecondPoint.y+4+m_PenWidth;
- point4[3].x=SecondPoint.x-3-m_PenWidth;
- point4[3].y=SecondPoint.y-4-m_PenWidth;
-
- }
- if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)>0)
- {
- if(m_FirstPoint.x>m_SecondPoint.x)
- {
-
- FirstPoint=m_FirstPoint;
- SecondPoint=m_SecondPoint;
- }
- else
- {
- FirstPoint=m_SecondPoint;
- SecondPoint=m_FirstPoint;
- }
- FirstPoint.Offset(-4-m_PenWidth,-4-m_PenWidth);
- SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
- point4[0].x=FirstPoint.x+3+m_PenWidth;
- point4[0].y=FirstPoint.y-4-m_PenWidth;
- point4[1].x=FirstPoint.x-3-m_PenWidth;
- point4[1].y=FirstPoint.y+4+m_PenWidth;
- point4[2].x=SecondPoint.x-3-m_PenWidth;
- point4[2].y=SecondPoint.y+4+m_PenWidth;
- point4[3].x=SecondPoint.x+3+m_PenWidth;
- point4[3].y=SecondPoint.y-4-m_PenWidth;
-
- }
- if((m_FirstPoint.x-m_SecondPoint.x)==0)
- {
- point4[0].x=m_FirstPoint.x-3-m_PenWidth;
- point4[0].y=m_FirstPoint.y-4-m_PenWidth;
- point4[1].x=m_FirstPoint.x+3+m_PenWidth;
- point4[1].y=m_FirstPoint.y-4-m_PenWidth;
- point4[2].x=m_SecondPoint.x+3+m_PenWidth;
- point4[2].y=m_SecondPoint.y+4+m_PenWidth;
- point4[3].x=m_SecondPoint.x-3-m_PenWidth;
- point4[3].y=m_SecondPoint.y+4+m_PenWidth;
-
- }
- if((m_FirstPoint.y-m_SecondPoint.y)==0)
- {
- point4[0].x=m_FirstPoint.x-4-m_PenWidth;
- point4[0].y=m_FirstPoint.y-3-m_PenWidth;
- point4[1].x=m_FirstPoint.x-4-m_PenWidth;
- point4[1].y=m_FirstPoint.y+3+m_PenWidth;
- point4[2].x=m_SecondPoint.x+4+m_PenWidth;
- point4[2].y=m_SecondPoint.y+3+m_PenWidth;
- point4[3].x=m_SecondPoint.x+4+m_PenWidth;
- point4[3].y=m_SecondPoint.y-3-m_PenWidth;
- }
- m_FirstPoint=m_SecondPoint;
- rgn.CreatePolygonRgn(point4,4,ALTERNATE);
- if(rgn.PtInRegion(point))
- return true;
- rgn.DeleteObject();
- }
- return false;
- }
- void CPolygon::ExchangeDraw(CDC *pDC, CPoint point)
- {
- //m_FoundIndex
- CPen m_pen;
- CBrush m_brush;
- int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
- m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
- CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
- LOGBRUSH brushlog;
- brushlog.lbColor=m_BrushColor;
- brushlog.lbHatch=m_BrushHatch;
- brushlog.lbStyle=m_BrushStyle;
- m_brush.CreateBrushIndirect(&brushlog);
-
- CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
-
- int OldBkMode=pDC->SetBkMode(m_BkMode);
- COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
-
- m_SecondPoint=m_PointList->GetAt((m_FoundIndex+1)%m_PointList->GetSize());
- m_FirstPoint=m_PointList->GetAt(m_FoundIndex==0? m_PointList->GetSize()-1:m_FoundIndex-1);
-
- if(m_FoundIndex!=0)
- {
- pDC->MoveTo(m_FirstPoint);
- pDC->LineTo(point);
- pDC->MoveTo(point);
- pDC->LineTo(m_SecondPoint);
- }
- if(m_FoundIndex==0)
- {
- pDC->MoveTo(m_SecondPoint);
- pDC->LineTo(point);
- pDC->MoveTo(point);
- pDC->LineTo(m_FirstPoint);
- }
- m_PointList->SetAt(m_FoundIndex,point);
- pDC->SelectObject(brush);
- pDC->SelectObject(pen);
- pDC->SetBkMode(OldBkMode);
- pDC->SetBkColor(OldColor);
- pDC->SetROP2(oldDrawingMode);
- }
- void CPolygon::ShowSelectPoint(CDC *pDC)
- {
- CRect rect;
- CPoint firstPoint;
- CPoint secondPoint;
- firstPoint=secondPoint=m_PointList->GetAt(0);
- CBrush brush;
- brush.CreateSolidBrush(RGB(0,0,255));
- CPen m_pen;
- m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
- CPen *OldPen=pDC->SelectObject(&m_pen);
- int oldBkMode=pDC->SetBkMode(OPAQUE);
- CBrush *OldBrush=pDC->SelectObject(&brush);
- int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
- for(int i=0;i<m_PointList->GetSize();i++)
- {
- rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
- rect.InflateRect(3,3);
- pDC->Rectangle(rect);
- }
-
- pDC->SelectObject(OldBrush);
- brush.DeleteObject();
- brush.CreateSolidBrush(RGB(255,0,0));
- pDC->SelectObject(&brush);
- firstPoint=m_PointList->GetAt(0);
- //secondPoint=m_PointList->GetAt(1);
- for(i=1;i<m_PointList->GetSize();i++)
- {
- secondPoint=m_PointList->GetAt(i);
- rect.SetRect(CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2),CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2));
- rect.InflateRect(3,3);
- pDC->Rectangle(rect);
- firstPoint=secondPoint;
- }
-
- pDC->SelectObject(OldPen);
- pDC->SetBkMode(oldBkMode);
- pDC->SelectObject(OldBrush);
- pDC->SetROP2(oldDrawingMode);
- }
- void CPolygon::DrawActive(CDC *pDC,CPoint point)
- {
- CPen m_pen;
- CBrush m_brush;
-
- m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
- CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
- LOGBRUSH brushlog;
- brushlog.lbColor=m_BrushColor;
- brushlog.lbHatch=m_BrushHatch;
- brushlog.lbStyle=m_BrushStyle;
- m_brush.CreateBrushIndirect(&brushlog);
-
- CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
-
- int OldBkMode=pDC->SetBkMode(m_BkMode);
- COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
- m_FirstPoint=m_PointList->GetAt(0);
- pDC->MoveTo(m_FirstPoint);
- for(int i=0;i<m_PointList->GetSize();i++)
- {
- pDC->LineTo(m_PointList->GetAt(i));
- }
- pDC->LineTo(m_PointList->GetAt(0));
- pDC->SelectObject(brush);
- pDC->SelectObject(pen);
- pDC->SetBkMode(OldBkMode);
- pDC->SetBkColor(OldColor);
-
- }
- void CPolygon::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
- {
- if(m_HaveFindFirst)
- {
- pView->L_iPointCount=IsOnMarginPoint(point);
- if(pView->L_iPointCount>=0)
- {
- pView->m_bHaveFindSecond=true;
- pView->m_CurrentDrawTool=Polygon_Tool;
- pView->m_CurrentDrawStatus=Change_Status;
-
- }
- if(pView->L_iPointCount<-1)
- {
- pView->m_bHaveFindSecond=true;
- pView->m_CurrentDrawTool=Polygon_Tool;
- pView->m_CurrentDrawStatus=Drag_Status;
- }
- }//*******if(L_pPolygon->m_HaveFindFirst)
-
- if(!pView->m_bHaveFindSecond&&IsInRgn(point))
- {
- if(!m_HaveFindFirst)
- {
- m_HaveFindFirst=true;
- pView->m_bHaveFindFirst=true;
- ShowSelectPoint(pDC);
- }
- else
- {
- this->m_HaveFindFirst=false;
- pView->m_bHaveFindFirst=true;
- this->ShowSelectPoint(pDC);
- }
- }
- if(pView->m_bHaveFindSecond)
- {
- pView->m_pCurrentUnit=this;
- m_DrawingMode=pDC->GetROP2();
- ShowSelectPoint(pDC);
- DrawOldReferencePoint(pDC,m_FoundPoint);
- DrawStatic(pDC);
- }
- }
- void CPolygon::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
- {
- if(pView->m_CurrentDrawStatus==Draw_Status)
- {
- if(pView->m_LBDownTimes!=0)
- {
- pDC->SetROP2(R2_NOTXORPEN);
- //m_DrawingMode=pDC->GetROP2();
- DrawMask(pDC,pView->m_SecondPoint);
- pView->m_SecondPoint=point;
- pDC->SetROP2(this->m_DrawingMode);
- DrawMask(pDC,pView->m_SecondPoint);
- }
- }
- if(pView->m_CurrentDrawStatus==Change_Status)
- {
- pDC->SetROP2(R2_NOTXORPEN);
- ExchangeDraw(pDC,pView->m_SecondPoint);
- pView->m_SecondPoint=point;
- ExchangeDraw(pDC,pView->m_SecondPoint);
- }
- if(pView->m_CurrentDrawStatus==Drag_Status)
- {
- for(int i=0;i<m_PointList->GetSize();i++)
- {
- CPoint point=m_PointList->GetAt(i);
- point.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
- m_PointList->SetAt(i,point);
- }
-
-
- }
- }
|