Download demo project - 21 KbDownload source files - 8 Kb
Intro
MFC implements a convenient feature called a most recently used list, or MRU. In SDI and MDI apps generated
by AppWizard, the MRU list appears on the File menu. However, another handy place where MRUs are used is in comboboxes.
For example, the Start/Run dialog lists the last few programs that you've run in its combobox.
While MFC does not directly support placing an MRU in a combobox, the class CRecentFileList (which implements
the File menu MRU list) provides several member functions for maintining, saving, and loading an MRU list. My CMRUComboBox
class encapsulates this functionality, and adds the logic necessary to put the MRU in a combobox.
CMRUComboBox was written with MSVC 5.0 and MFC 4.2. I have tested it on Win 98 and NT 4 (ANSI and Unicode builds).
Using an MRU combobox
To use CMRUComboBox, create a combobox control with the CBS_DROPDOWN style. Including CBS_AUTOHSCROLL also is
not a bad idea, since without this style, the edit portion of the combobox will not scroll, effectively limiting
the amount of text that can be typed in the edit box.. Use ClassWizard to add a member variable of type CComboBox
that is hooked to the combobox control. Then, go to the source and change the type of the variable to CMRUComboBox.
CMRUComboBox derives from CComboBox so all the operations you normally do with CComboBoxes will still work the
same; CMRUComboBox just adds new methods for managing the MRU list.
In your OnInitDialog() (or other initialization code), you will need to set up a few parameters and flags for
the MRU list. At minimum, set the maximum size for the MRU list, and the location in the registry (or INI file)
where the list will be saved. There are also a few flags you can optionally set, which are described in the HTML
documentation that is in the ZIP file that contains the source code.
Once all that initialization is done, call LoadMRU() to read in the saved contents of the MRU list. When you
want to add a string to the list, call AddToMRU(). Duplicate strings are automatically brought to the top of the
list, just as in File menu MRU lists. When you want to save the contents of the MRU, call SaveMRU().
Methods List
This is a list of all the new methods available for maintaining an MRU. The full docs are included in the ZIP
file that contains the source code files.
BOOL AddToMRU ( LPCTSTR szNewItem )
void EmptyMRU()
int SetMaxMRUSize ( int nMaxSize )
int GetMaxMRUSize()
void SetMRURegKey ( LPCTSTR szRegKey )
const CString& GetMRURegKey()
BOOL SetMRUValueFormat ( LPCTSTR szValueFormat )
const CString& GetMRUValueFormat()
BOOL LoadMRU()
BOOL SaveMRU()
void RefreshCtrl()
BOOL SetAutoRefreshAfterAdd ( BOOL bAutoSave )
BOOL SetAutoSaveAfterAdd ( BOOL bAutoSave )
BOOL SetAutoSaveOnDestroy ( BOOL bAutoSave )
Simple example
Here is a sample usage of CMRUComboBox. Suppose you have a dialog similar to the one pictured at the beginning
of this article, which has a place for the user to enter a path to a file, and you want to list the last few filenames
to be quickly available in the MRU list.. Once you've created a CMRUComboBox member variable in your dialog class
as described above, you would do the following in your OnInitDialog() function:
m_combo.SetMRURegKey ( _T("Location Dlg MRU") );
m_combo.SetMRUValueFormat ( _T("File%d") );
m_combo.SetMaxMRUSize ( 12 );
VERIFY ( m_combo.LoadMRU() );
Then, whenever you want to add a string to the MRU, call
m_combo.AddToMRU ( szFilename );
Notes
Unfortunately, because CRecentFileList assumes that all the strings it holds are filenames, the strings added
with AddToMRU() must also be filenames. (If you want to see what happens, trace into AddToMRU() and the MFC functions
that CRecentFileList calls.) If you add a plain word, it will have the current directory tacked on before it.
The solution to this would be to implement a new CRecentFileList work-alike that does not make this assumption.
Michael lives in sunny Mountain View, California. He started programming with an Apple
//e in 4th grade, graduated from
UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at
Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at
VMware.
He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.
Mike was a
VC MVP from 2005 to 2009.