Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


When using an application over and over, sometimes a user may want to open the last
accessed document or at least see a list of the last documents opened on an application.
To provide this functionality, create a menu item called ID_FILE_MRU_FILE1 and set
its prompt to a string such as Recent File. This menu item is usually added to the File
menu above the Exit or quit. The actual list of recent files is stored in an INI file that
accompanies your application. To make this list available, you must call the
LoadStdProfileSettings() method of the CWinApp class in your InitInstance() method.
The syntax of this method is:

void LoadStdProfileSettings(UINT nMaxMRU = _AFX_MRU_COUNT);

By default, this allows the list to display up to four names of documents. This method
takes one argument as the number of document names to be displayed in the list. If you
do not want the default of 4, specify the nMaxMRU value to your liking.

Practical Learning: Improving the Application



  1. To provide new functionality to the application, in the Resource View, change the
    IDentifier of the Exit menu item to ID_APP_EXIT and set its Prompt to Quit the
    application

  2. Add the following menu item under File:


Caption ID Prompt
&New\tCtrl+N ID_FILE_NEW Create a new document
&Open...\tCtrl+O ID_FILE_OPEN Open an existing document


  • P&rint Setup... ID_FILE_PRINT_SETUP Change the printer and printing
    options


  • Recent file ID_FILE_MRU_FILE1 Open this file




  • E&xit ID_APP_EXIT Quit the application; prompt the
    save document





  1. To allow the application to treat documents, change the InitInstance()
    implementation as follows:


BEGIN_MESSAGE_MAP(CExerciseApp, CWinApp)
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
Free download pdf