MyManageGraphObj.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:30k
- #include "stdafx.h"
- #include "MyManageGraphObj.h"
- #include "math.h"
- #include "MyDefine.h"
- #include "resource.h"
- //被选择图形对象信息
- CSelectedObjectInfo::CSelectedObjectInfo(int type, int index)
- {
- m_type = type;
- m_index = index;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNCREATE(CManageGraphObject, CObject)
- CManageGraphObject::CManageGraphObject()
- {
- Proportion = 1.0;
- LRepeatTimes = 0;
- RRepeatTimes = 0;
- }
- CManageGraphObject::~CManageGraphObject()
- {
- DelAllLine();
- DelAllRect();
- DelAllCircle();
- DelAllArc();
- }
- void CManageGraphObject::Initialize(int style, COLORREF color)
- {
- LineStyle = style;
- PenColor = color;
- }
- void CManageGraphObject::SetLineStyle(int style)
- {
- LineStyle = style;
- }
- void CManageGraphObject::SetPenColor(COLORREF color)
- {
- PenColor = color;
- }
- //获取命令堆栈指针
- void CManageGraphObject::GetCommandStack(CCommandStack* pStack)
- {
- pCommandStack = pStack;
- }
- void CManageGraphObject::GetDC(CClientDC* pClientDC)
- {
- ASSERT(pClientDC != NULL);
- pDC = pClientDC;
- }
- void CManageGraphObject::Serialize(CArchive& ar)
- {
- int num;
- int index;
- CGraphObjectInfo* pLineInfo;
- CGraphObjectInfo* pRectInfo;
- CGraphObjectInfo* pCircleInfo;
- CGraphObjectInfo* pArcInfo;
- if(ar.IsStoring())
- {
- ar << m_LineArray.GetSize() << m_RectArray.GetSize()
- << m_CircleArray.GetSize() << m_ArcArray.GetSize();
- }
- else
- {
- ar >> num;
- for(index = 0; index < num; index++)
- {
- pLineInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_LineInfoArray.Add(pLineInfo);
- }
- ar >> num;
- for(index = 0; index < num; index++)
- {
- pRectInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_RectInfoArray.Add(pRectInfo);
- }
- ar >> num;
- for(index = 0; index < num; index++)
- {
- pCircleInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_CircleInfoArray.Add(pCircleInfo);
- }
- ar >> num;
- for(index = 0; index < num; index++)
- {
- pArcInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_ArcInfoArray.Add(pArcInfo);
- }
- }
- //注意当打开文件时,下面的Serialize()函数会自动创建CMyLine,CMyRect等对象
- m_LineArray.Serialize(ar);
- m_RectArray.Serialize(ar);
- m_CircleArray.Serialize(ar);
- m_ArcArray.Serialize(ar);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- //处理命令
- void CManageGraphObject::OnLButtonDown(UINT ID, CPoint point)
- {
- ASSERT(pDC != NULL);
- ASSERT(pCommandStack != NULL);
- int ObjectType;
- int ObjetcIndex;
- switch(ID)
- {
- case DRAW_SELECT:
- SelectObject(pDC, point);
- break;
- case DRAW_LINE:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- StartPoint = point;
- m_DrawGrapObject.SetOnceFlag();
- break;
- case 2:
- EndPoint = point;
- m_DrawGrapObject.DrawLine(pDC, StartPoint, EndPoint, LineStyle, PenColor);
- AddLine(StartPoint, EndPoint, LineStyle, PenColor);
- ObjectType = OBJECT_LINE;
- ObjetcIndex = m_LineArray.GetUpperBound();
- //因为执行一项新操作后,将不再有可以Redo的命令
- pCommandStack->PushCommand(DRAW_LINE, 1, &ObjectType, &ObjetcIndex);
-
- LRepeatTimes--;
- StartPoint = EndPoint;
- oldEndPoint = StartPoint;
- break;
- }
- }
- break;
- case DRAW_RECT:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- StartPoint = point;
- m_DrawGrapObject.SetOnceFlag();
- break;
- case 2:
- EndPoint = point;
- m_DrawGrapObject.DrawRect(pDC, StartPoint, EndPoint, LineStyle, PenColor);
- AddRect(StartPoint, EndPoint, LineStyle, PenColor);
- ObjectType = OBJECT_RECT;
- ObjetcIndex = m_RectArray.GetUpperBound();
- pCommandStack->PushCommand(DRAW_RECT, 1, &ObjectType, &ObjetcIndex);
-
- LRepeatTimes = 0;
- break;
- }
- }
- break;
- case DRAW_CIRCLE:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- StartPoint = point;
- m_DrawGrapObject.SetOnceFlag();
- break;
- case 2:
- EndPoint = point;
- m_DrawGrapObject.DrawCircle(pDC, StartPoint, EndPoint, LineStyle, PenColor);
- AddCircle(StartPoint, EndPoint, LineStyle, PenColor);
- ObjectType = OBJECT_CIRCLE;
- ObjetcIndex = m_CircleArray.GetUpperBound();
- pCommandStack->PushCommand(DRAW_CIRCLE, 1, &ObjectType, &ObjetcIndex);
- LRepeatTimes = 0;
- break;
- }
- }
- break;
- case DRAW_ARC:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- StartPoint = point;
- m_DrawGrapObject.SetOnceFlag();
- break;
- case 2:
- MidPoint = point;
- m_DrawGrapObject.SetOnceFlag();
- break;
- case 3:
- EndPoint = point;
- m_DrawGrapObject.DrawArc(pDC, StartPoint, MidPoint, EndPoint,
- LineStyle, PenColor);
- AddArc(StartPoint, MidPoint, EndPoint, LineStyle, PenColor);
- ObjectType = OBJECT_ARC;
- ObjetcIndex = m_ArcArray.GetUpperBound();
- pCommandStack->PushCommand(DRAW_ARC, 1, &ObjectType, &ObjetcIndex);
-
- LRepeatTimes = 0;
- break;
- }
- }
- break;
- case DRAW_TEXT:
- break;
- }
- }
- UINT CManageGraphObject::OnRButtonDown(UINT ID, CPoint point)
- {
- ASSERT(pDC != NULL);
- switch(ID)
- {
- case DRAW_SELECT:
- //if(UnselectObject())
- //Invalidate();
- break;
- case DRAW_LINE:
- if(LRepeatTimes == 1)
- {
- m_DrawGrapObject.CancelDrawLine(pDC, StartPoint, EndPoint);
- LRepeatTimes = 0;
- }
- else
- {
- ID = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- case DRAW_RECT:
- if(LRepeatTimes == 1)
- {
- m_DrawGrapObject.CancelDrawRect(pDC, StartPoint, EndPoint);
- LRepeatTimes = 0;
- }
- else
- {
- ID = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- case DRAW_CIRCLE:
- if(LRepeatTimes == 1)
- {
- m_DrawGrapObject.CancelDrawCircle(pDC, StartPoint, EndPoint);
- LRepeatTimes = 0;
- }
- else
- {
- ID = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- case DRAW_ARC:
- if(LRepeatTimes > 0)
- {
- m_DrawGrapObject.CancelDrawArc(pDC, StartPoint, MidPoint, EndPoint, LRepeatTimes);
- LRepeatTimes = 0;
- }
- else
- {
- ID = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- }
- return ID;
- }
- void CManageGraphObject::OnMouseMove(UINT ID, CPoint point)
- {
- ASSERT(pDC != NULL);
- switch(ID)
- {
- case DRAW_SELECT:
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- break;
- case DRAW_LINE:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- if(LRepeatTimes == 1)
- {
- EndPoint = point;
- m_DrawGrapObject.DrawLineXOR(pDC, StartPoint, EndPoint, oldEndPoint);
- oldEndPoint = EndPoint;
- }
- break;
- case DRAW_RECT:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- if(LRepeatTimes == 1)
- {
- EndPoint = point;
- m_DrawGrapObject.DrawRectXOR(pDC, StartPoint, EndPoint, oldEndPoint);
- oldEndPoint = EndPoint;
- }
- break;
- case DRAW_CIRCLE:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- if(LRepeatTimes == 1)
- {
- EndPoint = point;
- m_DrawGrapObject.DrawCircleXOR(pDC, StartPoint, EndPoint, oldEndPoint);
- oldEndPoint = EndPoint;
- }
- break;
- case DRAW_ARC:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- switch(LRepeatTimes)
- {
- case 1:
- MidPoint = point;
- m_DrawGrapObject.DrawArcXOR(pDC, StartPoint, MidPoint, oldMidPoint,
- EndPoint, oldEndPoint, LRepeatTimes);
- oldMidPoint = MidPoint;
- break;
- case 2:
- EndPoint = point;
- m_DrawGrapObject.DrawArcXOR(pDC, StartPoint, MidPoint, oldMidPoint,
- EndPoint, oldEndPoint, LRepeatTimes);
- oldEndPoint = EndPoint;
- break;
- }
- break;
- }
- }
- void CManageGraphObject::OnScroll(UINT ID)
- {
- ASSERT(pDC != NULL);
- switch(ID)
- {
- case DRAW_LINE:
- if(LRepeatTimes == 1)
- {
- m_DrawGrapObject.CancelDrawLine(pDC, StartPoint, EndPoint);
- oldEndPoint = StartPoint;
- EndPoint = StartPoint;
- m_DrawGrapObject.SetOnceFlag();
- }
- break;
- case DRAW_RECT:
- if(LRepeatTimes == 1)
- {
- m_DrawGrapObject.CancelDrawRect(pDC, StartPoint, EndPoint);
- oldEndPoint = StartPoint;
- EndPoint = StartPoint;
- m_DrawGrapObject.SetOnceFlag();
- }
- break;
- case DRAW_CIRCLE:
- if(LRepeatTimes == 1)
- {
- m_DrawGrapObject.CancelDrawCircle(pDC, StartPoint, EndPoint);
- oldEndPoint = StartPoint;
- EndPoint = StartPoint;
- m_DrawGrapObject.SetOnceFlag();
- }
- break;
- case DRAW_ARC:
- m_DrawGrapObject.CancelDrawArc(pDC, StartPoint, MidPoint, EndPoint, LRepeatTimes);
- if(LRepeatTimes == 1)
- {
- oldMidPoint = StartPoint;
- MidPoint = StartPoint;
- }
- if(LRepeatTimes == 2)
- {
- oldEndPoint = StartPoint;
- EndPoint = StartPoint;
- }
- m_DrawGrapObject.SetOnceFlag();
- break;
- }
-
- }
- //实现“撤销”命令
- void CManageGraphObject::OnUndo()
- {
- CCommandStruct* pCommand;
- CGraphObjectInfo* pLineInfo;
- CGraphObjectInfo* pRectInfo;
- CGraphObjectInfo* pCircleInfo;
- CGraphObjectInfo* pArcInfo;
- pCommand = (CCommandStruct*)pCommandStack->Undo();
- if(pCommand == NULL)
- return;
- switch(pCommand->m_command)
- {
- case DRAW_LINE:
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[0]);
- pLineInfo->m_del = TRUE;
- break;
- case DRAW_RECT:
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[0]);
- pRectInfo->m_del = TRUE;
- break;
- case DRAW_CIRCLE:
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[0]);
- pCircleInfo->m_del = TRUE;
- break;
- case DRAW_ARC:
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[0]);
- pArcInfo->m_del = TRUE;
- break;
- case CHANGE_DELETE:
- for(int index =0; index < pCommand->m_num; index++)
- {
- switch(pCommand->m_object[index])
- {
- case OBJECT_LINE:
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[index]);
- pLineInfo->m_del = FALSE;
- break;
- case OBJECT_RECT:
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[index]);
- pRectInfo->m_del = FALSE;
- break;
- case OBJECT_CIRCLE:
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[index]);
- pCircleInfo->m_del = FALSE;
- break;
- case OBJECT_ARC:
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[index]);
- pArcInfo->m_del = FALSE;
- break;
- }
- }
- break;
- }
- }
- //实现“重做”命令
- void CManageGraphObject::OnRedo()
- {
- CCommandStruct* pCommand;
- CGraphObjectInfo* pLineInfo;
- CGraphObjectInfo* pRectInfo;
- CGraphObjectInfo* pCircleInfo;
- CGraphObjectInfo* pArcInfo;
- pCommand = (CCommandStruct*)pCommandStack->Redo();
- if(pCommand == NULL)
- return;
- switch(pCommand->m_command)
- {
- case DRAW_LINE:
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[0]);
- pLineInfo->m_del = FALSE;
- break;
- case DRAW_RECT:
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[0]);
- pRectInfo->m_del = FALSE;
- break;
- case DRAW_CIRCLE:
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[0]);
- pCircleInfo->m_del = FALSE;
- break;
- case DRAW_ARC:
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[0]);
- pArcInfo->m_del = FALSE;
- break;
- case CHANGE_DELETE:
- for(int index =0; index < pCommand->m_num; index++)
- {
- switch(pCommand->m_object[index])
- {
- case OBJECT_LINE:
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[index]);
- pLineInfo->m_del = TRUE;
- break;
- case OBJECT_RECT:
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[index]);
- pRectInfo->m_del = TRUE;
- break;
- case OBJECT_CIRCLE:
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[index]);
- pCircleInfo->m_del = TRUE;
- break;
- case OBJECT_ARC:
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[index]);
- pArcInfo->m_del = TRUE;
- break;
- }
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- POINT CManageGraphObject::DrawingToLogic(FPOINT fpoint)
- {
- POINT point;
- point.x = long(fpoint.x * Proportion) + EXTRA_WIDTH;
- point.y = PageSize.cy - (long)(fpoint.y * Proportion) - EXTRA_HIGTH;
- return point;
- }
- long CManageGraphObject::DrawingToLogic(float fdistance)
- {
- long distance;
- distance = (long)(fdistance * Proportion);
- return distance;
- }
- FPOINT CManageGraphObject::LogicToDrawing(POINT point)
- {
- FPOINT fpoint;
- fpoint.x = (float)(point.x - EXTRA_WIDTH) / Proportion;
- fpoint.y = (float)(PageSize.cy - point.y - EXTRA_HIGTH) / Proportion;
- return fpoint;
- }
- float CManageGraphObject::LogicToDrawing(long distance)
- {
- float fdistance;
- fdistance = (float)distance / Proportion;
- return fdistance;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- void CManageGraphObject::RedrawAllObject(CDC* pDC)
- {
- RedrawLine(pDC);
- RedrawRect(pDC);
- RedrawCircle(pDC);
- RedrawArc(pDC);
- }
- void CManageGraphObject::SelectObject(CDC* pDC, CPoint point)
- {
- SelectLine(pDC, point);
- SelectRect(pDC, point);
- SelectCircle(pDC, point);
- SelectArc(pDC, point);
- }
- BOOL CManageGraphObject::UnselectObject()
- {
- BOOL flag = FALSE;
- int nIndex;
-
- if(UnselectLine())
- flag = TRUE;
- if(UnselectRect())
- flag = TRUE;
- if(UnselectCircle())
- flag = TRUE;
- if(UnselectArc())
- flag = TRUE;
-
- nIndex = m_SelObjInfoArray.GetSize();
- while(nIndex--)
- delete (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
- m_SelObjInfoArray.RemoveAll();
- return flag;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CManageGraphObject::RedrawLine(CDC* pDC)
- {
- int index;
- CMyLine* pLine;
- CGraphObjectInfo* pLineInfo;
- index = m_LineArray.GetSize();
- while(index--)
- {
- pLine = (CMyLine*)m_LineArray.GetAt(index);
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
- if(pLineInfo->m_del == TRUE)
- continue;
- if(pLineInfo->m_selected == TRUE)
- {
- m_DrawGrapObject.DrawSelLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
- pLine->m_style, pLine->m_color);
- continue;
- }
-
- m_DrawGrapObject.DrawLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
- pLine->m_style, pLine->m_color);
- }
- }
- void CManageGraphObject::SelectLine(CDC* pDC, CPoint point)
- {
- int nIndex;
- long distance;
- CRect rect;
- CGraphObjectInfo* pLineInfo;
- CSelectedObjectInfo* pSelObjInfo;
- CMyLine* pLine;
- for(nIndex = 0; nIndex < m_LineInfoArray.GetSize(); nIndex++)
- {
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(nIndex);
- if(pLineInfo ->m_del == TRUE || pLineInfo ->m_selected ==TRUE)
- continue;
- pLine = (CMyLine*)m_LineArray.GetAt(nIndex);
- rect.left = DrawingToLogic(pLine->m_start).x;
- rect.top = DrawingToLogic(pLine->m_start).y;
- rect.right = DrawingToLogic(pLine->m_end).x;
- rect.bottom = DrawingToLogic(pLine->m_end).y;
- if( (point.x >= min(rect.left, rect.right) - SELECT_RANGE) && (point.x <= max(rect.left, rect.right) + SELECT_RANGE) &&
- (point.y >= min(rect.top, rect.bottom) - SELECT_RANGE) && (point.y <= max(rect.top, rect.bottom) + SELECT_RANGE) )
- {
- distance = (long)( abs( (rect.bottom - rect.top) * point.x - (rect.right - rect.left) * point.y + rect.top * (rect.right - rect.left) - rect.left * (rect.bottom - rect.top) )
- / sqrt( (rect.right - rect.left) * (rect.right - rect.left) + (rect.bottom - rect.top) * (rect.bottom - rect.top) ) );
- if(distance <= SELECT_RANGE)
- {
- m_DrawGrapObject.DrawSelLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
- pLine->m_style, pLine->m_color);
- pLineInfo->m_selected = TRUE;
- pSelObjInfo = new CSelectedObjectInfo(OBJECT_LINE, nIndex);
- m_SelObjInfoArray.Add(pSelObjInfo);
- }
- }
- }
- }
- BOOL CManageGraphObject::UnselectLine()
- {
- BOOL flag = FALSE;
- int nIndex;
- CSelectedObjectInfo* pSelObjInfo;
- CGraphObjectInfo* pLineInfo;
- for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
- {
- pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
- if(pSelObjInfo->m_type ==OBJECT_LINE)
- {
- pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pSelObjInfo->m_index);
- pLineInfo->m_selected = FALSE;
- flag = TRUE;
- }
- }
- return flag;
- }
- void CManageGraphObject::AddLine(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
- {
- FPOINT start;
- FPOINT end;
- start = LogicToDrawing(SPoint);
- end = LogicToDrawing(EPoint);
- CMyLine* pMyLine = new CMyLine(start, end, Style, Color);
- m_LineArray.Add(pMyLine);
- CGraphObjectInfo * pLineInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_LineInfoArray.Add(pLineInfo);
- }
- CMyLine* CManageGraphObject::GetLine(int index)
- {
- if(index < 0 || index > m_LineArray.GetUpperBound())
- return 0;
- return (CMyLine*)m_LineArray.GetAt(index);
- }
- void CManageGraphObject::DelAllLine()
- {
- int index;
- index = m_LineArray.GetSize();
- while(index--)
- delete (CMyLine*)m_LineArray.GetAt(index);
- m_LineArray.RemoveAll();
-
- index = m_LineInfoArray.GetSize();
- while(index--)
- delete (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
- m_LineInfoArray.RemoveAll();
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CManageGraphObject::RedrawRect(CDC* pDC)
- {
- int index;
- CMyRect* pRect;
- CGraphObjectInfo* pRectInfo;
- index = m_RectArray.GetSize();
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- while(index--)
- {
- pRect = (CMyRect*)m_RectArray.GetAt(index);
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
- if(pRectInfo->m_del == TRUE)
- continue;
- if(pRectInfo->m_selected == TRUE)
- {
- m_DrawGrapObject.DrawSelRect(pDC, DrawingToLogic(pRect->m_start), DrawingToLogic(pRect->m_end),
- pRect->m_style, pRect->m_color);
- continue;
- }
- m_DrawGrapObject.DrawRect(pDC, DrawingToLogic(pRect->m_start), DrawingToLogic(pRect->m_end),
- pRect->m_style, pRect->m_color);
-
- }
- }
- void CManageGraphObject::SelectRect(CDC* pDC, CPoint point)
- {
- int nIndex;
- CRect rect;
- CGraphObjectInfo* pRectInfo;
- CSelectedObjectInfo* pSelObjInfo;
- CMyRect* pRect;
- for(nIndex = 0; nIndex < m_RectInfoArray.GetSize(); nIndex++)
- {
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(nIndex);
- if(pRectInfo ->m_del == TRUE || pRectInfo ->m_selected ==TRUE)
- continue;
- pRect = (CMyRect*)m_RectArray.GetAt(nIndex);
- rect.left = DrawingToLogic(pRect->m_start).x;
- rect.top = DrawingToLogic(pRect->m_start).y;
- rect.right = DrawingToLogic(pRect->m_end).x;
- rect.bottom = DrawingToLogic(pRect->m_end).y;
- if( ( (point.x >= min(rect.left, rect.right) - SELECT_RANGE) && (point.x <= max(rect.left, rect.right) + SELECT_RANGE) &&
- (point.y >= min(rect.top, rect.bottom) - SELECT_RANGE) && (point.y <= max(rect.top, rect.bottom) + SELECT_RANGE) ) &&
- ( (point.x <= min(rect.left, rect.right) + SELECT_RANGE) || (point.x >= max(rect.left, rect.right) - SELECT_RANGE) ||
- (point.y <= min(rect.top, rect.bottom) + SELECT_RANGE) || (point.y >= max(rect.top, rect.bottom) - SELECT_RANGE) ) )
- {
- m_DrawGrapObject.DrawSelRect(pDC, rect.TopLeft(), rect.BottomRight(),
- pRect->m_style, pRect->m_color);
- pRectInfo->m_selected = TRUE;
- pSelObjInfo = new CSelectedObjectInfo(OBJECT_RECT, nIndex);
- m_SelObjInfoArray.Add(pSelObjInfo);
- }
- }
- }
- BOOL CManageGraphObject::UnselectRect()
- {
- BOOL flag = FALSE;
- int nIndex;
- CSelectedObjectInfo* pSelObjInfo;
- CGraphObjectInfo* pRectInfo;
- for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
- {
- pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
- if(pSelObjInfo->m_type ==OBJECT_RECT)
- {
- pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pSelObjInfo->m_index);
- pRectInfo->m_selected = FALSE;
- flag = TRUE;
- }
- }
- return flag;
- }
- void CManageGraphObject::AddRect(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
- {
- FPOINT start;
- FPOINT end;
-
- start = LogicToDrawing(SPoint);
- end = LogicToDrawing(EPoint);
-
- CMyRect* pMyRect = new CMyRect(start, end, Style, Color);
- m_RectArray.Add(pMyRect);
- CGraphObjectInfo * pRectInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_RectInfoArray.Add(pRectInfo);
- }
- CMyRect* CManageGraphObject::GetRect(int index)
- {
- if(index < 0 || index > m_RectArray.GetUpperBound())
- return 0;
- return (CMyRect*)m_RectArray.GetAt(index);
- }
- void CManageGraphObject::DelAllRect()
- {
- int index;
- index = m_RectArray.GetSize();
- while(index--)
- delete (CMyRect*)m_RectArray.GetAt(index);
- m_RectArray.RemoveAll();
- index = m_RectInfoArray.GetSize();
- while(index--)
- delete (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
- m_RectInfoArray.RemoveAll();
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CManageGraphObject::RedrawCircle(CDC* pDC)
- {
- int index;
- CMyCircle* pCircle;
- CGraphObjectInfo* pCircleInfo;
- index = m_CircleArray.GetSize();
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- while(index--)
- {
- pCircle = (CMyCircle*)m_CircleArray.GetAt(index);
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
- if(pCircleInfo->m_del == TRUE)
- continue;
- if(pCircleInfo->m_selected == TRUE)
- {
- m_DrawGrapObject.DrawSelCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
- pCircle->m_style, pCircle->m_color);
- continue;
- }
- m_DrawGrapObject.DrawCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
- pCircle->m_style, pCircle->m_color);
- }
- }
- void CManageGraphObject::SelectCircle(CDC* pDC, CPoint point)
- {
- int nIndex;
- POINT origin;
- long r;
- long distance;
- CGraphObjectInfo* pCircleInfo;
- CSelectedObjectInfo* pSelObjInfo;
- CMyCircle* pCircle;
- for(nIndex = 0; nIndex < m_CircleInfoArray.GetSize(); nIndex++)
- {
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(nIndex);
- if(pCircleInfo ->m_del == TRUE || pCircleInfo ->m_selected ==TRUE)
- continue;
- pCircle = (CMyCircle*)m_CircleArray.GetAt(nIndex);
- origin = DrawingToLogic(pCircle->m_origin);
- r = DrawingToLogic(pCircle->m_radius);
- distance = (long)sqrt( (origin.x - point.x) * (origin.x - point.x) + (origin.y - point.y) * (origin.y - point.y) );
- if(distance <= r + SELECT_RANGE && distance >= r - SELECT_RANGE)
- {
- m_DrawGrapObject.DrawSelCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
- pCircle->m_style, pCircle->m_color);
- pCircleInfo->m_selected = TRUE;
- pSelObjInfo = new CSelectedObjectInfo(OBJECT_CIRCLE, nIndex);
- m_SelObjInfoArray.Add(pSelObjInfo);
- }
- }
- }
- BOOL CManageGraphObject::UnselectCircle()
- {
- BOOL flag = FALSE;
- int nIndex;
- CSelectedObjectInfo* pSelObjInfo;
- CGraphObjectInfo* pCircleInfo;
- for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
- {
- pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
- if(pSelObjInfo->m_type ==OBJECT_CIRCLE)
- {
- pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pSelObjInfo->m_index);
- pCircleInfo->m_selected = FALSE;
- flag = TRUE;
- }
- }
- return flag;
- }
- void CManageGraphObject::AddCircle(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
- {
- FPOINT start;
- FPOINT end;
- float r;
-
- start = LogicToDrawing(SPoint);
- end = LogicToDrawing(EPoint);
- r = (float)sqrt(pow(start.x - end.x, 2) + pow(start.y - end.y, 2));
- CMyCircle* pMyCircle = new CMyCircle(start, r, Style, Color);
- m_CircleArray.Add(pMyCircle);
- CGraphObjectInfo* pCircleInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_CircleInfoArray.Add(pCircleInfo);
- }
- CMyCircle* CManageGraphObject::GetCircle(int index)
- {
- if(index < 0 || index > m_CircleArray.GetUpperBound())
- return 0;
- return (CMyCircle*)m_CircleArray.GetAt(index);
- }
- void CManageGraphObject::DelAllCircle()
- {
- int index;
- index = m_CircleArray.GetSize();
- while(index--)
- delete (CMyCircle*)m_CircleArray.GetAt(index);
- m_CircleArray.RemoveAll();
- index = m_CircleInfoArray.GetSize();
- while(index--)
- delete (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
- m_CircleInfoArray.RemoveAll();
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CManageGraphObject::RedrawArc(CDC* pDC)
- {
- int index;
- CMyArc* pArc;
- CGraphObjectInfo* pArcInfo;
- index = m_ArcArray.GetSize();
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- while(index--)
- {
- pArc = (CMyArc*)m_ArcArray.GetAt(index);
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
- if(pArcInfo->m_del == TRUE)
- continue;
- if(pArcInfo->m_selected == TRUE)
- {
- m_DrawGrapObject.DrawSelArc(pDC, DrawingToLogic(pArc->m_origin), DrawingToLogic(pArc->m_start),
- DrawingToLogic(pArc->m_end),
- DrawingToLogic(pArc->m_radius), LineStyle, PenColor);
- continue;
- }
-
-
- m_DrawGrapObject.DrawArc(pDC, DrawingToLogic(pArc->m_origin),
- DrawingToLogic(pArc->m_start), DrawingToLogic(pArc->m_end),
- DrawingToLogic(pArc->m_radius), pArc->m_style, pArc->m_color);
- }
- }
- void CManageGraphObject::SelectArc(CDC* pDC, CPoint point)
- {
- int nIndex;
- POINT origin;
- long r;
- long distance;
- CPoint start;
- CPoint end;
- CGraphObjectInfo* pArcInfo;
- CSelectedObjectInfo* pSelObjInfo;
- CMyArc* pArc;
- for(nIndex = 0; nIndex < m_ArcInfoArray.GetSize(); nIndex++)
- {
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(nIndex);
- if(pArcInfo ->m_del == TRUE || pArcInfo ->m_selected ==TRUE)
- continue;
- pArc = (CMyArc*)m_ArcArray.GetAt(nIndex);
- origin = DrawingToLogic(pArc->m_origin);
- start = DrawingToLogic(pArc->m_start);
- end = DrawingToLogic(pArc->m_end);
- r = DrawingToLogic(pArc->m_radius);
- distance = (long)sqrt( (origin.x - point.x) * (origin.x - point.x) + (origin.y - point.y) * (origin.y - point.y) );
- if(distance <= r + SELECT_RANGE && distance >= r - SELECT_RANGE)
- {
- m_DrawGrapObject.DrawSelArc(pDC, origin, start, end, r, pArc->m_style, pArc->m_color);
- pArcInfo->m_selected = TRUE;
- pSelObjInfo = new CSelectedObjectInfo(OBJECT_ARC, nIndex);
- m_SelObjInfoArray.Add(pSelObjInfo);
- }
- }
- }
- BOOL CManageGraphObject::UnselectArc()
- {
- BOOL flag = FALSE;
- int nIndex;
- CSelectedObjectInfo* pSelObjInfo;
- CGraphObjectInfo* pArcInfo;
- for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
- {
- pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
- if(pSelObjInfo->m_type ==OBJECT_ARC)
- {
- pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pSelObjInfo->m_index);
- pArcInfo->m_selected = FALSE;
- flag = TRUE;
- }
- }
- return flag;
- }
- void CManageGraphObject::AddArc(POINT SPoint, POINT MPoint, POINT EPoint, int Style, COLORREF Color)
- {
- long x1, y1, x2, y2;
- double originX;
- double originY;
- double k1;
- double k2;
- double mid1X;
- double mid1Y;
- double mid2X;
- double mid2Y;
- POINT OPoint;
- long Radius;
- CMyArc* pMyArc;
-
- if(SPoint.y == MPoint.y)
- k1 = 1000000.0;
- else
- k1 = (- (double)(SPoint.x - MPoint.x) / (double)(SPoint.y - MPoint.y));
- if(EPoint.y == MPoint.y)
- k2 = 1000000.0;
- else
- k2 = (- (double)(EPoint.x - MPoint.x) / (double)(EPoint.y - MPoint.y));
- mid1X = (double)(SPoint.x + MPoint.x) / 2.0;
- mid1Y = (double)(SPoint.y + MPoint.y) / 2.0;
- mid2X = (double)(EPoint.x + MPoint.x) / 2.0;
- mid2Y = (double)(EPoint.y + MPoint.y) / 2.0;
- if(k1 == k2)
- return;
-
- if(k1 == 1000000.0)
- {
- originX = mid1X;
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- else
- if(k2 == 1000000.0)
- {
- originX = mid2X;
- originY = k1 * (originX - mid1X) + mid1Y;
- }
- else
- {
- originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- Radius = (long)sqrt(pow((double)(originX - SPoint.x), 2) + pow((double)(originY - SPoint.y), 2));
- OPoint.x = (long)originX;
- OPoint.y = (long)originY ;
- x1 = MidPoint.x - SPoint.x;
- y1 = MPoint.y - SPoint.y;
- x2 = EPoint.x - SPoint.x;
- y2 = EPoint.y - SPoint.y;
- if(x1*y2 - x2*y1 < 0)
- pMyArc = new CMyArc(LogicToDrawing(OPoint), LogicToDrawing(SPoint),
- LogicToDrawing(EPoint), LogicToDrawing(Radius),
- LineStyle, PenColor);
- else
- pMyArc = new CMyArc(LogicToDrawing(OPoint), LogicToDrawing(EPoint),
- LogicToDrawing(SPoint), LogicToDrawing(Radius),
- LineStyle, PenColor);
-
- m_ArcArray.Add(pMyArc);
- CGraphObjectInfo* pArcInfo = new CGraphObjectInfo(0, FALSE, FALSE);
- m_ArcInfoArray.Add(pArcInfo);
- }
- CMyArc* CManageGraphObject::GetArc(int index)
- {
- if(index < 0 || index > m_ArcArray.GetUpperBound())
- return 0;
- return (CMyArc*)m_ArcArray.GetAt(index);
- }
- void CManageGraphObject::DelAllArc()
- {
- int index;
- index = m_ArcArray.GetSize();
- while(index--)
- delete (CMyArc*)m_ArcArray.GetAt(index);
- m_ArcArray.RemoveAll();
- index = m_ArcInfoArray.GetSize();
- while(index--)
- delete (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
- m_ArcInfoArray.RemoveAll();
- }