www.pudn.com > my2cadtest07261930.rar > 2cad.cpp, change:2006-07-26,size:15096b


 
#include "stdafx.h" 
#include "resource.h" 
#include <windows.h> 
#include <stdio.h> 
 
 
#include "LineTool.h" 
#include "ParaTool.h" 
#include "RectangleTool.h" 
#include "AnyPolygonTool.h" 
#include "TriangleTool.h" 
#include "PolygonTool.h" 
#include "circleTool.h" 
#include "EllipseTool.h" 
#include "CurveTool.h" 
 
#include "CatchTool.h" 
#include "MoveTool.h" 
#include "RotateTool.h" 
#include "MirrorTool.h" 
#include "ZoomTool.h" 
#include "CopyTool.h" 
#include "DelTool.h" 
#include "AttribTool.h" 
 
 
#include "Line.h" 
#include "shape.h" 
#include "list.h" 
#include "FileManger.h" 
 
 
#include "SetPenAttr.h" 
#include "SetPolyNum.h" 
 
#include "StatusBar.h" 
 
#define MAX_LOADSTRING 100 
 
CSetPenAttr  *penattrib=new CSetPenAttr ; 
HWND g_hwnd; 
 
CStatusBar statusbar; 
 
CList<CShape *> m_list;   //(图形链表类 临时用) 
 
// Global Variables: 
 
HINSTANCE hInst;								// current instance 
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text 
 
// Foward declarations of functions included in this code module: 
ATOM				MyRegisterClass(HINSTANCE hInstance); 
BOOL				InitInstance(HINSTANCE, int); 
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM); 
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM); 
LRESULT CALLBACK	ATTRIBBOX(HWND, UINT, WPARAM, LPARAM); 
 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
                     LPSTR     lpCmdLine, 
                     int       nCmdShow) 
{ 
 	// TODO: Place code here. 
	MSG msg; 
	HACCEL hAccelTable; 
	 
	 
	// Initialize global strings 
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
	LoadString(hInstance, IDC_MY2CAD, szWindowClass, MAX_LOADSTRING); 
	MyRegisterClass(hInstance); 
 
	// Perform application initialization: 
	if (!InitInstance (hInstance, nCmdShow))  
	{ 
		return FALSE; 
	} 
 
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY2CAD); 
 
	// Main message loop: 
	while (GetMessage(&msg, NULL, 0, 0))  
	{ 
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))  
		{ 
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		} 
	} 
 
	return msg.wParam; 
} 
 
 
 
// 
//  FUNCTION: MyRegisterClass() 
// 
//  PURPOSE: Registers the window class. 
// 
//  COMMENTS: 
// 
//    This function and its usage is only necessary if you want this code 
//    to be compatible with Win32 systems prior to the 'RegisterClassEx' 
//    function that was added to Windows 95. It is important to call this function 
//    so that the application will get 'well formed' small icons associated 
//    with it. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
	WNDCLASSEX wcex; 
 
	wcex.cbSize = sizeof(WNDCLASSEX);  
 
	wcex.style			= CS_HREDRAW | CS_VREDRAW; 
	wcex.lpfnWndProc	= (WNDPROC)WndProc; 
	wcex.cbClsExtra		= 0; 
	wcex.cbWndExtra		= 0; 
	wcex.hInstance		= hInstance; 
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_MY2CAD); 
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW); 
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1); 
	wcex.lpszMenuName	= (LPCSTR)IDR_MENU; 
	wcex.lpszClassName	= szWindowClass; 
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); 
 
	return RegisterClassEx(&wcex); 
} 
 
// 
//   FUNCTION: InitInstance(HANDLE, int) 
// 
//   PURPOSE: Saves instance handle and creates main window 
// 
//   COMMENTS: 
// 
//        In this function, we save the instance handle in a global variable and 
//        create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
   HWND hWnd; 
 
   hInst = hInstance; // Store instance handle in our global variable 
 
//   hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP, 
//      0, 0, GetSystemMetrics( SM_CXSCREEN ), GetSystemMetrics( SM_CYSCREEN ), NULL, NULL, hInstance, NULL); 
 
 
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 
 
   statusbar.CreateSB(hWnd);  
 
   if (!hWnd) 
   { 
      return FALSE; 
   } 
 
   ShowWindow(hWnd, nCmdShow); 
   UpdateWindow(hWnd); 
 
   return TRUE; 
} 
 
