[轉]MFC下關於“建立空文檔失敗”問題的分析


這類問題的出現主要在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)


3&­'  
  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()) 
{


q  
 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;Mj8F  
 else 
  return;     // none - cancel operation


w  
}

assert(ptemplate != NULL);


]kW  
ASSERT_KINDOF(CDocTemplate, pTemplate);

ptemplate->opendocumentfile(null);

通過上面的代碼我們可以看出,cwinapp的onfilenew和onfileopen分別調用cdocmanager的虛擬函數onfilenew和onfileopen。而在cdocmanager里面。通過模板鏈表選擇不同的模板來調用文檔模板的opendocumentfile(); 
如果傳入參數NULL表示新建文件。

下面我們來看看cdoctemplate::opendocumentfile()它是一個最關鍵的函數。因為他是虛擬函數,我們考慮CSingleDocTemplate::OpenDocumentFile的情況。


4RqL=  
這個函數里面有一段代碼: 
其中: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)N+  

AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC); 
return NULL; 

ASSERT(pDocument == m_pOnlyDoc);?#Z  
if (pFrame == NULL) 

ASSERT(bCreated);

// create frame - set as main document frame


@(m`w  
BOOL bAutoDelete = pDocument->m_bAutoDelete; 
pDocument->m_bAutoDelete = FALSE; 
// don’t destroy if something goes wrong 
pFrame = CreateNewFrame(pDocument, NULL); 
pDocument->m_bAutoDelete = bAutoDelete;1nD{WwQ  
if (pFrame == NULL) 

 AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC); 
 delete pDocument;       // explicit delete on error 
 return NULL; 
}

通過觀察上面的代碼我們很容易的看出有兩個可能出錯的原因:1 CreateNewDocument返回為NULL 2 createnewframe 返回為空。

先看 CreateNewDocument() 一般來說這個函數很少失敗。不過在調試時也不能掉以輕心。


6GYC
>'@  
再看看CreateNewFrame()  里面有一個函數LoadFrame是造成這種“建立新文檔失敗”錯誤的源泉所在。 
只要它返回False就會彈出這樣的提示。 
我們在來看看LoadFrame() 里面調用CFrameWnd::Create()來創建窗口,創建窗口失敗返回Fasle。 
這樣問題就變的比較簡單了。

看看create和createex函數的動作就知道怎么回事了。


[V:!R  
****************************************************************S_zS  
1如果找不到菜單資源返回False 同時也彈出“建立空文檔失敗” 
HINSTANCE hInst = AfxFindResourceHandle(lpszMenuName, RT_MENU); 
if ((hMenu = ::LoadMenu(hInst, lpszMenuName)) == NULL) 

 TRACE0("Warning: failed to load menu for CFrameWnd. 
"); 
 PostNcDestroy();            // perhaps delete the C++ object 
 return FALSE; 

2重載了PreCreateWindow而且返回False也會導致彈出“建立空文檔失敗” 
3在OnCreate 里面返回-1 也會導致彈出“建立空文檔失敗”。 
******************************************************************

以上就是我分析的出現這樣“建立空文檔失敗”問題的大致原因。也許還有其他的原因。我這里就不一一列舉了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM