MainFrame.cpp
资源名称:CAD.zip [点击查看]
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:5k
源码类别:
CAD
开发平台:
Visual C++
- // MainFrame.cpp: implementation of the CMainFrame class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "MainFrame.h"
- #include "resource.h"
- #include "WinApp.h"
- #include "Shape.h"
- extern CWinApp g_theApp;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMainFrame::CMainFrame()
- {
- }
- CMainFrame::~CMainFrame()
- {
- }
- LRESULT CMainFrame::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- CMainFrame *pMainWnd = &(g_theApp.m_MainFrame);
- CManagerTool *pManagerTool = &(g_theApp.m_ManagerTool);
- switch (message)
- {
- case WM_COMMAND:
- {
- pMainWnd->OnCommand(hWnd,message,wParam,lParam);
- }
- break;
- case WM_LBUTTONUP:
- case WM_LBUTTONDOWN:
- case WM_RBUTTONDOWN:
- case WM_MOUSEMOVE:
- case WM_NCMOUSEMOVE:
- case WM_SETCURSOR:
- {
- //动态绘制
- if(pManagerTool != NULL && pManagerTool->m_pCurrentTool != NULL)
- {
- pManagerTool->DrawObject(hWnd,message,wParam,lParam);
- }
- //镜像
- else if(pManagerTool->MirrorFlag == TRUE)
- {
- pManagerTool->MirrorFlag = pManagerTool->MirrorObject(hWnd,message,wParam,lParam);
- if (pManagerTool->MirrorFlag == FALSE)
- InvalidateRect(hWnd,NULL,TRUE);
- }
- //编辑图形
- else
- {
- BOOL isModify = FALSE;
- isModify = pManagerTool->EditObject(hWnd,message,wParam,lParam);
- if (isModify == TRUE) InvalidateRect(hWnd,NULL,TRUE);
- }
- }
- break;
- case WM_PAINT:
- {
- pMainWnd->onPaint(hWnd,message,wParam,lParam);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- LRESULT CMainFrame::About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
- //功能:响应菜单消息
- void CMainFrame::OnCommand(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId = LOWORD(wParam);
- int wmEvent = HIWORD(wParam);
- CManagerTool *pManagerTool = &(g_theApp.m_ManagerTool);
- CDataBase *pDataBase = &(g_theApp.m_DataBase);
- switch (wmId)
- {
- case ID_DRAW_LINE:
- {
- pManagerTool->SetCurActiveTool(ID_DRAW_LINE);
- }
- break;
- case ID_DRAW_RECTANGLE:
- {
- pManagerTool->SetCurActiveTool(ID_DRAW_RECTANGLE);
- }
- break;
- case ID_DRAW_C2PT:
- {
- pManagerTool->SetCurActiveTool(ID_DRAW_C2PT);
- }
- break;
- case OVER_CUR_TOOL:
- {
- //取消鼠标绘图状态
- if(pManagerTool->m_pCurrentTool != NULL)
- delete pManagerTool->m_pCurrentTool;
- pManagerTool->m_pCurrentTool = NULL;
- }
- break;
- case ID_COLOR_RED:
- {
- pDataBase->m_Shape.crColor = RGB(255,0,0);
- }
- break;
- case ID_COLOR_GREEN:
- {
- pDataBase->m_Shape.crColor = RGB(0,255,0);
- }
- break;
- case ID_COLOR_BLUE:
- {
- pDataBase->m_Shape.crColor = RGB(0,0,255);
- }
- break;
- case ID_WIDTH_1:
- {
- pDataBase->m_Shape.nWidth = 1;
- }
- break;
- case ID_WIDTH_4:
- {
- pDataBase->m_Shape.nWidth = 4;
- }
- break;
- case ID_WIDTH_8:
- {
- pDataBase->m_Shape.nWidth = 8;
- }
- break;
- case ID_TYPE_SOLID:
- {
- pDataBase->m_Shape.fnPenStyle = PS_SOLID;
- }
- break;
- case ID_TYPE_DOT:
- {
- pDataBase->m_Shape.fnPenStyle = PS_DOT;
- }
- break;
- case ID_TYPE_DASH:
- {
- pDataBase->m_Shape.fnPenStyle = PS_DASH;
- }
- break;
- case ID_EDIT_COPY:
- {
- pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_COPY);
- }
- break;
- case ID_EDIT_PASTE:
- {
- pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_PASTE);
- }
- break;
- case ID_EDIT_MIRROR:
- {
- pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_MIRROR);
- }
- break;
- case ID_EDIT_DELETE:
- {
- pManagerTool->OnCurMenuAction(hWnd,ID_EDIT_DELETE);
- }
- break;
- case ID_EDIT_DELETEALL:
- {
- pDataBase->m_ArrayShape.RemoveAll();
- InvalidateRect(hWnd,NULL,TRUE);
- }
- break;
- case ID_SAVE_FILE:
- {
- ofstream ofstr("2dCAD.txt");
- for(int i= 0;i<pDataBase->m_ArrayShape.GetArraySize();i++)
- {
- pDataBase->SaveObject(ofstr,pDataBase->m_ArrayShape[i]);
- }
- }
- break;
- case ID_OPEN_FILE:
- {
- ifstream ifstr("2dCAD.txt");
- while(!ifstr.eof())
- {
- CShape *pShape = pDataBase->OpenObject(ifstr);
- if (pShape != NULL)
- {
- pDataBase->m_ArrayShape.Add(pShape);
- }
- }
- InvalidateRect(hWnd,NULL,TRUE);
- }
- break;
- case IDM_ABOUT:
- {
- DialogBox(g_theApp.GetInst(), (LPCTSTR)IDD_ABOUTBOX, m_hMainWnd, (DLGPROC)About);
- }
- break;
- case IDM_EXIT:
- {
- DestroyWindow(hWnd);
- }
- break;
- default:
- ::DefWindowProc(m_hMainWnd, message, wParam, lParam);
- }
- }
- //功能:重绘图形
- void CMainFrame::onPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- CDataBase *pDataBase = &(g_theApp.m_DataBase);
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- for(int i = 0;i<pDataBase->m_ArrayShape.GetArraySize();i++)
- {
- if(pDataBase->m_ArrayShape[i] != NULL)
- {
- pDataBase->m_ArrayShape[i]->Draw(hdc);
- }
- }
- EndPaint(hWnd, &ps);
- }