How to simulate "Word" window form
Key: Make an article on cchildframe.
1. To generate a multi-document application TestMDi, build the following classes:
CAboutDlg, CChildFrame, CMainFrame, CTestMDIApp, CTestMDIDoc, CTestMDIView2, transformation CTestMDIApp :: InitInstance (): pMainFrame-> ShowWindow (SW_HIDE); 3, the transformation CChildFrame: First, replace it with a base class CFrameWnd, then in CMainFrame now create toolbars The status bar related code is moved to CChildFrame. In order to highlight the focus,
I only list the changed code. As follows: //ChildFrm.hclass 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.cppimplement_dyncreate (CCHildFrame, CFrameWnd)
Begin_MESSAGE_MAP (CCHildFrame, CFrameWnd // {AFX_MSG_MAP (CCHildFrame) ON_WM_CREATE () ON_WM_CLOSE () //}} AFX_MSG_MAPEND_MESSAGE_MAP ()
Static uint indeicators [] = {id_separator, // status line indeicator id_indicator_caps, id_indicator_num, id_indicator_scrl,};
Bool Cchildframe :: PrecreateWindow (CreateStruct & Cs) {if (! Cframewnd :: PrecreateWindow (CS)) Return False;
return TRUE;} /// CChildFrame message handlersint 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 (INDCATORS) / SIZEOF (TRACE0 ("Failed to Create Status Bar / N); Return-1; // Fail TO CREATE}
// TODO: Delete these three lines if you do not 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);}
Ok, it should be compiled now. What is it, good? However, you will soon discover how my program is not closed? Yes, the main window is hidden, yet is not closed. It turns out that the main window should also be in the case where all windows are closed.
is closed. So I chose a time cchildframe :: onClose (), send a message to the main window, so that the main window detects whether there is a cchildframe window:
void CChildFrame :: OnClose () {CFrameWnd :: OnClose (); AfxGetMainWnd () -> PostMessage (WM_COMMAND, ID_CLOSE_CHILD, 0); // ID_CLOSE_CHILD} are respective custom message, CMainFrame process the message has a corresponding function: BOOL CMainFrame: : OnCommand (WPARAM WPARAM, LPARAM LPARAM) {IF (wparam == id_close_child) {checkchildwnd (); return true;} return cmdiframewnd :: oncommand (wparam, lparam);
void CMainFrame :: CheckChildWnd () {CWinApp * pApp = AfxGetApp (); POSITION ps = pApp-> GetFirstDocTemplatePosition (); ASSERT (ps = NULL!); CDocTemplate * pDocTemplate = pApp-> GetNextDocTemplate (ps); ps = pDocTemplate- > GetFirstDocPosition (); if (ps == null) PostMessage (WM_Close);
Basic problems resolved.
What is the benefit of doing this? Since multiple views use a toolbar and status bar, it is very painful when the status bar and toolbar is updated during view switching. Especially different views have different status columns.
bitter. In the way such a single document interface, this step is expected. Of course, there should be its deficiencies, I hope all the letter tells, and I hope that you can tell the questions or bugs it involve, and let usually let us know.
Lan Zhengpeng zlan@corp.netese.com