Click here to Skip to main content
15,891,896 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.
// [!output PROJECT_NAME]Doc.cpp : implementation of the C[!output PROJECT_NAME]Doc class
//

#include "stdafx.h"
#include "[!output PROJECT_NAME].h"
#include "[!output PROJECT_NAME]Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// C[!output PROJECT_NAME]Doc

IMPLEMENT_DYNCREATE(C[!output PROJECT_NAME]Doc, COleDocument)

BEGIN_MESSAGE_MAP(C[!output PROJECT_NAME]Doc, COleDocument)
END_MESSAGE_MAP()


// C[!output PROJECT_NAME]Doc construction/destruction

C[!output PROJECT_NAME]Doc::C[!output PROJECT_NAME]Doc()
{
	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="[!output PROJECT_NAME]#[!output PROJECT_NAME].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("[!output PROJECT_NAME]#[!output PROJECT_NAME].[!output PROJECT_NAME]DocObject");

	//�õ���չ�ĵ������IDֵ��
	if(g_pDotNetExtImpl->m_strCurExtDocObjID!=_T(""))
		m_strExternalDocObjID = g_pDotNetExtImpl->m_strCurExtDocObjID;
	else
		m_strExternalDocObjID = _T("");
	m_pExternalDocObj = NULL;
	m_p[!output PROJECT_NAME]DocObj = NULL;
}

C[!output PROJECT_NAME]Doc::~C[!output PROJECT_NAME]Doc()
{
	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_p[!output PROJECT_NAME]DocObj;
}

//Create Document Object and External Document Object:
void C[!output PROJECT_NAME]Doc::InitialDocument()
{
	try
	{
		/***************************************************************************
		InitialDocument���ڴ���MFC�ĵ��Ķ�ż.NET�����Լ���չ�ĵ�����ͬʱҲ��������
		����������չ�ĵ�����֮��Ĺ�ͨ;��������ϣ����չ�ĵ�������һ�����ԣ��������
		�����ĵ�����ͨ�������ԣ���չ�ĵ�������Է����ĵ����󣬷�����������Ϊ��չ��
		������ָ��һ���������࣬��ʵ�ʴ��ݵ��Ǹû������������IDֵ����ˣ�Ӧ�ó���
		�γ����ġ���������ƣ���ÿ����˲�������չ�ĵ�ģ��������Ϊһ�������
		TangramLittleϵͳ�������ط�����InitialDocument����һ���ط��Ǵ������ĵ�ʱ����
		���λ����C[!output PROJECT_NAME]Doc::OnNewDocument()����һ���ط������ڴ���
		���ڵ��ĵ�ʱ�������λ����C[!output PROJECT_NAME]Doc::Serialize(CArchive& ar)��
		���ǿ�����InitialDocument�ṩ���������ο�����;����һ��������MFC�ĵ���ż����
		��������һ���ṩ��չ�ĵ�����
		***************************************************************************/
		////////////////////////Begin Create Document Object////////////////////////
		/***************************************************************************
		�����ϣ���ĵ�����߱��������ƣ������´����ĵ�����
			#pragma push_macro("new")
			#undef new
				m_p[!output PROJECT_NAME]DocObj = new [!output PROJECT_NAME]::[!output PROJECT_NAME]DocObject();
			#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);
		[!output PROJECT_NAME]::[!output PROJECT_NAME]DocObject*	m_pDotNetDocObj = NULL;
		Object*	m_pExternalDocObj = NULL;
		m_pDotNetDocObj = __try_cast< [!output PROJECT_NAME]::[!output PROJECT_NAME]DocObject* >(Activator::CreateInstance(m_pDotNetDocType)); 
		m_p[!output PROJECT_NAME]DocObj = m_pDotNetDocObj;
		m_p[!output PROJECT_NAME]DocObj->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 C[!output PROJECT_NAME]Doc::OnNewDocument()
{
	if (!COleDocument::OnNewDocument())
		return FALSE;
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	InitialDocument();

	if(m_p[!output PROJECT_NAME]DocObj)
	{
		//�����麯�������û��򿪷��������ĵ�����ʱ����������
		//�ĵ������OnNewDocument������
		m_p[!output PROJECT_NAME]DocObj -> OnNewDocument();
		//�����ĵ������¼������û��򿪷����ṩ��չ�ĵ�����ʱ��
		//��������չ�ĵ���������Ӧ����¼���
		m_p[!output PROJECT_NAME]DocObj -> Fire_NewDoc();
	}
	return TRUE;
}

BOOL C[!output PROJECT_NAME]Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!COleDocument::OnOpenDocument(lpszPathName))
		return FALSE;

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

	return TRUE;
}

void C[!output PROJECT_NAME]Doc::OnCloseDocument()
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_p[!output PROJECT_NAME]DocObj)
	{
		//�����ĵ������CloseDoc�¼�������չ�ĵ��������ʱ��
		//��������չ�ĵ���������Ӧ����¼���
		m_p[!output PROJECT_NAME]DocObj -> Fire_CloseDoc();
	}
	COleDocument::OnCloseDocument();
}

// C[!output PROJECT_NAME]Doc serialization

void C[!output PROJECT_NAME]Doc::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);
}


// C[!output PROJECT_NAME]Doc diagnostics

#ifdef _DEBUG
void C[!output PROJECT_NAME]Doc::AssertValid() const
{
	COleDocument::AssertValid();
}

void C[!output PROJECT_NAME]Doc::Dump(CDumpContext& dc) const
{
	COleDocument::Dump(dc);
}
#endif //_DEBUG


// C[!output PROJECT_NAME]Doc 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