Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 5: The Document/View Architecture Visual C++ and MFC Fundamentals


the user cannot use it as a document. Still, the main frame allows the user to either create
a new document or open an existing one.

To manage the differentiation between the parent and its children, the application uses
two different menus: one for the main frame and one for a (or each) child. Both menus
use the same menu bar, meaning that the application cannot display two frame menus at
one time. It displays either the parent menu or a (the) child menu. When at least one
document is displaying, the menu applies to the document. If no document is displaying,
the main frame is equipped with a simplified menu with limited but appropriate
operations. The user can only create a new document, only open an existing document, or
simply close the application.

5.4.2 Creating a Multiple Document Interface.........................................


We have mentioned that an MDI is an application made of a parent frame and at least one
child. Therefore, to create an MDI, because it requires a frame of its own, you should first
create a series of resource objects that share a common name as IDR_MAINFRAME. A
basic application should have an icon and a menu. The icon can be designed any way you
like and you are recommended to create one made of a 32x32 and a 16x16 versions. The
menu, since it will be used only when no document is available, should provide
functionality that does not process a document. It should allow the user to create a new
document, to open an existing document, and to quit the application. Such a menu can
have the following items:

As done since Lesson 3 (when we studied resources), these two resources are sufficient
to create an application since you can call the CFrameWnd::LoadFrame() method. To
create a frame for a Multiple Document Interface, you must derive your frame class from
CMDIFrameWnd:

BOOL CExercise1App::InitInstance()
{
// Create a frame for the window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;

CCommandLineInfo cmdInfo;

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
Free download pdf