MFC初学者,使用MFC的向导生成的简单单文档程序中的CMainFrame类是在什么时候构造的啊?
如题:
CWinApp的对象是直接由全局对象构造的,
那CMainFrame类的对象是什么时候构造的呢?我跟踪了很久,发现是深不可测啊!
高手能不能指点我一下啊,
关于CMainFrame类的构造侯捷的《深入浅出MFC》和孙鑫的《VC++深入详解》都没有提到
[解决办法]
/////////////////////////////////////////////////////////////////////////////
// CSingleDocTemplate commands
CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
BOOL bMakeVisible)
// if lpszPathName == NULL => create new file of this type
{
CDocument* pDocument = NULL;
CFrameWnd* pFrame = NULL;
BOOL bCreated = FALSE; // => doc and frame created
BOOL bWasModified = FALSE;
if (m_pOnlyDoc != NULL)
{
// already have a document - reinit it
pDocument = m_pOnlyDoc;
if (!pDocument-> SaveModified())
return NULL; // leave the original one
pFrame = (CFrameWnd*)AfxGetMainWnd();
ASSERT(pFrame != NULL);
ASSERT_KINDOF(CFrameWnd, pFrame);
ASSERT_VALID(pFrame);
}
else
{
// 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;
}
}
if (lpszPathName == NULL)
{
// create a new document
SetDefaultTitle(pDocument);
// avoid creating temporary compound file when starting up invisible
if (!bMakeVisible)
pDocument-> m_bEmbedded = TRUE;
if (!pDocument-> OnNewDocument())
{
// user has been alerted to what failed in OnNewDocument
TRACE(traceAppMsg, 0, "CDocument::OnNewDocument returned FALSE.\n ");
if (bCreated)
pFrame-> DestroyWindow(); // will destroy document
return NULL;
}
}
else
{
CWaitCursor wait;
// open an existing document
bWasModified = pDocument-> IsModified();
pDocument-> SetModifiedFlag(FALSE); // not dirty for open
if (!pDocument-> OnOpenDocument(lpszPathName))
{
// user has been alerted to what failed in OnOpenDocument
TRACE(traceAppMsg, 0, "CDocument::OnOpenDocument returned FALSE.\n ");
if (bCreated)
{
pFrame-> DestroyWindow(); // will destroy document
}
else if (!pDocument-> IsModified())
{
// original document is untouched
pDocument-> SetModifiedFlag(bWasModified);
}
else
{
// we corrupted the original document
SetDefaultTitle(pDocument);
if (!pDocument-> OnNewDocument())
{
TRACE(traceAppMsg, 0, "Error: OnNewDocument failed after trying "
"to open a document - trying to continue.\n ");
// assume we can continue
}
}
return NULL; // open failed
}
pDocument-> SetPathName(lpszPathName);
}
CWinThread* pThread = AfxGetThread();
ASSERT(pThread);
if (bCreated && pThread-> m_pMainWnd == NULL)
{
// set as main frame (InitialUpdateFrame will show the window)
pThread-> m_pMainWnd = pFrame;
}
InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
return pDocument;
}
[解决办法]
生成App全局的时候不就生成了,
[解决办法]
在构造函数里和初始化设断点调试就知道了。
[解决办法]
BOOL CxxxApp::InitInstance()->
if (!ProcessShellCommand(cmdInfo))-> 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();
............
}
->
BOOL CCmdTarget::OnCmdMsg(UINT nID, int nCode, void* pExtra,AFX_CMDHANDLERINFO* pHandlerInfo)->
_AfxDispatchCmdMsg(this, nID, nCode,lpEntry-> pfn, pExtra, lpEntry
-> nSig,pHandlerInfo)-> (pTarget-> *mmf.pfn_COMMAND)()->
void CWinApp::OnFileNew()->
m_pDocManager-> OnFileNew()
-> pTemplate-> OpenDocumentFile(NULL)
->
pFrame = CreateNewFrame(pDocument, NULL)->
CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass-> CreateObject()
-> if (!pFrame-> LoadFrame(m_nIDResource,WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, // default frame stylesNULL, &context))->
if (!Create(lpszClass, lpszTitle, dwDefaultStyle, rectDefault,
pParentWnd, MAKEINTRESOURCE(nIDResource), 0L, pContext))
-> 是到的意思。
基本就是这么跟踪的
[解决办法]
CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass-> CreateObject(),创建主窗口的MFC对象,if (!Create(.......))把主窗口Windows对象创建出来。
[解决办法]
BOOL void InitInstance();执行结束后,主窗口就创建好了,但是你必须ShowWindow()才能让主窗口可见,所以要m_nMainWindows-> ShowWindow(m_nCmdShow);
楼住根据我给的顺序好好跟踪以下,不过越是跟踪越是觉得糊涂,因为要掌握东东太多了,这是个艰苦过程.加油啊
[解决办法]
不知道对不对
构造theApp,创建对象
执行Winmain函数
执行IniInstance()
构造文档类对象
构造主框架窗口类
PerCreateWindow()//注册主框架窗口
OnCreate() //创建主框架窗口(菜单栏船物化)
构造视图类对象
ShowWindow()
UpdateWindos() //窗口出现
@CTestView::OnDraw()
return TURE
Run()
(转到@处,进行消息循环)