Article ID: 118435
Article Last Modified on 12/2/2003
class CSharedDocTemplate : public CMultiDocTemplate
{
public:
CSharedDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass);
virtual ~CSharedDocTemplate();
};
CSharedDocTemplate::CSharedDocTemplate(UINT nIDResource,
CRuntimeClass* pDocClass, CRuntimeClass* pFrameClass,
CRuntimeClass* pViewClass) : CMultiDocTemplate(nIDResource, pDocClass,
pFrameClass, pViewClass)
{
}
CSharedDocTemplate::~CSharedDocTemplate()
{
// This prevents the base class virtual destructor from
// destroying the menu resource (i.e. its checks to make
// sure the handle isn't NULL)
m_hMenuShared = NULL;
}
At this point, declare a public member variable to hold the common menu for
the application; the CWinApp-derived object will take the responsibility of
loading and destroying the common menu.
class CMyApp : public CWinApp
{
public:
HMENU m_ghMenuShared;
//...
};
Now, after the CSharedDocTemplate object has been created, you need to set
its public member variable to the common menu resouce for the application.
The following code fragments demonstrate how to set the handle for a single
template or traverse the template list and set the common handle for all
CSharedDocTemplate objects. Either of these code fragments could be used in
the InitInstance of the CWinApp-derived object:
BOOL CMyApp::InitInstance()
{
...
// New member of CWinApp derived class to store menu handle
m_ghMenuShared = ::LoadMenu(m_hInstance,
MAKEINTRESOURCE(IDR_SHAREDMENU));
CSharedDocTemplate* pDocTemplate;
pDocTemplate = new CSharedDocTemplate(
IDR_MYTYPE, // No menu resource should exist for
// this ID, the menu is set manually
RUNTIME_CLASS(CMDIMyDoc),
RUNTIME_CLASS(CMDIChildWnd),
RUNTIME_CLASS(CMDIMyView));
pDocTemplate->m_hMenuShared = m_ghMenuShared;
AddDocTemplate(pDocTemplate);
...
}
-or-
BOOL CMyApp::InitInstance()
{
...
m_ghMenuShared = ::LoadMenu(m_hInstance,
MAKEINTRESOURCE(IDR_SHAREDMENU));
if (!m_templateList.IsEmpty())
{
CDocTemplate *pDocTemplate;
for (POSITION rPos = m_templateList.GetHeadPosition();
rPos != NULL;)
{
pDocTemplate = m_templateList.GetNext(rPos);
if (pDocTemplate->IsKindOf(RUNTIME_CLASS(CSharedDocTemplate)))
((CSharedDocTemplate *)pDocTemplate)->m_hMenuShared =
m_ghMenuShared;
}
}
...
}
Lastly, you need to destroy the menu resource loaded in InitInstance. This
can easily be accomplished by overriding the ExitInstance member of the
CWinApp-derived class:
int CMyApp::ExitInstance()
{
::DestroyMenu(m_ghMenuShared);
}
Additional query words: kbinf 1.00 1.50 2.00 2.10 2.50 3.00 4.00
Keywords: kbinfo kbuidesign KB118435