MainFrame.cpp
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:5k
源码类别:

CAD

开发平台:

Visual C++

  1. // MainFrame.cpp: implementation of the CMainFrame class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "MainFrame.h"
  6. #include "resource.h"
  7. #include "WinApp.h"
  8. #include "Shape.h"
  9. extern CWinApp g_theApp;
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CMainFrame::CMainFrame()
  14. {
  15. }
  16. CMainFrame::~CMainFrame()
  17. {
  18. }
  19. LRESULT CMainFrame::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  20. {
  21. CMainFrame   *pMainWnd     = &(g_theApp.m_MainFrame);
  22. CManagerTool *pManagerTool = &(g_theApp.m_ManagerTool);
  23. switch (message) 
  24. {
  25. case WM_COMMAND:
  26. {
  27. pMainWnd->OnCommand(hWnd,message,wParam,lParam);
  28. }
  29. break;
  30. case WM_LBUTTONUP:
  31. case WM_LBUTTONDOWN:
  32. case WM_RBUTTONDOWN:
  33. case WM_MOUSEMOVE:
  34. case WM_NCMOUSEMOVE:
  35. case WM_SETCURSOR:
  36. {
  37. //动态绘制
  38. if(pManagerTool != NULL && pManagerTool->m_pCurrentTool != NULL)
  39. {
  40. pManagerTool->DrawObject(hWnd,message,wParam,lParam);
  41. }
  42. //镜像
  43. else if(pManagerTool->MirrorFlag == TRUE)
  44. {
  45. pManagerTool->MirrorFlag = pManagerTool->MirrorObject(hWnd,message,wParam,lParam);
  46. if (pManagerTool->MirrorFlag == FALSE)
  47. InvalidateRect(hWnd,NULL,TRUE);
  48. }
  49. //编辑图形
  50. else
  51. {
  52. BOOL isModify = FALSE;
  53. isModify = pManagerTool->EditObject(hWnd,message,wParam,lParam);
  54. if (isModify == TRUE) InvalidateRect(hWnd,NULL,TRUE);
  55. }
  56. }
  57. break;
  58. case WM_PAINT:
  59. {
  60. pMainWnd->onPaint(hWnd,message,wParam,lParam);
  61. }
  62. break;
  63. case WM_DESTROY:
  64. PostQuitMessage(0);
  65. break;
  66. default:
  67. return DefWindowProc(hWnd, message, wParam, lParam);
  68.    }
  69.    return 0;
  70. }
  71. LRESULT CMainFrame::About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  72. {
  73. switch (message)
  74. {
  75. case WM_INITDIALOG:
  76. return TRUE;
  77. case WM_COMMAND:
  78. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  79. {
  80. EndDialog(hDlg, LOWORD(wParam));
  81. return TRUE;
  82. }
  83. break;
  84. }
  85.     return FALSE;
  86. }
  87. //功能:响应菜单消息
  88. void CMainFrame::OnCommand(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam)
  89. {
  90. int wmId    = LOWORD(wParam); 
  91. int wmEvent = HIWORD(wParam); 
  92. CManagerTool *pManagerTool = &(g_theApp.m_ManagerTool);
  93. CDataBase    *pDataBase    = &(g_theApp.m_DataBase);
  94.  
  95. switch (wmId)
  96. {
  97. case ID_DRAW_LINE:
  98. {
  99. pManagerTool->SetCurActiveTool(ID_DRAW_LINE);
  100. }
  101. break;
  102. case ID_DRAW_RECTANGLE:
  103. {
  104. pManagerTool->SetCurActiveTool(ID_DRAW_RECTANGLE);
  105. }
  106. break;
  107. case ID_DRAW_C2PT:
  108. {
  109. pManagerTool->SetCurActiveTool(ID_DRAW_C2PT);
  110. }
  111. break;
  112. case OVER_CUR_TOOL:
  113. {
  114. //取消鼠标绘图状态
  115. if(pManagerTool->m_pCurrentTool != NULL)
  116. delete pManagerTool->m_pCurrentTool;
  117. pManagerTool->m_pCurrentTool = NULL;
  118. }
  119. break;
  120. case ID_COLOR_RED:
  121. {
  122. pDataBase->m_Shape.crColor = RGB(255,0,0);
  123. }
  124. break;
  125. case ID_COLOR_GREEN:
  126. {
  127. pDataBase->m_Shape.crColor = RGB(0,255,0);
  128. }
  129. break;
  130. case ID_COLOR_BLUE:
  131. {
  132. pDataBase->m_Shape.crColor = RGB(0,0,255);
  133. }
  134. break;
  135. case ID_WIDTH_1:
  136. {
  137. pDataBase->m_Shape.nWidth = 1;
  138. }
  139. break;
  140. case ID_WIDTH_4:
  141. {
  142. pDataBase->m_Shape.nWidth = 4;
  143. }
  144. break;
  145. case ID_WIDTH_8:
  146. {
  147. pDataBase->m_Shape.nWidth = 8;
  148. }
  149. break;
  150. case ID_TYPE_SOLID:
  151. {
  152. pDataBase->m_Shape.fnPenStyle = PS_SOLID;
  153. }
  154. break;
  155. case ID_TYPE_DOT:
  156. {
  157. pDataBase->m_Shape.fnPenStyle = PS_DOT;
  158. }
  159. break;
  160. case ID_TYPE_DASH:
  161. {
  162. pDataBase->m_Shape.fnPenStyle = PS_DASH;
  163. }
  164. break;
  165. case ID_EDIT_COPY:
  166. {
  167. pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_COPY);
  168. }
  169. break;
  170. case ID_EDIT_PASTE:
  171. {
  172. pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_PASTE);
  173. }
  174. break;
  175. case ID_EDIT_MIRROR:
  176. {
  177. pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_MIRROR);
  178. }
  179. break;
  180. case ID_EDIT_DELETE:
  181. {
  182. pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_DELETE);
  183. }
  184. break;
  185. case ID_EDIT_DELETEALL:
  186. {
  187. pDataBase->m_ArrayShape.RemoveAll();
  188. InvalidateRect(hWnd,NULL,TRUE);
  189. }
  190. break;
  191. case ID_SAVE_FILE:
  192. {
  193. ofstream ofstr("2dCAD.txt");
  194. for(int i= 0;i<pDataBase->m_ArrayShape.GetArraySize();i++)
  195. {
  196. pDataBase->SaveObject(ofstr,pDataBase->m_ArrayShape[i]);
  197. }
  198. }
  199. break;
  200. case ID_OPEN_FILE:
  201. {
  202. ifstream ifstr("2dCAD.txt");
  203. while(!ifstr.eof())
  204. {
  205. CShape *pShape = pDataBase->OpenObject(ifstr);
  206. if (pShape != NULL)
  207. {
  208. pDataBase->m_ArrayShape.Add(pShape);
  209. }
  210. }
  211. InvalidateRect(hWnd,NULL,TRUE);
  212. }
  213. break;
  214. case IDM_ABOUT:
  215. {
  216. DialogBox(g_theApp.GetInst(), (LPCTSTR)IDD_ABOUTBOX, m_hMainWnd, (DLGPROC)About);
  217. }
  218. break;
  219. case IDM_EXIT:
  220. {
  221. DestroyWindow(hWnd);
  222. }
  223. break;
  224. default:
  225. ::DefWindowProc(m_hMainWnd, message, wParam, lParam);
  226. }
  227. }
  228. //功能:重绘图形
  229. void CMainFrame::onPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  230. {
  231. CDataBase *pDataBase = &(g_theApp.m_DataBase);
  232. PAINTSTRUCT ps;
  233. HDC hdc = BeginPaint(hWnd, &ps);
  234. for(int i = 0;i<pDataBase->m_ArrayShape.GetArraySize();i++)
  235. {
  236. if(pDataBase->m_ArrayShape[i] != NULL)
  237. {
  238. pDataBase->m_ArrayShape[i]->Draw(hdc);
  239. }
  240. }
  241. EndPaint(hWnd, &ps);
  242. }