最近需要做这样一个在DLL中创建SDI窗体,在网上找了些资料,但是还是有些问题。
我从CDocument,CView, CFrameWnd派生出了自己的类,在DLL的App中重载了InitInstance方法,
代码如下:
BOOL CTestApp::InitInstance()
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMainDocument),
RUNTIME_CLASS(CMainFrame), // custom MDI child frame
RUNTIME_CLASS(CMainView));
AddDocTemplate(pDocTemplate);
CMainDocument *pDocument = new CMainDocument;
// 创建新的frame
CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(pDocument, NULL);
if (NULL == pFrame)
{
return FALSE;
}
pDocTemplate->InitialUpdateFrame(pFrame, pDocument);
return TRUE;
}然后导出函数:
extern "C" _declspec(dllexport) void Test()
{
theApp.InitInstance();
}在主程序调用Test()时第一次能成功创建窗体,但是我发现在InitInstance中创建新的frame时返回false,而且第二次就不能成功了,哪位做过类似的或者有没有其他更好的办法?
我从CDocument,CView, CFrameWnd派生出了自己的类,在DLL的App中重载了InitInstance方法,
代码如下:
BOOL CTestApp::InitInstance()
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMainDocument),
RUNTIME_CLASS(CMainFrame), // custom MDI child frame
RUNTIME_CLASS(CMainView));
AddDocTemplate(pDocTemplate);
CMainDocument *pDocument = new CMainDocument;
// 创建新的frame
CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(pDocument, NULL);
if (NULL == pFrame)
{
return FALSE;
}
pDocTemplate->InitialUpdateFrame(pFrame, pDocument);
return TRUE;
}然后导出函数:
extern "C" _declspec(dllexport) void Test()
{
theApp.InitInstance();
}在主程序调用Test()时第一次能成功创建窗体,但是我发现在InitInstance中创建新的frame时返回false,而且第二次就不能成功了,哪位做过类似的或者有没有其他更好的办法?
{
//加下以下三句
CoInitialize(NULL);
AfxEnableControlContainer();
AFX_MANAGE_STATE(AfxGetStaticModuleState());
......
}然后导出函数:
extern "C" _declspec(dllexport) void Test()
{
//导出函数应加上以下一句
AFX_MANAGE_STATE(AfxGetStaticModuleState()); ......
}
我前几天刚把一个MDI封到DLL中,如果你想看的话请回贴说,我可以贴上来给你看看。