Click here to Skip to main content
15,881,882 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.
#include "StdAfx.h"
#include "DocTemplate3DocObject.h"
#include "DocTemplate3Doc.h"
#include "ManagedObj.h"

DocTemplate3::DocTemplate3DocObject::DocTemplate3DocObject(void)
{
	theApp.m_pManagedCnnObj = this;
	m_pDoc = (CDocTemplate3Doc *)theApp.m_pCurDoc;
	m_pMfcUserControl = new MfcUserControl();
	set_PrjItem(S"MfcUserCtrl", m_pMfcUserControl);
}

DocTemplate3::DocTemplate3DocObject::~DocTemplate3DocObject(void)
{
}

void DocTemplate3::DocTemplate3DocObject::OnNewDocument()
{
}

void DocTemplate3::DocTemplate3DocObject::OnOpenDocument()
{
}

void DocTemplate3::DocTemplate3DocObject::Connect(Object* pCtrlObj, String* strObjName)
{
	//insert object into object dictionary,thus other component can
	//using this object through the property "PrjItem" of a document object:
	set_PrjItem(strObjName, pCtrlObj);
	//you can convert pCtrlObj to some proper object like this:
	//somenamespace::someobj* pNewCtrlObj = __try_cast<somenamespace::someobj*>(pCtrlObj);
	//if you want to hook event for this object,you can do like this:
	//if(strObjName == S"somename")
	//{
	//	pNewCtrlObj->add_SomeEvet(new System::EventHandler(this,OnSomeFunctionInThisObject));
	//}

	//if there exists an external document object, we pass pCtrlObj to this external document object,
	//the provider of external document object can get pCtrlObj in the following manner:
	if(m_pExternalDocObj)
	{
		Type* t = m_pExternalDocObj->GetType();
		MethodInfo* mi = t->GetMethod(S"Connect");
		Object* p[] = new Object*[2];
		p[0] = pCtrlObj;
		p[1] = strObjName;
		mi->Invoke(m_pExternalDocObj,p);
	}
}

//Sample Properties implement:
String* DocTemplate3::DocTemplate3DocObject::get_Caption() 
{ 
	if(m_pDoc)
	{
		String* strCaption = m_pDoc->GetTitle();
		return strCaption; 
	}
	return S"";
}

void DocTemplate3::DocTemplate3DocObject::set_Caption(String* newCaption)
{
	if(m_pDoc)
	{
		CString strCaption = theApp.StringToCStr(newCaption);
		m_pDoc->SetTitle(LPCTSTR(strCaption));
	}
}

void DocTemplate3::DocTemplate3DocObject::set_PrjItem(String* strIndex, Object* value) 
{ 
	CString m_strDocIndex = theApp.StringToCStr(strIndex);
	m_strDocIndex.MakeLower();
	CManagedObj* m_pManagedObj = NULL;
	if(!m_pDoc->m_PrjItemDictionary.Lookup(LPCTSTR(m_strDocIndex),(CObject*&)m_pManagedObj))
	{
		m_pManagedObj = new CManagedObj();
		m_pManagedObj -> m_pManagedObj = value;
		m_pDoc->m_PrjItemDictionary.SetAt(m_strDocIndex, m_pManagedObj);
	}
}

Object* DocTemplate3::DocTemplate3DocObject::get_PrjItem(String* strIndex) 
{ 
	CString m_strDocIndex = theApp.StringToCStr(strIndex);
	m_strDocIndex.MakeLower();
	CManagedObj* m_pManagedObj = NULL;
	if(m_pDoc->m_PrjItemDictionary.Lookup(LPCTSTR(m_strDocIndex),(CObject*&)m_pManagedObj))
		return m_pManagedObj->m_pManagedObj;
	return NULL;
}

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