这类问题的出现主要在bool CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo);
函数的内容如下(在C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc\appui2.cpp)
BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
{
BOOL bResult = TRUE;
switch (rCmdInfo.m_nShellCommand)
{
case CCommandLineInfo::FileNew:
if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
OnFileNew();
if (m_pMainWnd == NULL)
bResult = FALSE;
break;
// If we've been asked to open a file, call OpenDocumentFile()
case CCommandLineInfo::FileOpen:
if (!OpenDocumentFile(rCmdInfo.m_strFileName))
bResult = FALSE;
break;
// If the user wanted to print, hide our main window and
// fire a message to ourselves to start the printing
case CCommandLineInfo::FilePrintTo:
case CCommandLineInfo::FilePrint:
m_nCmdShow = SW_HIDE;
ASSERT(m_pCmdInfo == NULL);
if(OpenDocumentFile(rCmdInfo.m_strFileName))
{
m_pCmdInfo = &rCmdInfo;
ENSURE_VALID(m_pMainWnd);
m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);
m_pCmdInfo = NULL;
}
bResult = FALSE;
break;
// If we're doing DDE, hide ourselves
case CCommandLineInfo::FileDDE:
m_pCmdInfo = (CCommandLineInfo*)(UINT_PTR)m_nCmdShow;
m_nCmdShow = SW_HIDE;
break;
// If we've been asked to register, exit without showing UI.
// Registration was already done in InitInstance().
case CCommandLineInfo::AppRegister:
{
Register();
bResult = FALSE; // that's all we do
// If nobody is using it already, we can use it.
// We'll flag that we're unregistering and not save our state
// on the way out. This new object gets deleted by the
// app object destructor.
if (m_pCmdInfo == NULL)
{
m_pCmdInfo = new CCommandLineInfo;
m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
}
break;
}
// If we've been asked to unregister, unregister and then terminate
case CCommandLineInfo::AppUnregister:
{
BOOL bUnregistered = Unregister();
// if you specify /EMBEDDED, we won't make an success/failure box
// this use of /EMBEDDED is not related to OLE
if (!rCmdInfo.m_bRunEmbedded)
{
if (bUnregistered)
AfxMessageBox(AFX_IDP_UNREG_DONE);
else
AfxMessageBox(AFX_IDP_UNREG_FAILURE);
}
bResult = FALSE; // that's all we do
// If nobody is using it already, we can use it.
// We'll flag that we're unregistering and not save our state
// on the way out. This new object gets deleted by the
// app object destructor.
if (m_pCmdInfo == NULL)
{
m_pCmdInfo = new CCommandLineInfo;
m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
}
}
break;
}
return bResult;
}
通过上面的内容我们可以看出:如果没有对ID_FILE_NEW做映射的话出现问题就在OnFileNew(); CWinApp对OnFileNew的默认实现是调用m_pDocManager->OnFileNew();我们继续解析cdocmanager,它究竟干了些什么? (首先说明一下CDocManager它主要的功能是帮助CWinApp是管理文档模板链表和注册文件类型.)
//如果模板列表为空的话
if (m_templateList.IsEmpty())
{
TRACE0("Error: no document templates registered with CWinApp.");
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC); //报错并返回.这里不会报建立新文档出错。
return;
}
cdoctemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead();
if (m_templateList.GetCount() > 1)
{
// more than one document template to choose from
// bring up dialog prompting user
CNewTypeDlg dlg(&m_templateList);
int nID = dlg.DoModal();
if (nID == IDOK)
pTemplate = dlg.m_pSelectedTemplate;
else
return; // none - cancel operation
}
assert(ptemplate != NULL);
ASSERT_KINDOF(CDocTemplate, pTemplate);
ptemplate->opendocumentfile(null);
过观察上面的代码我们很容易的看出 有两个可能出错的原因:1 CreateNewDocument返回为NULL 2 createnewframe 返回为空。先看 CreateNewDocument() 一般来说这个函数很少失败。不过在调试时也不能掉以轻心。 再看看CreateNewFrame() 里面有一个函数LoadFrame是造成这种“建立新文档失败”错误的源泉所在。 只要它返回False就会弹出这样的提示。 我们在来看看LoadFrame() 里面调用CFrameWnd::Create()来创建窗口,创建窗口失败返回Fasle。 这样问题就变的比较简单了。看看create和createex函数的动作就知道怎么回事了。
1 如果找不到菜单资源 返回False 同时也弹出“建立空文档失败”
2 重载了PreCreateWindow而且返回False也会导致弹出“建立空文档失败”
3 在OnCreate 里面返回-1 也会导致弹出“建立空文档失败”。