Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Multiple Top Level Windows

Rate me:
Please Sign up or sign in to vote.
4.92/5 (10 votes)
8 May 2003 168.5K   3.6K   51   23
Allows an application to have multiple top-level windows.
  • Download demo project - 7 Kb
  • Download source files - 18 Kb
  • Introduction

    MFC, out of the box, supports three interface models, MDI, SDI, and dialog based. What it doesn't have is support for MTLW (ok, so MS probably has a different acronym for it, but what I mean is Multiple Top Level Windows). This is the model popularized by those web browsers we all know and love, Netscape Navigator and Internet Explorer.

    Problem

    The main problem with MTLW and MFC is the main window (aka CWinApp::m_pMainWnd).

    1. MFC expects there to be one.
    2. MFC will close the application as soon as the main window is closed.

    Solution

    So what we need is a way to keep CWinApp::m_pMainWnd pointed at a valid TLW and switch it from one window to another as needed to make sure that the app doesn't shut down except when the last TLW is closed.

    Two classes will help you do this:

    • CMultiTopApp - should be used as the base class for your app object.
    • CTopLevelFrame - should be used as the base class for CMainFrame.
    Derive from these classes and wire up the message maps properly, and that's it. You're app now supports MTLW. Of course you need to have ways of creating new TLWs and probably you want to have a command to close all the windows and shut down your app, so the demo project includes those functions.

    First, a handler for the "New Window" menu item creates new TLWs.

    void CMultiTopSample::OnFileNewwindow() 
    {
    	CMainFrame* pFrame = new CMainFrame;
    
    	// create and load the frame with its resources
    	pFrame->LoadFrame(IDR_MAINFRAME,
    		WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
    		NULL);
    
    	// The one and only window has been initialized,<BR>	// so show and update it.
    	pFrame->ShowWindow(SW_SHOW);
    	pFrame->UpdateWindow();
    }

    Second, unless you want your users to have to close each window individually, you may want to have an "Exit" menu item which does the following:

    void CMultiTopSample::OnAppExit() 
    {
    	CloseAllFrames() ;
    }
    Is this easy, or what!

    How it works (briefly), and a BONUS member function:

    CMultiTopApp has an STL list for keeping track of TLWs...

    list< CTopLevelFrame* > m_listMainFrames ;
    ... and a few functions for manipulating them:

    void AddFrame( CTopLevelFrame* pFrame ) ;         // Adds a TLW to list
    void KillFrame( CTopLevelFrame* pFrame ) ;        // Removes TLW from list
    void ReplaceMainFrame( CTopLevelFrame* pFrame ) ; // Makes a differnt TLW the<BR>                                               // MFC main window if possible

    Plus a special bonus function. I use this to do print preview in YATLW and disable all the regular TLWs while I do so.

    void EnableFrames( BOOL bEnable ); // Enables/Disables all TLWs in list

    CTopLevelFrame uses the CMultiTopApp member functions in response to window creation and destruction messages.

    MESSAGEHandlerAction
    WM_CREATEOnCreate()calls AddFrame() after default processing
    WM_CLOSEOnClose()calls ReplaceMainFrame() before default processing
    WM_DESTROYOnDestroy()calls KillFrame() after default processing

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    NewsVisual Studio 2010 has it builtin Pin
    kiwi_4-Oct-10 11:08
    kiwi_4-Oct-10 11:08 
    GeneralLicense Pin
    BarryHolleran9-Apr-08 0:25
    BarryHolleran9-Apr-08 0:25 
    GeneralAfxMessageBox problem and solution Pin
    KHDev4u21-Apr-05 9:55
    KHDev4u21-Apr-05 9:55 
    GeneralRe: AfxMessageBox problem and solution Pin
    Janos Kerekes24-Oct-13 5:25
    Janos Kerekes24-Oct-13 5:25 
    Generalwhen the MainForm is Minimized Pin
    qiaoyun28-Feb-05 22:55
    qiaoyun28-Feb-05 22:55 
    GeneralGreat ! Pin
    Rodrigo Pinto Pereira de Souza7-Jul-04 9:46
    Rodrigo Pinto Pereira de Souza7-Jul-04 9:46 
    GeneralQuestion Pin
    lequang5-Feb-04 19:34
    lequang5-Feb-04 19:34 
    GeneralSmall extension to this excellent class... Pin
    Brendan Tregear26-Aug-03 19:01
    Brendan Tregear26-Aug-03 19:01 
    GeneralMultiple Top Level Windows in BCB 6 Pin
    bmanjones24-Feb-03 4:31
    bmanjones24-Feb-03 4:31 
    GeneralNote for VC++ 7 (.NET) users Pin
    Tunny9-Feb-03 9:18
    Tunny9-Feb-03 9:18 
    GeneralRe: Note for VC++ 7 (.NET) users Pin
    Brendan Tregear7-Aug-03 16:35
    Brendan Tregear7-Aug-03 16:35 
    GeneralRe: Note for VC++ 7 (.NET) users Pin
    james@tunny.co.uk7-Aug-03 20:09
    sussjames@tunny.co.uk7-Aug-03 20:09 
    GeneralRe: Note for VC++ 7 (.NET) users Pin
    Brendan Tregear11-Aug-03 0:29
    Brendan Tregear11-Aug-03 0:29 
    QuestionHow to deal with Documents Pin
    6-Feb-02 23:34
    suss6-Feb-02 23:34 
    GeneralIsn't the Way "Multiple Top-Level Windows" are done Pin
    Brian Hart7-Jul-00 20:07
    Brian Hart7-Jul-00 20:07 
    GeneralRe: Isn't the Way Pin
    Don Grasberger10-Jul-00 3:49
    sussDon Grasberger10-Jul-00 3:49 
    GeneralRe: Isn't the Way Pin
    Rob Krakora31-Jul-00 16:03
    Rob Krakora31-Jul-00 16:03 
    GeneralRe: Isn't the Way Pin
    Vaclav7-Aug-03 10:12
    Vaclav7-Aug-03 10:12 
    GeneralRe: Isn't the Way Pin
    Giles12-Jul-05 3:57
    Giles12-Jul-05 3:57 
    QuestionHow to avoid > 1 entry in the taskbar? Pin
    Peter Andersson19-May-00 11:09
    Peter Andersson19-May-00 11:09 
    AnswerRe: How to avoid > 1 entry in the taskbar? Pin
    Rob Krakora14-Jun-00 3:18
    Rob Krakora14-Jun-00 3:18 
    GeneralRe: How to avoid > 1 entry in the taskbar? Pin
    Sam C2-Dec-01 8:49
    Sam C2-Dec-01 8:49 
    GeneralRe: How to avoid > 1 entry in the taskbar? Pin
    Mike O'Neill9-May-03 12:48
    Mike O'Neill9-May-03 12:48 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.