// 
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG) 
// 
//  PURPOSE:  Processes messages for the main window. 
// 
//  WM_COMMAND	- process the application menu 
//  WM_PAINT	- Paint the main window 
//  WM_DESTROY	- post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
	g_hwnd=hWnd; 
	int wmId, wmEvent; 
	PAINTSTRUCT ps; 
	HDC hdc; 
//	TCHAR szHello[MAX_LOADSTRING]; 
//	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); 
  
   //实例化工具类	 
   static CLineTool lineTool(hWnd); 
   static CParaTool parallelTool(hWnd,false);  //平行线 
   static CParaTool paraTool(hWnd);            //平行四边形 
 
   static CRectangleTool  rectangleTool(hWnd);   //矩形 
   static CAnyPolygonTool anypolygonTool(hWnd); //任意多边形 
   static CPolygonTool    polygonTool(hWnd);   //正多边形 
   static CCircleTool    circleTool(hWnd); 
   static CEllipseTool ellipseTool(hWnd); 
   static CCurveTool curveTool(hWnd);   
   static CTriangleTool triangleTool(hWnd); 
 
   static CCatchTool catchTool(hWnd); 
   static CMoveTool  moveTool(hWnd); 
   static CRotateTool rotateTool(hWnd); 
   static CMirrorTool mirrorTool(hWnd); 
   static CZoomTool   zoomTool(hWnd); 
   static CCopyTool   copyTool(hWnd); 
   static CDelTool    delTool(hWnd); 
   static CAttribTool attribTool(hWnd); 
     
   static CFileManger filemanger; 
 
   static CTool *pTool=&lineTool; 
 
   POINT curpt; 
