Click here to Skip to main content
12,302,863 members (72,609 online)
Click here to Skip to main content
Add your own
alternative version

Stats

98.5K views
3.4K downloads
38 bookmarked
Posted

Adding a Recent File List to an MFC Dialog Based Application

, 4 Sep 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to add a recent file list to a dialog based application

Sample Image

Introduction

I am often writing simple utility applications for helping make life easier while programming. When I first started programming DOS applications, I often wrote simple command line utilities to do some mundane tasks. When moving to the Windows world, I continued to write the simple utilities as command line programs until I started using MFC. MFC dialog based applications gave me what I thought was the DOS command line utility of the Windows world.

The one thing missing from MFC dialog based applications that I enjoyed using in many Windows applications was the use of the recent file list.

It is actually not too difficult to add the recent file list capabilities to the MFC dialog based application. The first thing to do is add your menu to the dialog. Make sure you give it the IDR_MAINFRAME id. This is one of those special ids that MFC uses internally to do certain actions such as the recent file list. After you add your menu, add an entry to that menu which contains the id ID_FILE_MRU_FILE1. After you have added the menu, you will need to modify your applications CWinApp::InitInstance call. You will need to add two calls:

SetRegistryKey(_T("CodeProject Recent File List Dialog Demo"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)

The SetRegistryKey will setup a location in the Windows registry database to store your applications recent file list entry. The LoadStdProfileSettings will load the standard INI file options which includes your applications most recent file list. You will also need to override the CWinApp::OpenDocumentFile function. This function is called when you select a file from your recent file list. You will notice in the sample application that the function returns 1 instead of a CDocument*. This can be done because the calling function CWinApp::OnOpenRecentFile does not use the return value. The last thing that needs to be done is to cause the menu to display your recent file list in the menu. This is done by overriding the WM_INITMENUPOPUP in your dialog box class. Inside the OnInitMenuPopup function, you will force the calling of the OnUpdate handlers for the menus. In particular, we need to call the OnUpdate handler CWinApp::OnUpdateRecentFileMenu for the recent file list menu item. When setting up the CCmdUI for calling the OnUpdateRecentFileMenu, we need to be sure to set the m_nIndexMax value, otherwise you will assert when there are no recent files in the registry.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PPresedo
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionAdding a Recent File List Pin
Member 28764391-Apr-12 8:23
memberMember 28764391-Apr-12 8:23 
QuestionSystem.AccessViolationException Pin
cschraer17-Feb-12 9:39
membercschraer17-Feb-12 9:39 
GeneralThank you! Pin
Chris Hills3-Dec-07 18:18
memberChris Hills3-Dec-07 18:18 
Questionhow to create menus Pin
y_naga_malleswararao14-Mar-06 2:36
membery_naga_malleswararao14-Mar-06 2:36 
Questionhaving a problem compiling your program Pin
ghulam-e-sirsabz1-Mar-06 7:09
memberghulam-e-sirsabz1-Mar-06 7:09 
AnswerRe: having a problem compiling your program Pin
shengcheng_jin7-Jun-07 15:42
membershengcheng_jin7-Jun-07 15:42 
GeneralProblem with dialog menu Pin
one_eddie27-Dec-04 2:55
memberone_eddie27-Dec-04 2:55 
QuestionHow do you "add a menu" to you Dialog based app? Pin
albean9-Apr-02 11:10
memberalbean9-Apr-02 11:10 
AnswerRe: How do you "add a menu" to you Dialog based app? Pin
Chris Losinger9-Apr-02 11:16
memberChris Losinger9-Apr-02 11:16 
GeneralRe: How do you "add a menu" to you Dialog based app? Pin
jefflewis24-Nov-05 10:05
memberjefflewis24-Nov-05 10:05 
GeneralRe: How do you "add a menu" to you Dialog based app? Pin
rems8114-Feb-06 18:36
memberrems8114-Feb-06 18:36 
Generalhello Pin
Anonymous7-Jan-01 10:14
memberAnonymous7-Jan-01 10:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 5 Sep 2001
Article Copyright 2001 by PPresedo
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid