www.pudn.com > my2cadtest07261930.rar > FileManger.cpp, change:2006-07-26,size:10162b


// FileManger.cpp: implementation of the CFileManger class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "FileManger.h" 
#include "list.h" 
#include "Shape.h" 
 
#include "Curve.h" 
#include "Ellipse.h" 
 
#include "Line.h" 
#include "Triangle.h" 
#include "Rectangle.h" 
#include "AnyPolygon.h" 
 
#include "Circle.h" 
 
#include <string.h> 
#include <stdio.h> 
 
extern CList<CShape *> m_list; 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CFileManger::CFileManger() 
{ 
 
} 
 
CFileManger::~CFileManger() 
{ 
 
} 
 
void CFileManger::PopFileInitialize(HINSTANCE hInstance, HWND hwnd) 
{ 
	m_ofn.lStructSize       = sizeof (OPENFILENAME) ; 
	m_ofn.hwndOwner         = hwnd ;	 
	m_ofn.hInstance         = hInstance ;    
	m_ofn.lpstrFilter       = "Enhance MateFile (*.EMF)\0*.EMF\0Text File (*.TXT)\0*.TXT\0" ;  
	m_ofn.lpstrCustomFilter = NULL ; 
	m_ofn.nMaxCustFilter    = 0 ; 
	m_ofn.nFilterIndex      = 0 ; 
	m_ofn.lpstrFile         = NULL ;           
	m_ofn.nMaxFile          = MAX_PATH ; 
	m_ofn.lpstrFileTitle    = NULL ;           
	m_ofn.nMaxFileTitle     = MAX_PATH ; 
	m_ofn.lpstrInitialDir   = NULL ; 
	m_ofn.lpstrTitle        = NULL ; 
	m_ofn.Flags             = OFN_CREATEPROMPT ;              
	m_ofn.nFileOffset       = 0 ; 
	m_ofn.nFileExtension    = 0 ; 
	m_ofn.lpstrDefExt       = "EMF";  
	m_ofn.lCustData         = 0L ; 
	m_ofn.lpfnHook          = NULL ; 
	m_ofn.lpTemplateName    = NULL ; 
 
} 
 
bool CFileManger::SaveToFile(HWND hWnd) 
{ 
	char strFileName[256]; 
	strcpy(strFileName,""); 
	m_ofn.lpstrFile = strFileName ; 
	m_ofn.lpstrTitle = "文件保存"; 
	bool result  = ::GetSaveFileName (&m_ofn) ; 
	if( result ) 
	{ 
		HDC hdcEMF,hdcRef; 
		hdcRef=GetDC(NULL); 
		RECT rect; 
		GetClientRect(hWnd, &rect); 
		this->Map10um(hdcRef, rect); 
		char* signature="starzhao2008@hotmail.com"; 
		hdcEMF=CreateEnhMetaFile(NULL,strFileName,&rect,signature); 
		if (!hdcEMF) 
		{ 
			MessageBox(hWnd,"文件保存失败!","温情提示!",NULL); 
			return 0; 
		} 
		SetBkColor(hdcEMF,penattrib->m_bgcolor); 
		for(int nI=0;nI<m_list.GetCount();nI++) 
		   { 
			if (0==nI) 
			{ 
			penattrib->SetPenStyle((m_list[0].node)->GetLOGPEN()); 
			penattrib->SetBrushStyle((m_list[0].node)->GetLOGBRUSH()); 
			penattrib->GetPen(hdcEMF,false); 
			} 
			m_list[nI].node->SavetoEmf(hdcEMF); 
             
		   } 
		penattrib->ReleasePen(false); 
        penattrib->ReleaseBrush(); 
		HENHMETAFILE hemf; 
		hemf = CloseEnhMetaFile(hdcEMF); 
		if (DeleteEnhMetaFile(hemf))  
		{ 
		    m_list.SetEditStatus(false); 
            m_list.SetSaveStatus(false); 
		//	MessageBox(hWnd,strFileName,"温情提示!",NULL); 
			MessageBox(hWnd,"恭喜! 文件保存成功!","温情提示!",NULL); 
			return 1; 
		} 
		else 
		{ 
			MessageBox(hWnd,"文件保存失败!","温情提示!",NULL); 
			return 0; 
		} 
	}	 
} 
 
void CFileManger::Map10um(HDC hdc, RECT &rect) 
{ 
	int widthmm    = GetDeviceCaps(hdc, HORZSIZE); 
	int heightmm   = GetDeviceCaps(hdc, VERTSIZE); 
	int widthpixel = GetDeviceCaps(hdc, HORZRES); 
	int heightpixel= GetDeviceCaps(hdc, VERTRES); 
 
	LPtoDP(hdc, (POINT *) & rect, 2); // map from logical to device 
 
	rect.left  =(rect.left  *widthmm *100+widthpixel/2)  / widthpixel; 
	rect.right =(rect.right *widthmm *100+widthpixel/2)  / widthpixel; 
	rect.top   =(rect.top   *heightmm*100+heightpixel/2) / heightpixel; 
	rect.bottom=(rect.bottom*heightmm*100+heightpixel/2) / heightpixel; 
 
} 
 
