A Cleaner MRU List

Apr 12, 2008
This article was written back when Visual Studio 6 was new. As such, I cannot guarantee that the content of this article still applies in modern Windows environments.

The default means of adding a most recently used (MRU) list to a program is through a call to the LoadStdProfileSettings() method. One call to this method takes care of creating the MRU and populating it with stored data. But this method call contains one minor annoyance: a single query to a registry value that may or may not already exist under your program's registry branch. Should the latter scenario be the case (the registry item does not exist), it's parent branch will be created; regardless of whether or not you will ever use it. The branch that gets created is entitled "Settings", which can be a useful place to store your program specific registry values. But suppose you would rather use other branch names and never have the "Settings" heading appear in your program's registry branch? The solution is surprisingly easy. Our first course of action is to remove the call to the LoadStdProfileSettings() method. This call is usually found in your application's InitInstance() method. The simplest way to remove the call is to simply comment it out.

Creating the Most Recently Used List

The next step involves a call to the RecentFileList class constructor. A "hidden" variable exists in your CWinApp derived class (the same class that included the InitInstance() method), and has the name m_pRecentFileList. This variable is simply a pointer to a CRecentFileList object. So we can create a new one with a call like the following (add this code to your InitInstance() method):

CString strSection = "Recent File List";
CString strEntryFormat = "File%d";

m_pRecentFileList = new CRecentFileList(0, strSection, strEntryFormat, 4);

The constructor here takes four parameters: the first is an offset for the numbering in the most recently used list. It's best to just leave this value at 0. Next is a string which points to the name of the section in the registry where the MRU is read and written. In this example, I've used the default "Recent File List" heading. The third parameter given to the constructor points to a format string, which is used in creating the names of the entries stored in the registry. Here, I've used the value "File%d". So, as files get added to the MRU list, they will follow the format of "File0", "File1", etc. The final parameter is the number of files to keep track of. As you can see, I've used a value of 4 in this example. This value must range between 0 and 16 (inclusive), with a value of zero meaning that no MRU list should be kept at all.

Reading the List

One final call is needed to do the actual reading of the stored values. It should look like the following and, again, be placed in the InitInstance() method, right after the code we inserted above:

m_pRecentFileList->ReadList();

Once this call has been made, you will be able to use your MRU list just as in any other program. There is no need to worry about saving the values, as the application will take care of that for you when it closes. Pretty nice!

No comments (yet!)

Leave a Comment

Ignore this field:
Never displayed
Leave this blank:
Optional; will not be indexed
Ignore this field:
Both Markdown and a limited set of HTML tags are supported
Leave this empty: