Click here to Skip to main content
15,896,278 members
Articles / Desktop Programming / MFC

Develop MFC Doc/View Application Which Supports Any Number Document Template

Rate me:
Please Sign up or sign in to vote.
1.57/5 (3 votes)
2 Jun 20052 min read 36.5K   1.7K   26  
This article provides an introduction to TangramLittle, a C++ Framework for MFC and the .NET Framework. Knowledge in MFC and the .NET Framework is assumed.
// DocTemplate3Doc.cpp : implementation of the CDocTemplate3Doc class
//

#include "stdafx.h"
#include "DocTemplate3.h"
#include "DocTemplate3Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CDocTemplate3Doc

IMPLEMENT_DYNCREATE(CDocTemplate3Doc, COleDocument)

BEGIN_MESSAGE_MAP(CDocTemplate3Doc, COleDocument)
END_MESSAGE_MAP()


// CDocTemplate3Doc construction/destruction

CDocTemplate3Doc::CDocTemplate3Doc()
{
	theApp.m_pCurDoc = this;
	EnableCompoundFile();
	/********************************************************************************
	����.NET�����IDֵ��.NET�����IDֵ�ĸ�ʽΪ��xxxxx#xxxxx.yyyy������xxxxx��ʾ.NET��
	������ij��򼯣�xxxxx.yyyy��ʾ�����е�һ������ͨ���ṩһ��������.NET����IDֵ��
	���ǿ���ʵ�ָö���Ķ�̬��������ˣ���������Ҫһ������ʱ��ͨ���ṩ�ö����IDֵ����
	��ʵ�ְ��贴����ע�⣬��һ���������ϵͳ�У�������Ҫ����ָ��������������󣬶�.NET
	���ԣ���һ�����Ҫ�������������滻ԭʼ�����ʵ��Ӧ�ó���Ķ�̬�ԡ���һ���ĵ�ģ���
	�ԣ��������ļ����£�
		<?xml version="1.0" encoding="utf-8" ?> 
		<Tangram 
			DocViewID="DocTemplate3#DocTemplate3.DocTemplate"
			DocObjID = ""
			ExtDocObjID = "">
		</Tangram>
	�������ϣ���ı��ĵ��������ǿ��Դ�����.NET�����������ĵ��Ķ�ż.NET����������
	�Ķ�������ԭ�����ĵ�����Ϊ���࣬��ʱ��Ҫ���¶����IDֵ����DocObjID����������ϵͳ
	�������������������ĵ�ʱ���ᴴ����������ĵ�������ΪMFC�ĵ��Ķ�ż.NET����������
	���ĵ������µ�������ܱ仯������xml�ļ���ExtDocObjID����ָ����չ�ĵ�����һ��ָ��
	��չ�ĵ�����ID�����ο������ƾͽ����ˡ�
	ͨ�����������ĵ�ģ���xml�ļ������ǵõ������ַ�����
				g_pDotNetExtImpl->m_strCurDocTemplateID
				g_pDotNetExtImpl->m_strCurDocObjID
				g_pDotNetExtImpl->m_strCurExtDocObjID
	�������ַ����ֱ𱻱�����m_DocumentInfo.m_strDocTemplateID��m_strDocObjID��m_strExternalDocObjID
	�У����ĵ�������ʱ������m_DocumentInfo�Լ�xml�е�������Ϣ�����洢��MFC�ĵ���һ��
	�Ӵ洢�У��Ա���ĵ�ʱ�ܹ���ȷ�õ��ĵ���������Ϣ������Ϊ���ԭ��TangramLittle
	Ӧ�õ�MFC�ĵ������COleDocument������
	********************************************************************************/
	//�õ��ĵ������IDֵ��
	m_DocumentInfo.m_strDocTemplateID = g_pDotNetExtImpl->m_strCurDocTemplateID;
	if(g_pDotNetExtImpl->m_strCurDocObjID!=_T(""))
		m_strDocObjID = g_pDotNetExtImpl->m_strCurDocObjID;
	else
		m_strDocObjID = _T("DocTemplate3#DocTemplate3.DocTemplate3DocObject");

	//�õ���չ�ĵ������IDֵ��
	if(g_pDotNetExtImpl->m_strCurExtDocObjID!=_T(""))
		m_strExternalDocObjID = g_pDotNetExtImpl->m_strCurExtDocObjID;
	else
		m_strExternalDocObjID = _T("");
	m_pExternalDocObj = NULL;
	m_pDocTemplate3DocObj = NULL;
}

