Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


BOOL CExerciseApp::InitInstance()
{
#ifdef _AFXDLL
Enable3dControls( );
#else
Enable3dControlsStatic();
#endif
LoadStdProfileSettings(6);

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CExerciseDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CExerciseView));
AddDocTemplate(pDocTemplate);

CCommandLineInfo cmdInfo;

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;

m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();

return TRUE;
}


  1. Test the application and click the various menu items


5.3.2 SDI Improvements: The Document..................................................


The document is actually the object that holds the contents of a file. Based on this role, it
is its responsibility to validate the creation of a new file or to store a file that is being
saved. To perform these tasks and others, the CDocument class provides various methods
you can conveniently add to your application or add and customize their behavior.

Earlier, we saw that, to give the user a convenient means of creating a new document,
you can add an ID_FILE_NEW menu identifier and connect it to your application class in
the InitInstance() method. Clicking this menu item only allows to initiate the action, the
document that is the base of file contents must be aware and validate this action. When a
user decides to create a new document or when the application opens and is about to
create a new document, you may want to make sure that there is no existing document or
you may want to delete the existing one. To take care of this, the CDocument class
provides the virtual OnNewDocument() method. Its syntax is:

virtual BOOL OnNewDocument();

When a new file is about to be created, this method is called to initiate it. If everything
goes fine and the file is created, the OnNewDocument() method returns TRUE. If the file
cannot be initialized, this method returns FALSE or 0. This method, which really behaves
like an event, does not create a new file. It is launched when a new file is going to be
created and allows you to make it happen or to prevent the creation of a new file.
Free download pdf