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

📄 serobjdoc.cpp

📁 几个老外写的源代码大家块块下载
💻 CPP
字号:
// SerObjDoc.cpp : implementation of the CSerObjDoc class
//

#include "stdafx.h"
#include "SerObjDoc.h"
#include "SerObj.h"
#include "CntrItem.h"

#include "ByteArrayFile.h"
#include "ReportRec.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSerObjDoc

IMPLEMENT_DYNCREATE(CSerObjDoc, CRichEditDoc)

BEGIN_MESSAGE_MAP(CSerObjDoc, CRichEditDoc)
	//{{AFX_MSG_MAP(CSerObjDoc)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	//}}AFX_MSG_MAP
	// Enable default OLE container implementation
	ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, CRichEditDoc::OnUpdateEditLinksMenu)
	ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
	ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerObjDoc construction/destruction

CSerObjDoc::CSerObjDoc()
{
	// TODO: add one-time construction code here
	theApp.m_pDoc = this;
}

CSerObjDoc::~CSerObjDoc()
{
}

CRichEditCntrItem* CSerObjDoc::CreateClientItem(REOBJECT* preo) const
{
	// cast away constness of this
	return new CSerObjCntrItem(preo, (CSerObjDoc*) this);
}



/////////////////////////////////////////////////////////////////////////////
// CSerObjDoc serialization

void CSerObjDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}

	// Calling the base class CRichEditDoc enables serialization
	//  of the container document's COleClientItem objects.
	// TODO: set CRichEditDoc::m_bRTF = FALSE if you are serializing as text
	CRichEditDoc::Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CSerObjDoc diagnostics

#ifdef _DEBUG
void CSerObjDoc::AssertValid() const
{
	CRichEditDoc::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CSerObjDoc commands

BOOL CSerObjDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CRichEditDoc::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	
	return TRUE;
}

void CSerObjDoc::OnFileSave() 
{
	BeginWaitCursor();
	
	//** Get the reportId to saved from the child frame title bar
	CString strReportId;
	((CMDIFrameWnd*) AfxGetMainWnd ())->MDIGetActive()->GetWindowText ( strReportId );

	//** Delete the record
	//** This way the document size can change and the record set won't crash
	CString strSQL;
	strSQL.Format("DELETE FROM REPORTS WHERE ReportId = '%s'", strReportId );
	theApp.m_DB.ExecuteSQL ( strSQL );

	//** Serialize document and store in ByteArray
	//** update the size of the document in the record set
	CByteArray ba;
	long nDocSize = TransDataWithByteArray( ba, TRUE );

	//** open record set, this record won't exist, but the record set will still open
	CReportRec	rec( nDocSize, &theApp.m_DB);
	strSQL.Format ("SELECT ReportId, DocumentContents, DocSize FROM Reports WHERE ReportId = '%s'", strReportId );

	try {

		//** Open record set, add a blank record
		rec.Open(AFX_DB_USE_DEFAULT_TYPE, strSQL );

		//** if the project does not exist the add a new record
		if(rec.IsBOF () ) 
			rec.AddNew();
		
		//** , if the record is found, set all fields dirty
		else {	
			rec.SetFieldDirty(NULL);
			rec.Edit();
		}

		rec.m_ReportId = strReportId;
		rec.m_DocumentContents.Copy( ba );
		rec.m_DocSize = nDocSize;
	
		rec.Update();

	}
	catch(CException* pE) {
        pE->ReportError();
        pE->Delete();
        return;
    }

	rec.Close();


	EndWaitCursor(); 
}


BOOL CSerObjDoc::SaveModified() 
{
	//return CRichEditDoc::SaveModified();
	
	//** Prevent document from opening the Save file dialog
	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////////////////////
long CSerObjDoc::TransDataWithByteArray(CByteArray& byteArray, BOOL bStore)
{

	CByteArrayFile byFile(2000000);	//** create an initial 2 Meg buffer
									//** this can be any size you like, but to store large files to the database
									//** a large buffer will greatly reduce the write time.

	byFile.SetArray(&byteArray);    // attach byteArray with byFile

	if (bStore) {	// store object into byteArray
		CArchive ar( &byFile, CArchive::store); // create CArchive object with CByteArray File
		Serialize(ar);    // Call CSomeClass::Serialize function
		ar.Close();
	}

	else {	// load object from byteArray
		CArchive ar( &byFile, CArchive::load);
		Serialize(ar);    // Call CSomeClass::Serialize function
		ar.Close();
	}

	//** Save the file length
	long len = byFile.GetLength ();

	// important!!, detach byteArray with byFile. Or byteArray will be free with byFile
	byFile.Detach();

	return len;
}


long CSerObjDoc::GetDocumentSize(CString strReportId)
{

	CRecordset recDocSize(&theApp.m_DB);
	CString strDocSize;
    CString strSQL;
	strSQL.Format("SELECT DocSize FROM Reports WHERE ReportId = '%s'", strReportId );
	try {
		recDocSize.Open( CRecordset::forwardOnly, strSQL, CRecordset::readOnly );
		if(!recDocSize.IsBOF()) {
			recDocSize.GetFieldValue( "DocSize", strDocSize );
		}
	}
	catch(CException* pE) {
        pE->ReportError();
        pE->Delete();
        return 0;
    }

    recDocSize.Close();
	return atol(strDocSize);
}

⌨️ 快捷键说明

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