www.pudn.com > IFSEditor.rar > IFSEditorDoc.cpp


// IFSEditorDoc.cpp : implementation of the CIFSEditorDoc class 
// 
 
#include "stdafx.h" 
#include "IFSEditor.h" 
#include "IFSSettingDlg.h" 
 
#include "IFSEditorDoc.h" 
#include "ProgressDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CIFSEditorDoc 
 
IMPLEMENT_DYNCREATE(CIFSEditorDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CIFSEditorDoc, CDocument) 
	//{{AFX_MSG_MAP(CIFSEditorDoc) 
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave) 
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs) 
	ON_COMMAND(ID_EDIT_IFS, OnEditIfs) 
	ON_COMMAND(ID_EDIT_RESTART, OnEditRestart) 
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy) 
	ON_COMMAND(ID_EDIT_EXPORT, OnEditExport) 
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy) 
	ON_UPDATE_COMMAND_UI(ID_EDIT_EXPORT, OnUpdateEditExport) 
	ON_UPDATE_COMMAND_UI(ID_EDIT_RESTART, OnUpdateEditRestart) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CIFSEditorDoc construction/destruction 
 
CIFSEditorDoc::CIFSEditorDoc() 
{ 
	// TODO: add one-time construction code here 
} 
 
CIFSEditorDoc::~CIFSEditorDoc() 
{ 
} 
 
