本文针对单文档模式的MFC程序进行分析。一步步说明,如何创建窗口并显示。
首先我们创建一个单文档的MFC程序:Test。
1:打开CTestApp::InitInstance(),可以看到
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CTestView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
首先创建了一个单文档的模板,并且添加到模板管理器。
真正的创建窗口在ProcessShellCommand(cmdInfo),追踪CWinApp::ProcessShellCommand(cmdInfo) 可以看到调用了CWinApp::OpenDocumentFile
而在CWinApp::OpenDocumentFile中有调用了 m_pDocManager->OpenDocumentFile(lpszFileName);
通过文档模板管理器,绝对调用哪个模板的 return pBestTemplate->OpenDocumentFile(szPath);
这里是单文档,因此调用了单文档模板 CSingleDocTemplate::OpenDocumentFile 。
在 CSingleDocTemplate::OpenDocumentFile 中,继续可以看到 pFrame = CreateNewFrame(pDocument, NULL);
继续追踪 CDocTemplate::CreateNewFrame 则有 pFrame->LoadFrame(m_nIDResource,WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,NULL, &context)
这里调用了CFrameWnd::LoadFrame,而 CFrameWnd::LoadFrame 调用
CFrameWnd::Create创建窗口。
但是真正的创建窗口的动作在 CWnd::CreateEx 中。因此可以看到 CWnd::CreateEx 被 CFrameWnd::Create 调用。
只有显示窗口则在CTestApp::InitInstance 中的
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow(); 来完成。