MFC 程序来龙去脉_子文档 4_子文档 1
寻找 m_pMainWnd 初始化时机
直接搜索所有 m_pMainWnd 赋值语句(m_pMainWnd = )会发现如下代码:
(1)
UINT APIENTRY _AfxThreadEntry(void* pParam) {
pThread->m_pMainWnd = &threadWnd;
}
(2)
CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible)
{
pThread->m_pMainWnd = pFrame;
}
初步判定使用 OpenDocumentFile 初始化的,于是查找相关调用语句:
void CDocManager::OnFileNew()
{
if (m_templateList.IsEmpty())
{
TRACE(traceAppMsg, 0, "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_PTR 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);
// if returns NULL, the user has already been alerted
}
继续查找 CDocManager::OnFileNew() 调用语句:
void CWinApp::OnFileNew()
{
if (m_pDocManager != NULL)
m_pDocManager->OnFileNew();
}
继续查找 CWinApp::OnFileNew() 调用语句:
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;
}
回忆一下:在 CFirstMFCDemoApp::InitInstance() 中有这么一行代码:ProcessShellCommand(cmdInfo) ,我们不关心它干了哪些事情,。整理一下思路吧:
(1)CWinApp::ProcessShellCommand 调用 CWinApp::OnFileNew()
(2)CWinApp::OnFileNew() 调用 CDocManager::OnFileNew()
(3)CDocManager::OnFileNew() 调用 CSingleDocTemplate::
OpenDocumentFile
(4) CSingleDocTemplate::OpenDocumentFile执行语句
pThread->m_pMainWnd = pFrame