這類問題的出現主要在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;
case CCommandLineInfo::FileOpen:
if (!OpenDocumentFile(rCmdInfo.m_strFileName))
bResult = FALSE;
break;
通過上面的內容我們可以看出:如果沒有對ID_FILE_NEW做映射的話出現問題就在OnFileNew();
CWinApp對OnFileNew的預設實現是調用m_pDocManager->OnFileNew();
我們繼續解析CDocManager,它究竟幹了些什嗎?
(首先說明一下CDocManager它主要的功能是協助CWinApp是管理文件範本鏈表和註冊檔案類型.)
//如果模板列表為空白的話
if (m_templateList.IsEmpty())
{
TRACE0("Error: no document templates registered with CWinApp.\n");
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);
通過上面的代碼我們可以看出,CWinApp的OnFileNew和OnFileOpen分別調用CDocManager的虛擬函數OnFileNew
和OnFileOpen。而在CDocManager裡面。通過模板鏈表選擇不同的模板來調用文件範本的OpenDocumentFile();
如果傳入參數NULL表示建立檔案。
下面我們來看看CDocTemplate::OpenDocumentFile()它是一個最關鍵的函數。因為他是虛擬函數,我們考慮
CSingleDocTemplate::OpenDocumentFile的情況。
這個函數裡面有一段代碼:
其中:AFX_IDP_FAILED_TO_CREATE_DOC 就是字元“建立空文檔失敗”的資源id
// create a new document
pDocument = CreateNewDocument();
ASSERT(pFrame == NULL); // will be created below
bCreated = TRUE;
if (pDocument == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return NULL;
}
ASSERT(pDocument == m_pOnlyDoc);
if (pFrame == NULL)
{
ASSERT(bCreated);
// create frame - set as main document frame
BOOL bAutoDelete = pDocument->m_bAutoDelete;
pDocument->m_bAutoDelete = FALSE;
// don't destroy if something goes wrong
pFrame = 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;
}
通過觀察上面的代碼我們很容易的看出 有兩個可能出錯的原因:1 CreateNewDocument返回為NULL 2
CreateNewFrame 返回為空白。
先看 CreateNewDocument() 一般來說這個函數很少失敗。不過在調試時也不能掉以輕心。
再看看CreateNewFrame() 裡面有一個函數LoadFrame是造成這種“建立新文檔失敗”錯誤的源泉所在。
只要它返回False就會彈出這樣的提示。
我們在來看看LoadFrame() 裡面調用CFrameWnd::Create()來建立視窗,建立視窗失敗返回Fasle。
這樣問題就變的比較簡單了。
看看Create和CreateEx函數的動作就知道怎麼回事了。
****************************************************************
1 如果找不到菜單資源 返回False 同時也彈出“建立空文檔失敗”
HINSTANCE hInst = AfxFindResourceHandle(lpszMenuName, RT_MENU);
if ((hMenu = ::LoadMenu(hInst, lpszMenuName)) == NULL)
{
TRACE0("Warning: failed to load menu for CFrameWnd.\n");
PostNcDestroy(); // perhaps delete the C++ object
return FALSE;
}
2 重載了PreCreateWindow而且返回False也會導致彈出“建立空文檔失敗”
3 在OnCreate 裡面返回-1 也會導致彈出“建立空文檔失敗”。
******************************************************************