//   static bool selected=false; 
	switch (message)  
	{ 
	   case WM_CREATE: 
//		::InitCommonControls(); 
		filemanger.PopFileInitialize(hInst,hWnd); 
		break; 
	   case WM_COMMAND: 
			wmId    = LOWORD(wParam);  
			wmEvent = HIWORD(wParam);  
			// Parse the menu selections: 
			switch (wmId) 
			{ 
		     	case IDM_FILE_NEW:	 
					filemanger.NewFile(hWnd); 
                    break; 
			    case IDM_FILE_SAVE:					 
				   filemanger.SaveToFile(hWnd);					                 			 
				   break; 
                case IDM_FILE_OPEN: 
//				   m_list.DeleteAll();       //打开图形文件前先将链表清空 
				   filemanger.LoadFromFile(hWnd); 
				   break; 
                case IDM_ATTRIBUTE: 
                    DialogBox(hInst, (LPCTSTR)IDD_ATTRIBBOX, hWnd, (DLGPROC)ATTRIBBOX); 
					break; 
				case IDM_ABOUT: 
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); 
				   break; 
				case IDM_EXIT: 
					{ 
        //            char buff[256]=""; 
		//			 sprintf(buff,"%d-----%d",m_list.GetEditStatus(),m_list.GetSaveStatus()); 
		//		    MessageBox(NULL,buff,NULL,0); 
					if(m_list.GetEditStatus()) 
					{ 
						int result=MessageBox(hWnd,"当前文件已修改但未保存,是否保存?","保存",MB_YESNO); 
						if (result==IDYES) 
						{ 
							filemanger.SaveToFile(hWnd); 
							m_list.DeleteAll(); 
							InvalidateRect(hWnd,NULL,true); 
							DestroyWindow(hWnd); 
							return true; 
						} 
						else 
						{ 
							m_list.DeleteAll(); 
							InvalidateRect(hWnd,NULL,true); 
							DestroyWindow(hWnd); 
							return false; 
						} 
 
					} 
 
                  DestroyWindow(hWnd); 
					} 
				   break; 
				case IDM_DRAW_LINE: 
					{ 
                      pTool=&lineTool; 
                     break; 
					} 
                case IDM_DRAW_PARALLEL:  //平行线 
					{ 
						pTool=¶llelTool; 
						break; 
					} 
				case  IDM_DRAW_TRIANGLE: 
					{ 
                        pTool=&triangleTool; 
						break; 
						 
					} 
                case IDM_DRAW_PARA:      //平行四边形 
					{ 
                      pTool=¶Tool; 
                      break;    
					} 
                 case IDM_DRAW_RECT:      //矩形 
					{ 
                      pTool=&rectangleTool; 
                      break;    
					} 
                  
                 case IDM_DRAW_ANYPOLYGON: 
					{ 
                      pTool=&anypolygonTool; 
                      break;    
					} 
                 case IDM_DRAW_POLYGON:     //多边形 
					{ 
                      //弹出对话筐 
					   	CSetPolyNum setPolyNum(hWnd); 										 
						pTool=&polygonTool; 
						pTool->SetNumPoly(setPolyNum.GetPolyNum()); 
					   
                      break;    
					} 
				 case IDM_DRAW_CIRCLE: 
					 { 
                       pTool=&circleTool; 
					   break; 
					 } 
                  case IDM_DRAW_ELLIPSE: 
					 { 
						pTool=&ellipseTool; 
						break;  
					 } 
				 case IDM_DRAW_CURVE: 
					 { 
						pTool=&curveTool; 
						break;  
					 }	  
 
                case IDM_OPER_CATCH: 
					{ 
                     pTool=&catchTool; 
                     break; 
					} 
				case IDM_OPER_MOVE: 
					{ 
                     pTool=&moveTool; 
                     break; 
					} 
				case IDM_OPER_ROTATE: 
					{ 
						pTool=&rotateTool; 
						break; 
					} 
				case IDM_OPER_MIRROR: 
					{ 
						pTool=&mirrorTool; 
						break; 
					} 
                case IDM_OPER_ZOOM: 
					{ 
						pTool=&zoomTool; 
						break; 
					} 
				case IDM_OPER_COPY: 
					{ 
						pTool=©Tool; 
						 
						break; 
					} 
                case IDM_OPER_DEL: 
					{ 
                       pTool=&delTool;       
                       break; 
					} 
                case IDM_OPER_ATTRIB: 
					{ 
                       pTool=&attribTool; 
                       break; 
					} 
                case IDM_PENSET: 
					{ 
                      penattrib->PenAttribDlg(hInst,hWnd); 
                      break;  // 
					} 
				default: 
				   return DefWindowProc(hWnd, message, wParam, lParam); 
			} 
			break; 
		case WM_LBUTTONDOWN: 
            pTool->OnLButtonDown(wParam,lParam); 
            break; 
        case WM_LBUTTONUP: 
            pTool->OnLButtonUp(wParam,lParam); 
            break; 
        case WM_RBUTTONDOWN: 
		{             
             if(pTool->GetStatus()==0) 
             {    
				bool selected=false;  
				curpt.x=LOWORD(lParam); 
				curpt.y=HIWORD(lParam); 
				ClientToScreen(hWnd,&curpt); 
				for(int i=0;i<m_list.GetCount();i++) 
				{ 
					 if(m_list[i].node->IsLock()) 
					 { 
					   selected=true; 
					   break; 
                     } 
					 
				}	 
				if(selected)  
				{							               			   
				  HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_RBMENU1)); 
				  HMENU hSubMenu = GetSubMenu(hMenu,0); 
				  TrackPopupMenu(hSubMenu,TPM_LEFTALIGN,curpt.x,curpt.y,0,hWnd,NULL); 
				  break; 
				 } 
				 else 
				{				 
					HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_RBMENU2)); 
					HMENU hSubMenu = GetSubMenu(hMenu,0); 
					TrackPopupMenu(hSubMenu,TPM_LEFTALIGN,curpt.x,curpt.y,0,hWnd,NULL); 
				 }								 
			} 
 
         pTool->OnRButtonDown(wParam,lParam); 
		} 
            break; 
        case WM_MOUSEMOVE: 
            statusbar.OnMouseMove(lParam); 
			 
            pTool->OnMouseMove(wParam,lParam); 
			break; 
		case WM_PAINT: 
			{ 
              hdc = BeginPaint(hWnd, &ps);			   
              RECT rt; 
			  GetClientRect(hWnd, &rt); 
			  HBRUSH hbursh=CreateSolidBrush(penattrib->m_bgcolor); 
			  FillRect(hdc,&rt,hbursh); 
			   
				for(int i=0;i<m_list.GetCount();i++) 
				{ 
					penattrib->SetPenStyle(m_list[i].node->GetLOGPEN()); 
                    penattrib->GetPen(hdc,false); 
					m_list[i].node->DrawSelf(hdc);   //重绘几何图形 
					penattrib->ReleasePen(false); 
				} 
 
                EndPaint(hWnd, &ps); 
				break; 
			} 
        case WM_SIZE: 
			{ 
             statusbar.SetVisible(hWnd); 
	    	  statusbar.SetVisible(hWnd); 
//			 ::InvalidateRect(hWnd, NULL, true); 
 
				 
			} 
			break; 
		case WM_DESTROY: 
			PostQuitMessage(0); 
			break; 
		default: 
			return DefWindowProc(hWnd, message, wParam, lParam); 
   } 
   return 0;                         
}    
 