CDocTemplate3Doc::~CDocTemplate3Doc()
{
	CString strFileName = GetPathName();
	g_pDotNetExtImpl->m_OpenedDocCnnDictionary.RemoveKey(LPCTSTR(strFileName));
	POSITION pos;
	CString key;
	CObject* pManagedObj;

	// Iterate through the entire map.
	for( pos = m_PrjItemDictionary.GetStartPosition(); pos != NULL; )
	{
		m_PrjItemDictionary.GetNextAssoc( pos, key, (CObject*&)pManagedObj );
		delete pManagedObj;
	}
	m_PrjItemDictionary.RemoveAll();
	delete m_pDocTemplate3DocObj;
}

//Create Document Object and External Document Object:
void CDocTemplate3Doc::InitialDocument()
{
	try
	{
		/***************************************************************************
		InitialDocument���ڴ���MFC�ĵ��Ķ�ż.NET�����Լ���չ�ĵ�����ͬʱҲ��������
		����������չ�ĵ�����֮��Ĺ�ͨ;��������ϣ����չ�ĵ�������һ�����ԣ��������
		�����ĵ�����ͨ�������ԣ���չ�ĵ�������Է����ĵ����󣬷�����������Ϊ��չ��
		������ָ��һ���������࣬��ʵ�ʴ��ݵ��Ǹû������������IDֵ����ˣ�Ӧ�ó���
		�γ����ġ���������ƣ���ÿ����˲�������չ�ĵ�ģ��������Ϊһ�������
		TangramLittleϵͳ�������ط�����InitialDocument����һ���ط��Ǵ������ĵ�ʱ����
		���λ����CDocTemplate3Doc::OnNewDocument()����һ���ط������ڴ���
		���ڵ��ĵ�ʱ�������λ����CDocTemplate3Doc::Serialize(CArchive& ar)��
		���ǿ�����InitialDocument�ṩ���������ο�����;����һ��������MFC�ĵ���ż����
		��������һ���ṩ��չ�ĵ�����
		***************************************************************************/
		////////////////////////Begin Create Document Object////////////////////////
		/***************************************************************************
		�����ϣ���ĵ�����߱��������ƣ������´����ĵ�����
			#pragma push_macro("new")
			#undef new
				m_pDocTemplate3DocObj = new DocTemplate3::DocTemplate3DocObject();
			#pragma pop_macro("new")
		***************************************************************************/
		CString m_strAssembly,m_strObjID;
		int nPos = -1;
		nPos = m_strDocObjID.Find(_T("#"));
		m_strAssembly = m_strDocObjID.Left(nPos);
		m_strObjID = m_strDocObjID.Mid(nPos+1);
		String* strAssembly = m_strAssembly;
		String* strObjID = m_strObjID;
		Assembly* m_pDotNetAssembly = Assembly::Load(strAssembly);
		Type* m_pDotNetDocType = m_pDotNetAssembly->GetType(strObjID,true,true);
		DocTemplate3::DocTemplate3DocObject*	m_pDotNetDocObj = NULL;
		Object*	m_pExternalDocObj = NULL;
		m_pDotNetDocObj = __try_cast< DocTemplate3::DocTemplate3DocObject* >(Activator::CreateInstance(m_pDotNetDocType)); 
		m_pDocTemplate3DocObj = m_pDotNetDocObj;
		m_pDocTemplate3DocObj->m_pDoc = this;

		////////////////////////End Create Document Object///////////////////////////
		////////////////////////Begin Create External Document Object////////////////
		//We need the External Document Object has a property "Document" which hold a pointer
		//to the Document object created above, by use this property, an external component can hook the 
		//document object's events, call its methods , modify its properties etc. 
		//In general the Document Object may reference the external Document object(and vice versa).
		//when Document object and external Document object establish a reference eachother,
		//they can interact on each other more effectively.
		//External Document Object is usually supply by user or other developer
		if(m_pDotNetDocObj&&m_strExternalDocObjID!=_T(""))
		{
			nPos = m_strExternalDocObjID.Find(_T("#"));
			m_strAssembly = m_strExternalDocObjID.Left(nPos);
			m_strObjID = m_strExternalDocObjID.Mid(nPos+1);
			strAssembly = m_strAssembly;
			strObjID = m_strObjID;
			Assembly* m_pDotNetAssembly = Assembly::Load(strAssembly);
			Type* m_pDotNetExternalType = m_pDotNetAssembly->GetType(strObjID,true,true);
			m_pExternalDocObj = __try_cast< Object* >(Activator::CreateInstance(m_pDotNetExternalType)); 
			//�����ĵ���������չ�ĵ�����֮��Ľ���;����
			PropertyInfo* m_pPropertyInfo = m_pDotNetExternalType->GetProperty(S"Document");
			m_pPropertyInfo->SetValue(m_pExternalDocObj,m_pDotNetDocObj,NULL);
			PropertyInfo* m_pPropertyInfo2 = m_pDotNetDocType->GetProperty(S"ExternalDocument");
			m_pPropertyInfo2->SetValue(m_pDotNetDocObj,m_pExternalDocObj,NULL);
		}
		////////////////////////End Create External Document Object//////////////////
	}
	catch (Exception* exp)
	{
		CString strInfo = exp->Message;
		AfxMessageBox(strInfo);
		return ;
	}
}

