MyCommand.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:9k
- #include "stdafx.h"
- #include "MyDefine.h"
- #include "MyCommand.h"
- #include "resource.h"
- //////////////////////////////////////////////////////////////////////////////////////////////
- //命令结构类
- CCommandStruct::CCommandStruct(int command, int num, int* object, int* index)
- {
- int i;
- m_command = command;
- m_num = num;
- m_object = new int[m_num];
- m_index = new int[m_num];
- for(i=0; i<m_num; i++)
- {
- m_object[i] = object[i];
- m_index[i] = index[i];
- }
- }
- CCommandStruct::~CCommandStruct()
- {
- delete m_object;
- delete m_index;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- //命令处理类
- IMPLEMENT_DYNCREATE(CHandleCommand, CObject)
- CHandleCommand::CHandleCommand()
- {
- //设置当前省缺命令为DRAW_SELECT
- CurrentCommand = DRAW_SELECT;
- LRepeatTimes = 0;
- CommandPointer = -1; //命令指针
- ProportionPointer = -1;
- PositionPointer = -1;
- LineStylePointer = -1;
- ColorPointer = -1;
- }
- CHandleCommand::~CHandleCommand()
- {
- int index = m_CommandArray.GetSize();
- while(index--)
- delete (CCommandStruct*)m_CommandArray.GetAt(index);
- m_CommandArray.RemoveAll();
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CHandleCommand::Intialize(COLORREF pcolor, int lstyle)
- {
- m_OperateObject.Intialize(pcolor, lstyle);
- }
- void CHandleCommand::GetView(CScrollView* pView)
- {
- pScrollView = pView;
- }
- void CHandleCommand::SetCommand(int command)
- {
- CurrentCommand = command;
- LRepeatTimes = 0;
- }
- int CHandleCommand::GetCommand()
- {
- return CurrentCommand;
- }
- void CHandleCommand::SetPageSize(SIZE size)
- {
- m_OperateObject.SetPageSize(size);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CHandleCommand::LButtonDown(CDC* pDC, POINT point)
- {
- int ObjectType;
- int ObjetcIndex;
- //获取视相对窗口的偏移量
- switch(CurrentCommand)
- {
- case DRAW_SELECT:
- m_OperateObject.SelectObject(pDC, point);
- break;
- case DRAW_LINE:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- m_OperateObject.StartPoint = point;
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- break;
- case 2:
- {
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawLineB(pDC);
- m_OperateObject.AddLine();
- ObjectType = OBJ_LINE;
- ObjetcIndex = m_OperateObject.m_LineArray.GetUpperBound();
- //清除序号在CommandPointer + 1之后的命令
- //因为执行一项新操作后,将不再有可以Redo的命令
- DelCommandFrom(CommandPointer + 1);
- AddCommand(ID_DRAW_LINE, 1, &ObjectType, &ObjetcIndex);
- //命令指针移到命令数组末尾
- CommandPointer = m_CommandArray.GetUpperBound();
- LRepeatTimes--;
- m_OperateObject.StartPoint = m_OperateObject.EndPoint;
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- }
- break;
- }
- }
- break;
- case DRAW_RECT:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- //窗口坐标等于视坐标加视相对窗口的偏移量
- m_OperateObject.StartPoint = point;
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- break;
- case 2:
- //窗口坐标等于视坐标加视相对窗口的偏移量
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawRectB(pDC);
- m_OperateObject.AddRect();
- ObjectType = OBJ_RECT;
- ObjetcIndex = m_OperateObject.m_RectArray.GetUpperBound();
- DelCommandFrom(CommandPointer + 1);
- AddCommand(ID_DRAW_RECT, 1, &ObjectType, &ObjetcIndex);
- CommandPointer = m_CommandArray.GetUpperBound();
- LRepeatTimes = 0;
- }
- }
- break;
- case DRAW_CIRCLE:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- //窗口坐标等于视坐标加视相对窗口的偏移量
- m_OperateObject.StartPoint = point;
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- m_OperateObject.Radius = 0;
- break;
- case 2:
- //窗口坐标等于视坐标加视相对窗口的偏移量
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawCircleB(pDC);
- m_OperateObject.AddCircle();
- ObjectType = OBJ_CIRCLE;
- ObjetcIndex = m_OperateObject.m_CircleArray.GetUpperBound();
- DelCommandFrom(CommandPointer + 1);
- AddCommand(ID_DRAW_CIRCLE, 1, &ObjectType, &ObjetcIndex);
- CommandPointer = m_CommandArray.GetUpperBound();
- LRepeatTimes = 0;
- }
- }
- break;
- case DRAW_ARC:
- {
- LRepeatTimes++;
- switch(LRepeatTimes)
- {
- case 1:
- m_OperateObject.StartPoint = point;
- m_OperateObject.oldMidPoint = m_OperateObject.StartPoint;
- m_OperateObject.Radius = 0;
- break;
- case 2:
- m_OperateObject.MidPoint = point;
- break;
- case 3:
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawArcB(pDC);
- m_OperateObject.AddArc();
- ObjectType = OBJ_ARC;
- ObjetcIndex = m_OperateObject.m_ArcArray.GetUpperBound();
- DelCommandFrom(CommandPointer + 1);
- AddCommand(ID_DRAW_ARC, 1, &ObjectType, &ObjetcIndex);
- CommandPointer = m_CommandArray.GetUpperBound();
- LRepeatTimes = 0;
- break;
- }
- }
- break;
- case DRAW_TEXT:
- break;
- }
- }
- void CHandleCommand::RButtonDown(CDC* pDC, POINT point)
- {
- switch(CurrentCommand)
- {
- case DRAW_SELECT:
- //if(m_OperateObject.UnselectObject())
- //Invalidate();
- break;
- case DRAW_LINE:
- if(LRepeatTimes == 1)
- {
- m_OperateObject.CancelDrawLine(pDC);
- LRepeatTimes = 0;
- }
- else
- {
- CurrentCommand = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- case DRAW_RECT:
- if(LRepeatTimes == 1)
- {
- m_OperateObject.CancelDrawRect(pDC);
- LRepeatTimes = 0;
- }
- else
- {
- CurrentCommand = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- case DRAW_CIRCLE:
- if(LRepeatTimes == 1)
- {
- m_OperateObject.CancelDrawCircle(pDC);
- LRepeatTimes = 0;
- }
- else
- {
- CurrentCommand = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- case DRAW_ARC:
- if(LRepeatTimes > 0)
- {
- m_OperateObject.CancelDrawArc(pDC, LRepeatTimes);
- LRepeatTimes = 0;
- }
- else
- {
- CurrentCommand = DRAW_SELECT;
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- LRepeatTimes = 0;
- }
- break;
- }
- }
- void CHandleCommand::MouseMove(CDC* pDC, POINT point)
- {
- m_OperateObject.ScrollFlag = FALSE; //屏幕滚动标志为FALSE
- switch(CurrentCommand)
- {
- case DRAW_SELECT:
- SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
- break;
- case DRAW_LINE:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- if(LRepeatTimes == 1)
- {
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawLineA(pDC);
- }
- break;
- case DRAW_RECT:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- if(LRepeatTimes == 1)
- {
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawRectA(pDC);
- }
- break;
- case DRAW_CIRCLE:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- if(LRepeatTimes == 1)
- {
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawCircleA(pDC);
- }
- break;
- case DRAW_ARC:
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
- switch(LRepeatTimes)
- {
- case 1:
- m_OperateObject.MidPoint = point;
- m_OperateObject.DrawArcA(pDC, LRepeatTimes);
- break;
- case 2:
- m_OperateObject.EndPoint = point;
- m_OperateObject.DrawArcA(pDC, LRepeatTimes);
- break;
- }
- break;
- }
-
- }
- void CHandleCommand::ScrollScreen(CDC* pDC)
- {
- switch(CurrentCommand)
- {
- case DRAW_LINE:
- if(LRepeatTimes == 1 && m_OperateObject.ScrollFlag == FALSE)
- {
- m_OperateObject.CancelDrawLine(pDC);
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- }
- break;
- case DRAW_RECT:
- if(LRepeatTimes == 1 && m_OperateObject.ScrollFlag == FALSE)
- {
- m_OperateObject.CancelDrawRect(pDC);
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- m_OperateObject.EndPoint = m_OperateObject.StartPoint;
- }
- break;
- case DRAW_CIRCLE:
- if(LRepeatTimes == 1 && m_OperateObject.ScrollFlag == FALSE)
- {
- m_OperateObject.CancelDrawCircle(pDC);
- m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
- m_OperateObject.EndPoint = m_OperateObject.StartPoint;
- m_OperateObject.Radius = 0;
- }
- break;
- case DRAW_ARC:
- if(m_OperateObject.ScrollFlag == FALSE)
- {
- m_OperateObject.CancelDrawArc(pDC, LRepeatTimes);
- m_OperateObject.oldMidPoint = m_OperateObject.StartPoint;
- m_OperateObject.Radius = 0;
- }
- break;
- }
-
- m_OperateObject.ScrollFlag = TRUE; //屏幕滚动标志为TRUE
- }
- void CHandleCommand::Redraw(CDC* pDC)
- {
- //重画所有的图形对象
- m_OperateObject.RedrawAllObject(pDC);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- //处理命令数组,用以实现UnDo、Redu命令
- void CHandleCommand::AddCommand(int command, int num, int* object, int* index)
- {
- CCommandStruct* pCommand;
- pCommand = new CCommandStruct(command, num, object, index);
- m_CommandArray.Add(pCommand);
- }
- CCommandStruct* CHandleCommand::GetCommandAt(int index)
- {
- CCommandStruct* pCommand;
- pCommand = (CCommandStruct*)m_CommandArray.GetAt(index);
- return pCommand;
- }
- void CHandleCommand::DelCommandFrom(int index)
- {
- while(index < m_CommandArray.GetSize())
- {
- delete (CCommandStruct*)m_CommandArray.GetAt(index);
- m_CommandArray.RemoveAt(index);
- }
- }