// Message handler for about box. 
LRESULT CALLBACK 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; 
} 
 
// Message handler for attrib box. 
LRESULT CALLBACK ATTRIBBOX(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
   char buff[10]=""; 
   HWND hNumBox[10]; 
   HWND hBgBox; 
   int ntotalall=0; 
 
   HWND hOnOk; 
   hOnOk=::GetDlgItem(hDlg,IDOK); 
   ::SetFocus(hOnOk); 
 
   hBgBox  = ::GetDlgItem(hDlg, IDC_BGCOLOR);              
 
   hNumBox[0] = ::GetDlgItem(hDlg, IDC_LINE); 
   hNumBox[1] = ::GetDlgItem(hDlg, IDC_TRIANGLE); 
   hNumBox[2] = ::GetDlgItem(hDlg, IDC_PARA); 
   hNumBox[3] = ::GetDlgItem(hDlg, IDC_RECTANGLE); 
   hNumBox[4] = ::GetDlgItem(hDlg, IDC_ANYPOLYGON); 
   hNumBox[5] = ::GetDlgItem(hDlg, IDC_POLYGON); 
   hNumBox[6] = ::GetDlgItem(hDlg, IDC_CIRCLE); 
   hNumBox[7] = ::GetDlgItem(hDlg, IDC_ELLIPSE); 
   hNumBox[8] = ::GetDlgItem(hDlg, IDC_CURVE); 
 
   hNumBox[9] = ::GetDlgItem(hDlg, IDC_TOTAL); 
    
	switch (message) 
	{ 
		case WM_INITDIALOG: 
		{ 
			int ntotal[9]={0,0,0,0,0,0,0,0,0};			   			    
			 for(int i=0;i<m_list.GetCount();i++) 
			 { 
                switch(m_list[i].node->m_whatshape)  
				{ 
                case 0:   //line 
                    ntotal[0]++; 
                	break; 
                case 1:   //rect 
                    ntotal[3]++; 
                    break; 
				case 2:   //parall 
					ntotal[2]++;                                     
                	break; 
                case 3:   //para 
                     ntotal[0]=ntotal[0]+2;  
                    break; 
				case 4:   //triangle 
					ntotal[1]++; 
                	break; 
                case 5:   //poly 
					ntotal[5]++; 
                    break; 
				case 6:   //circle 
					ntotal[6]++; 
                	break; 
                case 7:  //anypoly 
					ntotal[4]++; 
                    break; 
				case 8:  //ellipse 
					ntotal[7]++; 
                	break; 
                case 9:  //curve 
					ntotal[8]++; 
                    break; 
                default: 
					break; 
                } 
			 } 
         	for(int j=0;j<9;j++) 
				{ 
				    ntotalall=ntotalall+ntotal[j]; 
					sprintf(buff,"%d",ntotal[j]); 
					::SetWindowText(hNumBox[j],buff); 
				} 
            sprintf(buff,"%d",ntotalall); 
			::SetWindowText(hNumBox[9],buff);       
		  return TRUE; 
         } 
		case WM_CTLCOLORSTATIC: 
			{ 
               if((HWND)lParam == hBgBox) 
				   {				   
				    HBRUSH Brush = ::CreateSolidBrush(penattrib->m_bgcolor); 		 
					HDC hdc = (HDC)wParam;			 
					::SelectObject(hdc, Brush); 
					::Rectangle(hdc, 0, 0, 100, 100); 
					::DeleteObject(Brush); 
					::ReleaseDC(hBgBox,hdc); 
				   } 
			} 
			break; 
		case WM_COMMAND: 
			if (LOWORD(wParam) == IDOK )  
			{ 
				EndDialog(hDlg, LOWORD(wParam)); 
				return TRUE; 
			} 
			break; 
        case WM_CLOSE: 
			::EndDialog(hDlg, LOWORD(wParam)); 
			return TRUE; 
		break; 
	} 
    return FALSE; 
}