小弟在学习一本书上的VC++例子时遇到一问题:
为了标记 MFC应用程序的启动顺序,我利用AppWizard创建了一个单文挡的项目Hhello;
在对程序中的有关位置添加消息框输出语句,从而验证MFC应用程序的初始化流程;
。。
。。
。。
4:
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
///////
AfxMessageBox("No.4:CMainFrame::CMainFrame().");
///////
}5:
BOOL CMainFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class ////
AfxMessageBox("No.5:CMainFrame::Create().");
////
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
6:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{ AfxMessageBox("No.6:CMainFrame::PreCreateWindow()."); if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
} 这本VC++书上说的顺序是 4,5,6依次弹出对话框;
编译成功!
问题是:
1:在5的位置函数不执行;为什么?(利用ClassWizard将Create该函数加入以便重载) 2:在6地位置函数执行两次 为什么? 多谢各位!!
为了标记 MFC应用程序的启动顺序,我利用AppWizard创建了一个单文挡的项目Hhello;
在对程序中的有关位置添加消息框输出语句,从而验证MFC应用程序的初始化流程;
。。
。。
。。
4:
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
///////
AfxMessageBox("No.4:CMainFrame::CMainFrame().");
///////
}5:
BOOL CMainFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class ////
AfxMessageBox("No.5:CMainFrame::Create().");
////
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
6:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{ AfxMessageBox("No.6:CMainFrame::PreCreateWindow()."); if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
} 这本VC++书上说的顺序是 4,5,6依次弹出对话框;
编译成功!
问题是:
1:在5的位置函数不执行;为什么?(利用ClassWizard将Create该函数加入以便重载) 2:在6地位置函数执行两次 为什么? 多谢各位!!
5位置的函数当然不执行嘛!!是不是 OnCreate 啊??再仔细看看书和你的代码。
OnCreate 在7的位置:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
AfxMessageBox("No.7:CMainFrame::OnCreate()."); if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
} if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
} // TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar); return 0;
}
执行顺序:1,2,3,4,()6,6,7,8,9,10,11,12,13,14,15,16 OnCreate 在7的位置正确执行;
在app的类里面,是执行的LoadFrame 函数的,不是用Create()函数的,这两个函数的调用可以看看MSDN的说明
它们的作用一样,不过LoadFrame 是利用资源来创建CFrame的,而Create函数就是根据参数来生成。