MyCommand.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:9k
源码类别:

绘图程序

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MyDefine.h"
  3. #include "MyCommand.h"
  4. #include "resource.h"
  5. //////////////////////////////////////////////////////////////////////////////////////////////
  6. //命令结构类
  7. CCommandStruct::CCommandStruct(int command, int num, int* object, int* index)
  8. {
  9. int i;
  10. m_command = command;
  11. m_num = num;
  12. m_object = new int[m_num];
  13. m_index = new int[m_num];
  14. for(i=0; i<m_num; i++)
  15. {
  16. m_object[i] = object[i];
  17. m_index[i] = index[i];
  18. }
  19. }
  20. CCommandStruct::~CCommandStruct()
  21. {
  22. delete m_object;
  23. delete m_index;
  24. }
  25. //////////////////////////////////////////////////////////////////////////////////////////////
  26. //命令处理类
  27. IMPLEMENT_DYNCREATE(CHandleCommand, CObject)
  28. CHandleCommand::CHandleCommand()
  29. {
  30.     //设置当前省缺命令为DRAW_SELECT
  31. CurrentCommand = DRAW_SELECT;
  32. LRepeatTimes = 0;
  33. CommandPointer = -1;   //命令指针
  34. ProportionPointer = -1;
  35. PositionPointer = -1;
  36. LineStylePointer = -1;
  37. ColorPointer = -1;
  38. }
  39. CHandleCommand::~CHandleCommand()
  40. {
  41. int index = m_CommandArray.GetSize();
  42. while(index--)
  43. delete (CCommandStruct*)m_CommandArray.GetAt(index);
  44. m_CommandArray.RemoveAll();
  45. }
  46. //////////////////////////////////////////////////////////////////////////////////////////////
  47. void CHandleCommand::Intialize(COLORREF pcolor, int lstyle)
  48. {
  49. m_OperateObject.Intialize(pcolor, lstyle);
  50. }
  51. void CHandleCommand::GetView(CScrollView* pView)
  52. {
  53. pScrollView = pView;
  54. }
  55. void CHandleCommand::SetCommand(int command)
  56. {
  57. CurrentCommand = command;
  58. LRepeatTimes = 0;
  59. }
  60. int  CHandleCommand::GetCommand()
  61. {
  62. return CurrentCommand;
  63. }
  64. void CHandleCommand::SetPageSize(SIZE size)
  65. {
  66. m_OperateObject.SetPageSize(size);
  67. }
  68. //////////////////////////////////////////////////////////////////////////////////////////////
  69. void CHandleCommand::LButtonDown(CDC* pDC, POINT point)
  70. {
  71. int ObjectType;
  72. int ObjetcIndex;
  73. //获取视相对窗口的偏移量
  74. switch(CurrentCommand)
  75. {
  76. case DRAW_SELECT:
  77. m_OperateObject.SelectObject(pDC, point);
  78. break;
  79. case DRAW_LINE:
  80. {
  81. LRepeatTimes++;
  82. switch(LRepeatTimes)
  83. {
  84. case 1:
  85. m_OperateObject.StartPoint = point;
  86. m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  87. break;
  88. case 2:
  89. {   
  90. m_OperateObject.EndPoint = point;
  91. m_OperateObject.DrawLineB(pDC);
  92. m_OperateObject.AddLine();
  93. ObjectType = OBJ_LINE;
  94. ObjetcIndex = m_OperateObject.m_LineArray.GetUpperBound();
  95. //清除序号在CommandPointer + 1之后的命令
  96. //因为执行一项新操作后,将不再有可以Redo的命令
  97. DelCommandFrom(CommandPointer + 1);
  98. AddCommand(ID_DRAW_LINE, 1, &ObjectType, &ObjetcIndex);
  99. //命令指针移到命令数组末尾
  100. CommandPointer = m_CommandArray.GetUpperBound();
  101. LRepeatTimes--;
  102. m_OperateObject.StartPoint = m_OperateObject.EndPoint;
  103. m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  104. }
  105. break;
  106. }
  107. }
  108. break;
  109. case DRAW_RECT:
  110. {
  111. LRepeatTimes++;
  112. switch(LRepeatTimes)
  113. {
  114. case 1:
  115. //窗口坐标等于视坐标加视相对窗口的偏移量
  116. m_OperateObject.StartPoint = point;
  117. m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  118. break;
  119. case 2:
  120. //窗口坐标等于视坐标加视相对窗口的偏移量
  121. m_OperateObject.EndPoint = point;
  122. m_OperateObject.DrawRectB(pDC);
  123. m_OperateObject.AddRect();
  124. ObjectType = OBJ_RECT;
  125. ObjetcIndex = m_OperateObject.m_RectArray.GetUpperBound();
  126. DelCommandFrom(CommandPointer + 1);
  127. AddCommand(ID_DRAW_RECT, 1, &ObjectType, &ObjetcIndex);
  128. CommandPointer = m_CommandArray.GetUpperBound();
  129. LRepeatTimes = 0;
  130. }
  131. }
  132. break;
  133. case DRAW_CIRCLE:
  134. {
  135. LRepeatTimes++;
  136. switch(LRepeatTimes)
  137. {
  138. case 1:
  139. //窗口坐标等于视坐标加视相对窗口的偏移量
  140. m_OperateObject.StartPoint = point;
  141. m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  142. m_OperateObject.Radius = 0;
  143. break;
  144. case 2:
  145. //窗口坐标等于视坐标加视相对窗口的偏移量
  146. m_OperateObject.EndPoint = point;
  147. m_OperateObject.DrawCircleB(pDC);
  148. m_OperateObject.AddCircle();
  149. ObjectType = OBJ_CIRCLE;
  150. ObjetcIndex = m_OperateObject.m_CircleArray.GetUpperBound();
  151. DelCommandFrom(CommandPointer + 1);
  152. AddCommand(ID_DRAW_CIRCLE, 1, &ObjectType, &ObjetcIndex);
  153. CommandPointer = m_CommandArray.GetUpperBound();
  154. LRepeatTimes = 0;
  155. }
  156. }
  157. break;
  158. case DRAW_ARC:
  159. {
  160. LRepeatTimes++;
  161. switch(LRepeatTimes)
  162. {
  163. case 1:
  164. m_OperateObject.StartPoint = point;
  165. m_OperateObject.oldMidPoint = m_OperateObject.StartPoint;
  166. m_OperateObject.Radius = 0;
  167. break;
  168. case 2:
  169. m_OperateObject.MidPoint = point;
  170. break;
  171. case 3:
  172. m_OperateObject.EndPoint = point;
  173. m_OperateObject.DrawArcB(pDC);
  174. m_OperateObject.AddArc(); 
  175. ObjectType = OBJ_ARC;
  176. ObjetcIndex = m_OperateObject.m_ArcArray.GetUpperBound();
  177. DelCommandFrom(CommandPointer + 1);
  178. AddCommand(ID_DRAW_ARC, 1, &ObjectType, &ObjetcIndex);
  179. CommandPointer = m_CommandArray.GetUpperBound();
  180. LRepeatTimes = 0;
  181. break;
  182. }
  183. }
  184. break;
  185. case DRAW_TEXT:
  186. break;
  187. }
  188. }
  189. void CHandleCommand::RButtonDown(CDC* pDC, POINT point)
  190. {
  191. switch(CurrentCommand)
  192. {
  193. case DRAW_SELECT:
  194. //if(m_OperateObject.UnselectObject())
  195. //Invalidate();
  196. break;
  197. case DRAW_LINE:
  198. if(LRepeatTimes == 1)
  199. {
  200. m_OperateObject.CancelDrawLine(pDC);
  201. LRepeatTimes = 0;
  202. }
  203. else
  204. {
  205. CurrentCommand = DRAW_SELECT;
  206. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  207. LRepeatTimes = 0;
  208. }
  209. break;
  210. case DRAW_RECT:
  211. if(LRepeatTimes == 1)
  212. {
  213. m_OperateObject.CancelDrawRect(pDC);
  214. LRepeatTimes = 0;
  215. }
  216. else
  217. {
  218. CurrentCommand = DRAW_SELECT;
  219. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  220. LRepeatTimes = 0;
  221. }
  222. break;
  223. case DRAW_CIRCLE:
  224. if(LRepeatTimes == 1)
  225. {
  226. m_OperateObject.CancelDrawCircle(pDC);
  227. LRepeatTimes = 0;
  228. }
  229. else
  230. {
  231. CurrentCommand = DRAW_SELECT;
  232. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  233. LRepeatTimes = 0;
  234. }
  235. break;
  236. case DRAW_ARC:
  237. if(LRepeatTimes > 0)
  238. {
  239. m_OperateObject.CancelDrawArc(pDC, LRepeatTimes);
  240. LRepeatTimes = 0;
  241. }
  242. else
  243. {
  244. CurrentCommand = DRAW_SELECT;
  245.     SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  246. LRepeatTimes = 0;
  247. }
  248. break;
  249. }
  250. }
  251. void CHandleCommand::MouseMove(CDC* pDC, POINT point)
  252. {
  253. m_OperateObject.ScrollFlag = FALSE;  //屏幕滚动标志为FALSE
  254. switch(CurrentCommand)
  255. {
  256. case DRAW_SELECT:
  257. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  258. break;
  259. case DRAW_LINE:
  260. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  261. if(LRepeatTimes == 1)
  262. {
  263. m_OperateObject.EndPoint = point;
  264. m_OperateObject.DrawLineA(pDC);
  265. }
  266. break;
  267. case DRAW_RECT:
  268. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  269. if(LRepeatTimes == 1)
  270. {
  271. m_OperateObject.EndPoint = point;
  272. m_OperateObject.DrawRectA(pDC);
  273. }
  274. break;
  275. case DRAW_CIRCLE:
  276. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  277. if(LRepeatTimes == 1)
  278. {
  279. m_OperateObject.EndPoint = point;
  280. m_OperateObject.DrawCircleA(pDC);
  281. }
  282. break;
  283. case DRAW_ARC:
  284. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  285. switch(LRepeatTimes)
  286. {
  287. case 1:
  288. m_OperateObject.MidPoint = point;
  289. m_OperateObject.DrawArcA(pDC, LRepeatTimes);
  290. break;
  291. case 2:
  292. m_OperateObject.EndPoint = point;
  293. m_OperateObject.DrawArcA(pDC, LRepeatTimes);
  294. break;
  295. }
  296. break;
  297. }
  298. }
  299. void CHandleCommand::ScrollScreen(CDC* pDC)
  300. {
  301. switch(CurrentCommand)
  302. {
  303. case DRAW_LINE:
  304. if(LRepeatTimes == 1 && m_OperateObject.ScrollFlag == FALSE)
  305. {
  306.     m_OperateObject.CancelDrawLine(pDC);
  307. m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  308. }
  309. break;
  310. case DRAW_RECT:
  311. if(LRepeatTimes == 1 && m_OperateObject.ScrollFlag == FALSE)
  312. {
  313. m_OperateObject.CancelDrawRect(pDC);
  314. m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  315. m_OperateObject.EndPoint = m_OperateObject.StartPoint;
  316. }
  317. break;
  318. case DRAW_CIRCLE:
  319. if(LRepeatTimes == 1 && m_OperateObject.ScrollFlag == FALSE)
  320. {
  321. m_OperateObject.CancelDrawCircle(pDC);
  322.     m_OperateObject.oldEndPoint = m_OperateObject.StartPoint;
  323. m_OperateObject.EndPoint = m_OperateObject.StartPoint;
  324. m_OperateObject.Radius = 0;
  325. }
  326. break;
  327. case DRAW_ARC:
  328. if(m_OperateObject.ScrollFlag == FALSE)
  329. {
  330. m_OperateObject.CancelDrawArc(pDC, LRepeatTimes);
  331. m_OperateObject.oldMidPoint = m_OperateObject.StartPoint;
  332. m_OperateObject.Radius = 0;
  333. }
  334. break;
  335. }
  336.     
  337. m_OperateObject.ScrollFlag = TRUE; //屏幕滚动标志为TRUE
  338. }
  339. void CHandleCommand::Redraw(CDC* pDC)
  340. {
  341. //重画所有的图形对象
  342. m_OperateObject.RedrawAllObject(pDC);
  343. }
  344. //////////////////////////////////////////////////////////////////////////////////////////////
  345. //处理命令数组,用以实现UnDo、Redu命令
  346. void CHandleCommand::AddCommand(int command, int num, int* object, int* index)
  347. {
  348.    CCommandStruct* pCommand;
  349.    pCommand = new CCommandStruct(command, num, object, index);
  350.    m_CommandArray.Add(pCommand);
  351. }
  352. CCommandStruct* CHandleCommand::GetCommandAt(int index)
  353. {
  354. CCommandStruct* pCommand;
  355. pCommand = (CCommandStruct*)m_CommandArray.GetAt(index);
  356. return pCommand;
  357. }
  358. void CHandleCommand::DelCommandFrom(int index)
  359. {
  360. while(index < m_CommandArray.GetSize())
  361. {
  362. delete (CCommandStruct*)m_CommandArray.GetAt(index);
  363. m_CommandArray.RemoveAt(index);
  364. }
  365. }