BOOL CDocTemplate3Doc::OnNewDocument()
{
	if (!COleDocument::OnNewDocument())
		return FALSE;
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	InitialDocument();

	if(m_pDocTemplate3DocObj)
	{
		//�����麯�������û��򿪷��������ĵ�����ʱ����������
		//�ĵ������OnNewDocument������
		m_pDocTemplate3DocObj -> OnNewDocument();
		//�����ĵ������¼������û��򿪷����ṩ��չ�ĵ�����ʱ��
		//��������չ�ĵ���������Ӧ����¼���
		m_pDocTemplate3DocObj -> Fire_NewDoc();
	}
	return TRUE;
}

BOOL CDocTemplate3Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!COleDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	if(m_pDocTemplate3DocObj)
	{
		//�����麯�������û��򿪷��������ĵ�����ʱ����������
		//�ĵ������OnOpenDocument������
		m_pDocTemplate3DocObj->OnOpenDocument();
		String* strFile = lpszPathName;
		//�����ĵ������CloseDoc�¼�������չ�ĵ��������ʱ��
		//��������չ�ĵ���������Ӧ����¼���
		m_pDocTemplate3DocObj -> Fire_OpenDoc(strFile);
	}
	// TODO:  Add your specialized creation code here

	return TRUE;
}

void CDocTemplate3Doc::OnCloseDocument()
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_pDocTemplate3DocObj)
	{
		//�����ĵ������CloseDoc�¼�������չ�ĵ��������ʱ��
		//��������չ�ĵ���������Ӧ����¼���
		m_pDocTemplate3DocObj -> Fire_CloseDoc();
	}
	COleDocument::OnCloseDocument();
}

// CDocTemplate3Doc serialization

void CDocTemplate3Doc::Serialize(CArchive& ar)
{
	m_DocumentInfo.m_pStorage = m_lpRootStg;
	m_DocumentInfo.Serialize(ar);

	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << m_strDocObjID;
		ar << m_strExternalDocObjID;
	}
	else
	{
		// TODO: add loading code here
		ar >> m_strDocObjID;
		ar >> m_strExternalDocObjID;
		InitialDocument();
	}

	COleDocument::Serialize(ar);
}


// CDocTemplate3Doc diagnostics

#ifdef _DEBUG
void CDocTemplate3Doc::AssertValid() const
{
	COleDocument::AssertValid();
}

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


// CDocTemplate3Doc commands

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions