兰征鹏
zlan@corp.netease.com
重点:在CChildFrame上做文章。
1、利用向导生成一个多文档应用
程序 TestMDI,则生成下列类:
CAboutDlg,CChildFrame,CMainFrame,CTestMDIApp,CTestMDIDoc,CTestMDIView
2、改造CTestMDIApp::InitInstance():pMainFrame->ShowWindow(SW_HIDE);
3、改造CChildFrame:首先用CFrameWnd替换它的基类,再将CMainFrame里跟创建工具栏状态栏相关代码移到CChildFrame里面。为了突出重点,
我只列出更改过的代码。如下:
//ChildFrm.h
class CChildFrame : public CFrameWnd
{
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//{{AFX_MSG(CChildFrame)
afx_msg void OnClose();
//}}AFX_MSG
};
//ChildFrm.cpp
IMPLEMENT_DYNCREATE(CChildFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CChildFrame, CFrameWnd)
//{{AFX_MSG_MAP(CChildFrame)
ON_WM_CREATE()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CChildFrame message handlers
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
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;
}
void CChildFrame::OnClose()
{
CFrameWnd::OnClose();
// AfxGetMainWnd()->PostMessage(WM_COMMAND,ID_CLOSE_CHILD,0);
}
好了,现在应该可以编译通过了。怎样,不错吧?
可是,你很快就发现,怎么我的
程序没有关闭?是啊,主窗口被藏起来了,还没有被关闭。原来,在所有窗口都关闭的情况下,主窗口也应该
被关闭。于是,我选择了个时机CChildFrame::OnClose(),向主窗口发送一个消息,让主窗口自己检测是否还存在有CChildFrame窗口:
void CChildFrame::OnClose()
{
CFrameWnd::OnClose();
AfxGetMainWnd()->PostMessage(WM_COMMAND,ID_CLOSE_CHILD,0); //ID_CLOSE_CHILD是自定义消息
}
相应,CMainFrame有函数对应处理这个消息:
BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
if(wParam == ID_CLOSE_CHIL