bool CFileManger::LoadFromFile(HWND hWnd) 
{ 
  if(m_list.GetEditStatus()) 
	{ 
		int result=MessageBox(hWnd,"当前文件未保存,是否保存?","新建文件",MB_YESNO); 
		if (result==IDYES) 
		{ 
			this->SaveToFile(hWnd); 
			m_list.DeleteAll(); 
			InvalidateRect(hWnd,NULL,true); 
			this->LoadFromFile(hWnd); 
			return true; 
		} 
		else 
		{ 
			m_list.DeleteAll(); 
			InvalidateRect(hWnd,NULL,true); 
		} 
 
	} 
	else 
	{ 
		m_list.DeleteAll(); 
		InvalidateRect(hWnd,NULL,true); 
 
	} 
 
	char strFileName[255]; 
	strcpy(strFileName,"");  // 让这个指针向一个空的内存区域,系统会把文件名填入其中 
	m_ofn.lpstrFile = strFileName ; 
	m_ofn.lpstrTitle = "打开文件"; 
	 
	bool result = ::GetOpenFileName (&m_ofn) ; 
	if( result ) 
	{ 
		HENHMETAFILE hemf ; 
		hemf = GetEnhMetaFile (strFileName) ; 
		::SetWindowText(hWnd,strFileName); 
		if (!hemf) 
		{ 
			MessageBox(hWnd,"文件打开失败!","温情提示!",NULL); 
			return 0; 
		} 
		RECT rect ; 
		ENHMETAHEADER header ; 
	    PTSTR pBuffer=NULL ; 
		int iLength; 
		iLength = GetEnhMetaFileDescription (hemf, 0, NULL) ; 
        pBuffer = new char[iLength] ; 
		GetEnhMetaFileDescription (hemf, iLength, pBuffer) ; 
		if (0!=strcmp(pBuffer,"starzhao2008@hotmail.com")) 
		{ 
			int result=MessageBox(hWnd,"此文件并非由--<<05040第4小组>>--所制作的CAD软件生成\n打开后可能会存在问题\n是否打开?","警告!",MB_YESNO); 
			if (result!=IDYES) 
			{ 
				delete[] pBuffer; 
				pBuffer=NULL; 
				return 0; 
			} 
 
		} 
		 
		if (pBuffer!=NULL)  
		{ 
			delete[] pBuffer; 
			pBuffer=NULL; 
		} 
		 
        GetEnhMetaFileHeader (hemf, sizeof(ENHMETAHEADER), &header) ; 
		UmToPixel(header,rect); 
		HDC hdc=GetDC(hWnd) ; 
//		char temp[256]; 
//		sprintf(temp,"%ld--%ld--%ld--%ld",rect.left,rect.top,rect.right,rect.bottom); 
//		MessageBox(hWnd,temp,"温情提示!",NULL); 
 
		EnumEnhMetaFile (hdc, hemf, EnhMetaFileProc, NULL, &rect) ; 
//		PlayEnhMetaFile (hdc, hemf, &rect) ; 
		DeleteEnhMetaFile (hemf) ; 
		ReleaseDC(hWnd,hdc); 
	} 
	 
return 1; 
} 
 
void CFileManger::UmToPixel(ENHMETAHEADER &header, RECT &rect) 
{ 
	double sx0 = header.rclFrame.left   / 100.0 / 
                 header.szlMillimeters.cx * header.szlDevice.cx; 
    double sy0 = header.rclFrame.top    / 100.0 / 
                 header.szlMillimeters.cy * header.szlDevice.cy; 
    double sx1 = header.rclFrame.right  / 100.0 / 
                 header.szlMillimeters.cx * header.szlDevice.cx; 
    double sy1 = header.rclFrame.bottom / 100.0 / 
                 header.szlMillimeters.cy * header.szlDevice.cy; 
 
	rect.left=sx0; 
	rect.top=sy0; 
	rect.right=sx1; 
	rect.bottom=sy1; 
 
} 
LOGPEN pen; 
LOGBRUSH brush; 
 
