如何在dll中创建MDI,在我的项目中失败 [英] How to create mdi in dll , failed in my project
问题描述
在Dll中,我创建了继承于CMultiDoctemplate的新类CODMMultiDoctemplate,并且创建了CChildFrame,COnDemandCMDoc,COnDemandCMView.
首先,创建新模板
In Dll,I create new class CODMMultiDoctemplate inhert CMultiDoctemplate,and CChildFrame,COnDemandCMDoc,COnDemandCMView .
First,create new Template
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_pOnDemandCMTemplate = new CODMMultiDocODMTemplate(hinst,
203,
RUNTIME_CLASS(COnDemandCMDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(COndemandCmView)
);
ASSERT_KINDOF(CODMMultiDocODMTemplate,m_pOnDemandCMTemplate);
然后,调用openDocument函数,但失败.
then,call openDocument function but failed.
CDocument* CReaderODMApp::opendocument()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
ASSERT(!lpTitle.IsEmpty());
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
COnDemandCMDoc* pDocument = new COnDemandCMDoc;
if (pDocument == NULL)
{
TRACE0("CDocTemplate::CreateNewDocument returned NULL.\n");
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return NULL;
}
ASSERT_VALID(pDocument);
BOOL bAutoDelete = pDocument->m_bAutoDelete;
pDocument->m_bAutoDelete = FALSE; // don't destroy if something goes wrong
CFrameWnd* pFrame =m_pOnDemandCMTemplate->CreateNewFrame(pDocument, NULL);
pDocument->m_bAutoDelete = bAutoDelete;
if (pFrame == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
delete pDocument; // explicit delete on error
return NULL;
}
ASSERT_VALID(pFrame);
// open an existing document
CWaitCursor wait;
if (!pDocument->OnOpenDocument(lpTitle))
{
// user has be alerted to what failed in OnOpenDocument
TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
pFrame->DestroyWindow();
return NULL;
}
pDocument->SetPathName(lpTitle,FALSE);
m_pOnDemandCMTemplate->InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
return pDocument;
}
我调试了,然后发现了一个
CFrameWnd * pFrame = m_pOnDemandCMTemplate-> CreateNewFrame(pDocument,NULL);
例外.
然后我发现了
I debug it ,and i found a
CFrameWnd* pFrame =m_pOnDemandCMTemplate->CreateNewFrame(pDocument, NULL);
exception here.
then i found
BOOL CMDIChildWnd::Create(LPCTSTR lpszClassName,
LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CMDIFrameWnd* pParentWnd,
CCreateContext* pContext)
{
if (pParentWnd == NULL)
{
CWnd* pMainWnd = AfxGetThread()->m_pMainWnd;
ASSERT(pMainWnd != NULL);
ASSERT_KINDOF(CMDIFrameWnd, pMainWnd);
pParentWnd = (CMDIFrameWnd*)pMainWnd;
}
....
}
ASSERT(pMainWnd!= NULL);这里的异常,pMainWnd为NULL
为什么?
ASSERT(pMainWnd != NULL); exception here ,pMainWnd is NULL
why?
推荐答案
可能,您必须设置CReaderODMApp::m_pMainWnd
转到应用程序的主框架窗口中的功能:
/*virtual*/ BOOL CReaderODMApp::InitInstance()
:)
第二部分:
Probably, you have to set theCReaderODMApp::m_pMainWnd
to the application''s main frame window in the function:
/*virtual*/ BOOL CReaderODMApp::InitInstance()
:)
Part Two:
// exe-app.h
class CYourExeApp : public CWinApp
{
// class CODMMultiDocODMTemplate is exported by your Dll:
CODMMultiDocODMTemplate* m_pcDllTemplate;
...
public:
...
// to be visible from the Dll-side:
CODMMultiDocODMTemplate* GetDllTemplate() { return pcDllTemplate; };
};
// exe-app.cpp
CYourExeApp::CYourExeApp()
{
...
m_pcDllTemplate = NULL;
...
}
BOOL CYourExeApp::InitInstance()
{
...
InitYourDllTemplate();
...
}
void CYourExeApp::InitYourDllTemplate()
{
/*CODMMultiDocODMTemplate* member, by Dll exported class*/
m_pcDllTemplate = new CODMMultiDocODMTemplate(IDR_YOUR_ID,
RUNTIME_CLASS(COnDemandCMDoc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(COndemandCmView));
m_pcDllTemplate->SetContainerInfo(IDR_YOUR_ID);
AddDocTemplate(m_pcDllTemplate); // good luck ! :)
}
请不要在DLL中使用(删除)任何管理器",但是:
Please do not use (remove) any "managers" in your DLL, but:
#include "..\YourApp\YourApp.h"
void CSomeDllClass::SomeDllFunction()
{
...
// we are _inside_ , and this is _our_ app-object:
CYourExeApp* pcApp = (CYourExeApp*) AfxGetApp();
ASSERT(pcApp);
COnDemandCMDoc* pcDoc = CDSTATIC_DOWNCAST(COnDemandCMDoc,
pcApp->GetDllTemplate()->OpenDocumentFile((LPCTSTR) NULL));
...
}
课程:
-CODMMultiDocODMTemplate
-COnDemandCMDoc
-COndemandCmView
...应由您的Dll定义,实施和导出:)
The classes:
- CODMMultiDocODMTemplate
- COnDemandCMDoc
- COndemandCmView
...should be defined, implemented and exported by your Dll :)
这篇关于如何在dll中创建MDI,在我的项目中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!