MFC TN005--Multiple Document Interface (MDI)

Created: April 15, 1992

ABSTRACT

This technical note describes the MFC routines for supporting the multiple document interface (MDI) of Windows.

The Microsoft Foundation Class (MFC) library provides a full-featured set of C++ object classes for the MicrosoftÒ WindowsÔ graphical environment. It includes classes that directly support application development for Windows as well as general-purpose classes for collections, files, persistent storage, exceptions, diagnostics, memory management, strings, and time. Each MFC technical note describes a feature of MFC using code fragments and examples.

THE PROBLEM

Creating an MDI frame window traditionally requires quite a bit of boilerplate code. To simplify writing an MDI application, MFC provides much of this boilerplate code. Therefore, the steps required to create an MDI frame window are different from those in traditional programming for the MicrosoftÒ WindowsÔ graphical environment.

CREATING AN MDI FRAME WINDOW

1.Define a new class derived from CMDIFrameWnd.

2.In the derived class constructor, invoke (for example):

Create(NULL, pTitle, WS_OVERLAPPEDWINDOW, rectDefault, NULL,

pMenuName);

Note:

See MFC sample programs (SAMPLE\MINMDI, SAMPLE\MDI, SAMPLE\MULTIPAD) for complete working examples of how to call CMDIFrameWnd::Create.

The MDICLIENT window (the area in which MDI children appear) is created by CMDIFrameWnd::CreateClient, which is called from the default WM_CREATE handler for CMDIFrameWnd.

When an MDICLIENT window is created, it must be told which menu should contain the list of open MDI child windows and where the range of IDs for MDI child windows starts.

CMDIFrameWnd::OnCreate assumes that the Window menu is the second from the right end of the menu bar and that MDI child window IDs start at AFX_IDM_FIRST_MDICHILD (which is defined as 0xff00; Windows and MFC reserve IDs between 0xf000 and 0xffff for system use).

If the default menu position is not satisfactory, override OnCreate and call the CreateClient function.

CREATING AN MDI CHILD WINDOW

1.Define a new class derived from CMDIFrameWnd.

2.In the derived class constructor, invoke (for example):

Create(NULL, pTitle, 0, rectDefault, pMDIFrameWnd)

COMMAND ACCELERATORS

The default PreTranslateMessage functions handle manually loaded accelerator tables for both MDI child windows and the MDI frame as well as the standard MDI system-command accelerators normally handled by TranslateMDISysAccel.

If the active MDI child window has an accelerator table attached to it, that table is searched first when a key is pressed. If a key is not found in the child’s table, the MDI frame window is checked. If the key is not found in the frame’s table or the frame does not have a table, the key is passed to TranslateMDISysAccel. If that function translates the key, key processing stops. Otherwise, the key is sent to the window with the focus through OnKeyDown, OnSysKeyDown, OnChar, or OnSysChar.

OTHER FUNCTIONS

MDI Frame Windows

Function Description

GetChildFrame Returns the active MDI child window or this if there are no children.
GetParentFrame Returns this.
MDIActivate Activates a particular MDI child.
MDICascade Arranges the MDI children so they overlap.
MDIGetActive Returns the active MDI child window or NULL if there are no children.
MDIIconArrange Arranges the MDI child icons.
MDIMaximize Maximizes a particular MDI child window.
MDINext Activates a different MDI child window.
MDIRestore Restores a particular MDI child window.
MDISetMenu Changes the menu bar.
MDITile Arranges the MDI children so they don’t overlap.

MDI Child Windows

Function Description

GetChildFrame Returns this.
GetParentFrame Returns the parent MDI frame window.
MDIDestroy Destroys this MDI child window.
MDIActivate Activates this MDI child window.
MDIMaximize Maximizes this MDI child window.
MDIRestore Restores this MDI child window.