int CALLBACK CFileManger::EnhMetaFileProc (HDC hdc, HANDLETABLE * pHandleTable, 
                              CONST ENHMETARECORD * pEmfRecord,  
                              int iHandles, LPARAM pData) 
{ 
	 
	CShape *Shape = NULL; 
	ENHMETARECORD * pEmfr ; 
	pEmfr = (ENHMETARECORD *) malloc (pEmfRecord->nSize) ; 
	CopyMemory (pEmfr, pEmfRecord, pEmfRecord->nSize) ; 
	POINT tmpPt[1000]; 
	switch(pEmfr->iType) 
	{ 
	case EMR_HEADER: 
         
		/*头,开始读文件*/ 
		break; 
	/////////////////////////////////////////// 
	case EMR_SETBKCOLOR: 
		penattrib->m_bgcolor=((PEMRSETBKCOLOR)pEmfr)->crColor; 
//		::MessageBox(NULL,"EMR_SETBKCOLOR","dsfsadf",NULL); 
		break; 
	case EMR_CREATEPEN: 
 
		//MessageBox(NULL,"EMR_CREATEPEN","颜色",NULL); 
		pen.lopnColor=(((PEMRCREATEPEN)pEmfr)->lopn).lopnColor; 
		pen.lopnStyle=(((PEMRCREATEPEN)pEmfr)->lopn).lopnStyle; 
		pen.lopnWidth.x=(((PEMRCREATEPEN)pEmfr)->lopn).lopnWidth.x; 
		pen.lopnWidth.y=(((PEMRCREATEPEN)pEmfr)->lopn).lopnWidth.y; 
 
		break; 
	//////////////////////////////////////// 
	case EMR_ARC://圆弧		 
//		MessageBox(NULL,"EMR_ARC","图形",NULL); 
		break; 
	case EMR_PIE://扇形 
		break; 
	case EMR_MOVETOEX: 
		break; 
	case EMR_LINETO: 
		break; 
	case EMR_ELLIPSE://圆 
		{ 
			long temp=(((PEMRELLIPSE)pEmfr)->rclBox).left; 
			long temp2=(((PEMRELLIPSE)pEmfr)->rclBox).right; 
			long temp3=(((PEMRELLIPSE)pEmfr)->rclBox).top; 
			long temp4=(((PEMRELLIPSE)pEmfr)->rclBox).bottom; 
                   
			tmpPt[0].x=temp; 
            tmpPt[0].y=temp3; 
            tmpPt[1].x=temp2; 
            tmpPt[1].y=temp4; 
			Shape=new CCircle(tmpPt,2,pen,brush); 
			m_list.AddTail(Shape); 
			 
		//	MessageBox(NULL,"EMR_ELLIPSE","图形",NULL);			 
		break; 
		} 
		 
	case EMR_POLYGON: 
    case EMR_POLYGON16://连线图形 
		{ 
			POINTS* A=NULL; 
			A=((PEMRPOLYLINE16)pEmfr)->apts; 
			DWORD count=((PEMRPOLYLINE16)pEmfr)->cpts; 
			for (int i=0;i<count;i++,A++) 
				{ 
					tmpPt[i].x=A->x; 
					tmpPt[i].y=A->y; 
				} 
             
            switch(count) 
				{ 
				  case 2: 
					{ 
						Shape=new CLine(tmpPt,count,pen,brush); 
						break; 
					} 
				  case 3: 
					{ 
						Shape=new CTriangle(tmpPt,count,pen,brush); 
						break; 
					} 
				 case 4: 
					{ 
						Shape=new CRectangle(tmpPt,count,pen,brush); 
						break; 
					} 
				 default: 
					 { 
						Shape=new CAnyPolygon(tmpPt,count,pen,brush); 
						break; 
					 } 
				} 
			m_list.AddTail(Shape); 
		} 
		break; 
	case EMR_POLYLINE:	 
	case EMR_POLYLINE16://任意曲线 
		{ 
			POINTS* A=NULL; 
			A=((PEMRPOLYLINE16)pEmfr)->apts; 
			DWORD count=((PEMRPOLYLINE16)pEmfr)->cpts; 
			for (int i=0;i<count;i++,A++) 
				{ 
					tmpPt[i].x=A->x; 
					tmpPt[i].y=A->y; 
				} 
			Shape=new CCurve(tmpPt,count,pen,brush); 
			m_list.AddTail(Shape); 
		} 
		break; 
	case EMR_POLYBEZIER: 
	case EMR_POLYBEZIER16: //椭圆 
		{ 
			POINTS* A=NULL; 
			A=((PEMRPOLYLINE16)pEmfr)->apts; 
			DWORD count=((PEMRPOLYLINE16)pEmfr)->cpts; 
			for (int i=0;i<count;i++,A++) 
				{ 
					tmpPt[i].x=A->x; 
					tmpPt[i].y=A->y; 
				} 
			Shape=new CEllipse(tmpPt,count,pen,brush); 
			m_list.AddTail(Shape); 
		} 
		break; 
	case EMR_EOF: 
		/*尾, 图形读取完毕*/ 
		char temp[1024]; 
		sprintf(temp,"图形读取完毕!\n  图形总数: %d\n",m_list.GetCount()); 
		InvalidateRect(NULL,NULL,true); 
		MessageBox(NULL,temp,"图形读取",NULL);		 
		break; 
	default: 
	//	MessageBox(NULL,"default","图形",NULL); 
		break; 
	} 
	 
     return TRUE ; 
 
 
} 
 
bool CFileManger::NewFile(HWND hWnd) 
{ 
	if(m_list.GetEditStatus()) 
	{ 
		int result=MessageBox(hWnd,"当前文件未保存,是否保存?","新建文件",MB_YESNO); 
		if (result==IDYES) 
		{ 
			this->SaveToFile(hWnd); 
			m_list.DeleteAll(); 
			InvalidateRect(hWnd,NULL,true); 
			return true; 
		} 
		else 
		{ 
			m_list.DeleteAll(); 
			InvalidateRect(hWnd,NULL,true); 
		} 
 
	} 
	else 
	{ 
		m_list.DeleteAll(); 
		InvalidateRect(hWnd,NULL,true); 
 
	} 
 
}