BOOL CIFSEditorDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
	m_bValid = FALSE; 
	m_IFSArray.SetSize(0, 1); 
 
	// Should Ask User To Set Paramters!!! 
	CIFSSettingDlg dlg; 
	dlg.m_szTitle = GetTitle(); 
	dlg.m_bCanSetTitle = TRUE; 
	if(dlg.DoModal() == IDOK) 
	{ 
		// Get Parameters 
		SetTitle(dlg.m_szTitle); 
		m_clrBack = dlg.m_BackClr; 
		m_clrFore = dlg.m_ForeClr; 
		m_dwHeight = dlg.m_dwHeight; 
		m_dwWidth = dlg.m_dwWidth; 
		m_dwN = dlg.m_dwN; 
		m_dwUnitLenX = dlg.m_dwUnitLenX; 
		m_dwUnitLenY = dlg.m_dwUnitLenY; 
		m_ptStartPos.x = dlg.m_dwStartPosX; 
		m_ptStartPos.y = dlg.m_dwStartPosY; 
		m_IFSArray.RemoveAll(); 
		for(int i=0; i> m_clrBack; 
		ar >> m_clrFore; 
		ar >> m_dwHeight; 
		ar >> m_dwWidth; 
		ar >> m_dwN; 
		ar >> m_dwUnitLenX; 
		ar >> m_dwUnitLenY; 
		ar >> m_ptStartPos.x; 
		ar >> m_ptStartPos.y; 
		int dwArrayLen = 0; 
		ar >> dwArrayLen; 
		for(int i=0; i> strTemp.m_a; 
			ar >> strTemp.m_b; 
			ar >> strTemp.m_c; 
			ar >> strTemp.m_d; 
			ar >> strTemp.m_e; 
			ar >> strTemp.m_f; 
			ar >> strTemp.m_pi; 
			m_IFSArray.Add(strTemp); 
		} 
 
		// Create bitmap for display 
		m_bmpDisplay.DeleteObject(); 
		CDC ScreenDC, memdc; 
		CBitmap *pOldBmp; 
		CBrush BackBrush; 
		CRect rt(0, 0, m_dwWidth, m_dwHeight); 
		for(;ScreenDC.CreateDC("DISPLAY", NULL, NULL, NULL) == FALSE;); 
		memdc.CreateCompatibleDC(&ScreenDC); 
		m_bmpDisplay.CreateCompatibleBitmap(&ScreenDC, m_dwWidth, m_dwHeight); 
		BackBrush.CreateSolidBrush(m_clrBack); 
		pOldBmp = (CBitmap *)memdc.SelectObject(&m_bmpDisplay); 
		memdc.FillRect(rt, &BackBrush); 
		memdc.SelectObject(pOldBmp); 
		BackBrush.DeleteObject(); 
		memdc.DeleteDC(); 
		ScreenDC.DeleteDC(); 
 
		// Caculation... 
		CProgressDlg dlg; 
		dlg.m_pDoc = this; 
		if(dlg.DoModal() == IDOK) 
		{ 
			m_bmpDisplay.DeleteObject(); 
			m_bmpDisplay.Attach(dlg.m_bmpRender.Detach()); 
		} 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CIFSEditorDoc diagnostics 
 
#ifdef _DEBUG 
void CIFSEditorDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CIFSEditorDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CIFSEditorDoc commands 
 
int CIFSEditorDoc::GetWidth() 
{ 
	return m_dwWidth; 
} 
 
int CIFSEditorDoc::GetHeight() 
{ 
	return m_dwHeight; 
} 
 
void CIFSEditorDoc::DeleteContents()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	m_bValid = FALSE; 
	m_bmpDisplay.DeleteObject(); 
	m_IFSArray.RemoveAll(); 
	CDocument::DeleteContents(); 
} 
 
BOOL CIFSEditorDoc::GetValid() 
{ 
	return m_bValid; 
} 
 
void CIFSEditorDoc::OnUpdateFileSave(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(m_bValid); 
} 
 
void CIFSEditorDoc::OnUpdateFileSaveAs(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(m_bValid); 
} 
 
CBitmap * CIFSEditorDoc::GetBitmap() 
{ 
	return &m_bmpDisplay; 
} 
 
void CIFSEditorDoc::OnEditIfs()  
{ 
	// Should Ask User To Set Paramters!!! 
	CIFSSettingDlg dlg; 
	dlg.m_BackClr = m_clrBack; 
	dlg.m_ForeClr = m_clrFore; 
	dlg.m_dwHeight = m_dwHeight; 
	dlg.m_dwWidth = m_dwWidth; 
	dlg.m_dwN = m_dwN; 
	dlg.m_dwUnitLenX = m_dwUnitLenX; 
	dlg.m_dwUnitLenY = m_dwUnitLenY; 
	dlg.m_dwStartPosX = m_ptStartPos.x; 
	dlg.m_dwStartPosY = m_ptStartPos.y; 
	dlg.m_szTitle = GetTitle(); 
	for(int i=0; iGetSafeHwnd()); 
	::EmptyClipboard(); 
	HANDLE hNewBmp = ::CopyImage(m_bmpDisplay.GetSafeHandle(), IMAGE_BITMAP, m_dwWidth, m_dwHeight, 0); 
	::SetClipboardData(CF_BITMAP, hNewBmp); 
	::CloseClipboard(); 
	::DeleteObject(hNewBmp); 
} 
 
void CIFSEditorDoc::OnEditExport()  
{ 
	// Export image into a bitmap file 
	CString szFileName = GetTitle(); 
	for(int i=szFileName.GetLength()-1; i>=0; i--) 
	{ 
		if(szFileName[i] == '.') 
		{ 
			szFileName.Delete(i, szFileName.GetLength() - i); 
			break; 
		} 
	} 
	szFileName += ".bmp"; 
	CFileDialog dlg(FALSE,"*.bmp",szFileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"位图文档(*.bmp)|*.bmp|所有类型(*.*)||",AfxGetMainWnd()); 
	if(dlg.DoModal()==IDOK) 
	{ 
		HDC hScreenDC; 
		for(;(hScreenDC = ::CreateDC("DISPLAY", NULL, NULL, NULL)) == NULL;); 
		char str[500]; 
		sprintf(str,"%s",dlg.GetPathName()); 
		PBITMAPINFO pInfo=CreateBitmapInfoStruct(AfxGetMainWnd()->GetSafeHwnd(),(HBITMAP)m_bmpDisplay.GetSafeHandle()); 
		CreateBMPFile(AfxGetMainWnd()->GetSafeHwnd(),str,pInfo,(HBITMAP)m_bmpDisplay.GetSafeHandle(),hScreenDC); 
		LocalFree(pInfo); 
		::DeleteDC(hScreenDC); 
	} 
} 
 
void CIFSEditorDoc::OnUpdateEditCopy(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(m_bValid); 
} 
 
void CIFSEditorDoc::OnUpdateEditExport(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(m_bValid); 
} 
 
void CIFSEditorDoc::OnUpdateEditRestart(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(m_bValid); 
} 
 
 
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp) 
{  
    BITMAP bmp;  
    PBITMAPINFO pbmi;  
    WORD    cClrBits;  
 
    // Retrieve the bitmap's color format, width, and height.  
    GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp); 
 
    // Convert the color format to a count of bits.  
    cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);  
    if (cClrBits == 1)  
        cClrBits = 1;  
    else if (cClrBits <= 4)  
        cClrBits = 4;  
    else if (cClrBits <= 8)  
        cClrBits = 8;  
    else if (cClrBits <= 16)  
        cClrBits = 16;  
    else if (cClrBits <= 24)  
        cClrBits = 24;  
    else cClrBits = 32;  
 
    // Allocate memory for the BITMAPINFO structure. (This structure  
    // contains a BITMAPINFOHEADER structure and an array of RGBQUAD  
    // data structures.)  
 
     if (cClrBits != 24)  
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR,  
                    sizeof(BITMAPINFOHEADER) +  
                    sizeof(RGBQUAD) * (1<< cClrBits));  
 
     // There is no RGBQUAD array for the 24-bit-per-pixel format.  
 
     else  
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR,  
                    sizeof(BITMAPINFOHEADER));  
 
    // Initialize the fields in the BITMAPINFO structure.  
 
    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
    pbmi->bmiHeader.biWidth = bmp.bmWidth;  
    pbmi->bmiHeader.biHeight = bmp.bmHeight;  
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes;  
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;  
    if (cClrBits < 24)  
        pbmi->bmiHeader.biClrUsed = (1<bmiHeader.biCompression = BI_RGB;  
 
    // Compute the number of bytes in the array of color  
    // indices and store the result in biSizeImage.  
    // For Windows NT/2000, the width must be DWORD aligned unless  
    // the bitmap is RLE compressed. This example shows this.  
    // For Windows 95/98, the width must be WORD aligned unless the  
    // bitmap is RLE compressed. 
    pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8 
                                  * pbmi->bmiHeader.biHeight;  
    // Set biClrImportant to 0, indicating that all of the  
    // device colors are important.  
     pbmi->bmiHeader.biClrImportant = 0;  
     return pbmi;  
}  
 
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)  
{  
    HANDLE hf;                 // file handle  
    BITMAPFILEHEADER hdr;       // bitmap file-header  
    PBITMAPINFOHEADER pbih;     // bitmap info-header  
    LPBYTE lpBits;              // memory pointer  
    DWORD dwTotal;              // total count of bytes  
    DWORD cb;                   // incremental count of bytes  
    BYTE *hp;                   // byte pointer  
    DWORD dwTmp;  
 
    pbih = (PBITMAPINFOHEADER) pbi;  
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage); 
 
    // Retrieve the color table (RGBQUAD array) and the bits  
    // (array of palette indices) from the DIB.  
    GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,  
        DIB_RGB_COLORS); 
 
    // Create the .BMP file.  
    hf = CreateFile(pszFile,  
                   GENERIC_READ | GENERIC_WRITE,  
                   (DWORD) 0,  
                    NULL,  
                   CREATE_ALWAYS,  
                   FILE_ATTRIBUTE_NORMAL,  
                   (HANDLE) NULL);  
    if (hf == INVALID_HANDLE_VALUE)  
	{ 
		MessageBox(hwnd,"无法打开您指定的文件!","错误",MB_OK|MB_ICONWARNING); 
		return; 
	} 
    hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"  
    // Compute the size of the entire file.  
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +  
                 pbih->biSize + pbih->biClrUsed  
                 * sizeof(RGBQUAD) + pbih->biSizeImage);  
    hdr.bfReserved1 = 0;  
    hdr.bfReserved2 = 0;  
 
    // Compute the offset to the array of color indices.  
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +  
                    pbih->biSize + pbih->biClrUsed  
                    * sizeof (RGBQUAD);  
 
    // Copy the BITMAPFILEHEADER into the .BMP file.  
    WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),  
        (LPDWORD) &dwTmp,  NULL); 
 
    // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
    WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)  
                  + pbih->biClrUsed * sizeof (RGBQUAD),  
                  (LPDWORD) &dwTmp, NULL); 
 
    // Copy the array of color indices into the .BMP file.  
    dwTotal = cb = pbih->biSizeImage;  
    hp = lpBits;  
    WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL); 
 
    // Close the .BMP file.  
    CloseHandle(hf); 
 
    // Free memory.  
    GlobalFree((HGLOBAL)lpBits); 
}