⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagecamdoc.cpp

📁 《数字图象工程案例》原码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// imageCAMDoc.cpp : implementation of the CimageCAMDoc class
//

#include "stdafx.h"
#include "imageCAM.h"
#include "windowsx.h"
#include "imageCAMDoc.h"
#include "imageCAMView.h"
#include "dib.h"
#include "PreviewFileDlg.h" 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////added by ljh:start////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//说明:对不同的视图执行undo/redo功能的记数
int RunTimes=0;                                //记录不同视图的undo临时文件标记
extern	CString setszCurDir;
//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////added by ljh:end////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CimageCAMDoc

IMPLEMENT_DYNCREATE(CimageCAMDoc, CDocument)

BEGIN_MESSAGE_MAP(CimageCAMDoc, CDocument)
	//{{AFX_MSG_MAP(CimageCAMDoc)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CimageCAMDoc, CDocument)
	//{{AFX_DISPATCH_MAP(CimageCAMDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//      DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IimageCAM to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {57ECEFC5-C909-11D2-83AE-5254AB1C48FD}
static const IID IID_IimageCAM =
{ 0x57ecefc5, 0xc909, 0x11d2, { 0x83, 0xae, 0x52, 0x54, 0xab, 0x1c, 0x48, 0xfd } };

BEGIN_INTERFACE_MAP(CimageCAMDoc, CDocument)
	INTERFACE_PART(CimageCAMDoc, IID_IimageCAM, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CimageCAMDoc construction/destruction

CimageCAMDoc::CimageCAMDoc()
{
	// TODO: add one-time construction code here
	m_pDib	= new CDib;
	m_pDib->Create(DEFAULT_HEIGHT, DEFAULT_WIDTH,24);
	CPoint x(0,0);CPoint y(DEFAULT_HEIGHT, DEFAULT_WIDTH);
	CRect rc(x,y);
	m_pDib->CutRect(rc);
//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////added by ljh:start////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//说明:执行undo/redo功能
if(!SetCuurentDirectory())
{
    CString str="undo/redo功能无法实现!\n请不要使用这两项功能!!";
    AfxMessageBox(str);//,"warning");
}
  fileindex=0;        //记录某个undo的临时文件组索引fileindex,fileindex+1为下一步要记录的文件(isrecover)为false;
                      //或者是要恢复的文件;isrecover)为true时;
                               
  CreatTmpFile();
  RunTimes++;
  isundodone=FALSE;
 //tfilestr[0]=NULL;
//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////added by ljh:end////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
	EnableAutomation();

	AfxOleLockApp();
}

CimageCAMDoc::~CimageCAMDoc()
{
	AfxOleUnlockApp();
	if (m_pDib != NULL)
		delete m_pDib;
if(SetCuurentDirectory())
{
	for(int index=0;index<4;index++)
    {  
		if (tfilestr[index].Fundo!=NULL)
		{
		tfilestr[index].Fundo.Remove(tfilestr[index].FileName);
		}
	
	}

}
}

BOOL CimageCAMDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CimageCAMDoc serialization

void CimageCAMDoc::Serialize(CArchive& ar)
{
	m_pDib->Serialize(ar);
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CimageCAMDoc diagnostics

#ifdef _DEBUG
void CimageCAMDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CimageCAMDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CimageCAMDoc commands
void CimageCAMDoc::CreatTmpFile()                   //功能:创建临时文件的函数
                                                                             //参数:int i 为传递的文件的索引值fileindex;
{   
	for(int j=0;j<4;j++)
	{
	char p1[4];
    sprintf(p1,"%d%d",RunTimes,j);
	tfilestr[j].FileName="tempdraw";
	tfilestr[j].FileName+=p1;
	tfilestr[j].FileName+=".tmp";
    //tfilestr[j].Fundo.Open(tfilestr[j].FileName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary);
	CFileException e;
   // char* pFileName = "test.dat";
    if( !tfilestr[j].Fundo.Open(tfilestr[j].FileName,CFile::modeCreate|CFile::modeReadWrite, &e ) )
    {
      #ifdef _DEBUG
      afxDump << "File could not be opened " << e.m_cause << "\n";
	  AfxMessageBox("创建临时文件出错,请慎用图象菜单");
      #endif
	  return;
   }

	DWORD dwFileAttributes=::GetFileAttributes(tfilestr[j].FileName);
    dwFileAttributes|=FILE_ATTRIBUTE_HIDDEN;
	::SetFileAttributes(tfilestr[j].FileName,dwFileAttributes);
	tfilestr[j].Fundo.Close();
	tfilestr[j].isfilerecover=FALSE;

	}
}

void CimageCAMDoc::EditUndo() 
{
	// TODO: Add your command handle
//	if(!tfilestr[fileindex%4].isfilerecover)
//	{
	 WriteFile();
	 tfilestr[fileindex%4].isfilerecover=TRUE;
//	}
    fileindex=fileindex-1;//将文件索引后移一位:要读入文件dib值
	if(ReadFile())
	{
		m_pDib->UpdateInternal();
		SetModifiedFlag(TRUE);
	}
}




void CimageCAMDoc::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
		if((fileindex>=1)&&tfilestr[(fileindex-1)%4].isfilerecover==FALSE)
		pCmdUI->Enable(TRUE);
	 else
		pCmdUI->Enable(FALSE);
}
BOOL 	CimageCAMDoc::ReadTmpFile()
{	if(!SetCuurentDirectory())
    return FALSE;
if(ReadFile())
{
	tfilestr[fileindex%4].isfilerecover=TRUE;
    fileindex=fileindex-1;
	return TRUE;
}
else
return FALSE;

}
BOOL 	CimageCAMDoc::ReadFile() 
{
	WaitCursorBegin();
	CString tfilename=tfilestr[fileindex%4].FileName;
	CFile pFile=tfilestr[fileindex%4].Fundo;
	LPBITMAPINFOHEADER lpbi;
	DWORD dwSize;
	TRY
	{
		pFile.Open(tfilename,CFile::modeReadWrite);
	}
		CATCH (CException, e)
	{
		
		WaitCursorEnd();
		return FALSE;
	}
	END_CATCH
	BITMAPFILEHEADER bmfHdr;
	TRY
	{
		// read DIB file header
		
		pFile.Read(&bmfHdr, sizeof(BITMAPFILEHEADER));
		// is DIB file?
		if (bmfHdr.bfType != DIB_HEADER_MARKER)//"BM"
		{
			WaitCursorEnd();
			return FALSE;
		}
		DWORD dwLength = pFile.GetLength();
		if (bmfHdr.bfSize != dwLength) 
			bmfHdr.bfSize = dwLength;

		// read DIB buffer
		dwSize = bmfHdr.bfSize - sizeof(BITMAPFILEHEADER);
		lpbi = (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, dwSize);
		DWORD dwCount = pFile.ReadHuge(lpbi, dwSize);
		// read ok?
		if (dwCount != dwSize)
		{
			GlobalFreePtr(lpbi);
			WaitCursorEnd();
			return FALSE;
		}

		// Check to see that it's a Windows DIB -- an OS/2 DIB would cause 
		// strange problems with the rest of the DIB API since the fields 
		// in the header are different and the color table entries are 
		// smaller. 
		// 
		// If it's not a Windows DIB (e.g. if biSize is wrong), return NULL. 
	    if (lpbi->biSize != sizeof(BITMAPINFOHEADER)) 
		{
			GlobalFreePtr(lpbi);
			WaitCursorEnd();
			return FALSE;
		}
		
		// fill color num item
		int nNumColors = (UINT)lpbi->biClrUsed;
		if (nNumColors == 0) 
		{ 
			// no color table for 24-bit, default size otherwise 
	        if (lpbi->biBitCount != 24) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -