Monday, 4 March 2013

Understanding a Multiple Document Interface

Understanding a Multiple Document Interface


Application
A multiple document interface application also has menus, and it enables the user to
have more than one document open at once. This section presents the code that is
generated when you choose an MDI application with no database or compound document
support, but instead with a toolbar, a status bar, Help, 3D controls, source file
comments, and the MFC library as a shared DLL. As with the SDI application, these are
the defaults after Step 1. The focus here is on what differs from the SDI application in
the previous section.
Five classes have been created for you. For the application FirstMDI, they are
l CAboutDlg, a dialog class for the About dialog box
l CFirstMDIApp, a CWinApp class for the entire application
l CFirstMDIDoc, a document class
l CFirstMDIView, a view class
l CMainFrame, a frame class
The App class header is shown in Listing 1.3.
Listing 1.3 FirstMDI.h - Main Header File for the FirstMDI Application
// FirstMDI.h : main header file for the FIRSTMDI application
//
#if
!defined(AFX_FIRSTMDI_H__CDF38D9E_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)
#define
AFX_FIRSTMDI_H__CDF38D9E_8718_11D0_B02C_0080C81A3AA2__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#ifndef __AFXWIN_H__
#error include `stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CFirstMDIApp:
// See FirstMDI.cpp for the implementation of this class
//
class CFirstMDIApp : public CWinApp
{
public:
CFirstMDIApp();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFirstMDIApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CFirstMDIApp)
afx_msg void OnAppAbout();
// NOTE - The ClassWizard will add and remove member
functions here.
// DO NOT EDIT what you see in these blocks of generated
code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations
immediately
// before the previous line.
#endif
//!defined(AFX_FIRSTMDI_H__CDF38D9E_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)
How does this differ from FirstSDI.h? Only in the classnames. The constructor is also
the same as before. OnAppAbout() is just like the SDI version. How about InitInstance()?
It is in Listing 1.4.
Listing 1.4 CFirstMDIApp::InitInstance()
BOOL CFirstMDIApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and want to reduce the
size
// of your final executable, you should remove from the
following
// the specific initialization routines you don't need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a
shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC
statically
#endif
// Change the registry key under which your settings are stored.
// You should modify this string to be something appropriate,
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options

(including // MRU)
// Register the application's document templates. Document
templates
// serve as the connection between documents, frame windows, and
views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_FIRSTMTYPE,
RUNTIME_CLASS(CFirstMDIDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CFirstMDIView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
}
What's different here? Using WinDiff can help. WinDiff is a tool that comes with Visual
C++ and is reached from the Tools menu. (If WinDiff isn't on your Tools menu, see the
"Tools" section of Appendix C.) Using WinDiff to compare the FirstSDI and FirstMDI
versions of InitInstance() confirms that, other than the classnames, the differences are
l The MDI application sets up a CMultiDocTemplate and the SDI application sets up
a CSingleDocTemplate, as discussed in Chapter 4.
l The MDI application sets up a mainframe window and then shows it; the SDI
application does not.
This shows a major advantage of the Document/View paradigm: It enables an enormous
design decision to affect only a small amount of the code in your project and hides that
decision as much as possible.


No comments:

Post a Comment