Click here to Skip to main content
13,455,132 members (52,894 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked
Posted 23 Jun 2003

XFileDialog - Customizing CFileDialog

, 23 Jun 2003
Rate this:
Please Sign up or sign in to vote.
XFileDialog demonstrates how to customize CFileDialog by adding a MRU combobox for folders.


When new common file dialogs were first introduced, I immediately liked the way they looked. But I also noticed that drop-down list of drives and directories seemed a bit awkward, especially if you wanted to flip between two directories on different drives. When we started hearing same thing from our customers, I knew we should try to make it easier to pick a folder.

Implementation notes

My initial thought was, OK, CFileDialog is a common dialog just like CFontDialog, all I have to do is copy the dialog template and insert my own controls. It turns out that customizing CFileDialog is completely different than customizing CFontDialog. (My article XFontDialog - Customizing CFontDialog Part I: Adding Font Filters explains how to customize CFontDialog). I found a reasonably complete description of what was going on in the MSDN article Open and Save As Dialog Boxes. This article explains that, to add controls to the CFileDialog dialog, what you do is create a dialog template that contains only the controls that you want to add. IDD_XFILEDIALOG is defined as:

FONT 8, "MS Sans Serif"
    LTEXT           "Recent folders:",IDC_MRU_CAPTION,13,3,51,8,
                    SS_CENTERIMAGE | NOT WS_GROUP
    COMBOBOX        IDC_MRU_COMBO,85,1,260,102,CBS_DROPDOWN |

Note the styles of this dialog - they are necessary for proper integration with CFileDialog. Once you have this dialog defined, you can add it to CFileDialog in ctor of your derived class:

SetTemplate(0, _T("IDD_XFILEDIALOG"));

I set up dialog template and its rc file according to guidelines I described in my article XDialogImport - How to share dialogs between projects.

Once I had dialog template, I then started to unravel secrets of how to integrate with CFileDialog. As I said before, it is completely different than CFontDialog. Whereas with CFontDialog all controls are on one dialog, with a customized CFileDialog you have two levels of dialogs: the new IDD_XFILEDIALOG actually is a child of original CFileDialog. This is alluded to in MSDN article, but I did not fully appreciate what this meant, until I saw it using one of my favorite tools, HPS HwndSpy:


As you see in the screenshot, IDD_XFILEDIALOG is highlighted in window tree, and it is child of "Open" dialog. What this means in terms of implementation of CXFileDialog is that you can access all controls on IDD_XFILEDIALOG just like you would on any other dialog - by using GetDlgItem() or by using DDX_Control. However, all controls of CFileDialog are sibling to IDD_XFILEDIALOG - to access them, you must use GetParent()->GetDlgItem(xxx). Once I understood this, rest of the code was easy to implement - well, most of it was easy. There were still a few more things that bit me.

Before getting into more details, let me show you what I was attempting to do. I wanted to add a history combo box for most-recently used folders, so that every time user clicked Open, the current folder would be added to history list. Every time CXFileDialog was opened, history list items would be restored to combo box (if they were still valid directories). I decided to use Paul Vickery's excellent History ComboBox to take care of loading and saving folder items, with a modification I made to allow checking folders for validity. Here is what XFileDialog looks like:


Of course there were usual details to take care of - like positioning and sizing IDD_XFILEDIALOG controls in the OnSize() handler. But when I tested CXFileDialog with pre-Win2K layout, I discovered that "File Name" control ID actually had two different values - one (1152) for when it was acting like an edit box (pre-Win2K), and another value (1148) for when it was acting like a combo box (Win2K and later). Furthermore, File Name control was also affected by a registry setting, which can be used to enable/disable the MRU property of the File Name control. How did I figure this out? Again, I used HPS HwndSpy:


The next thing that I tested was Read-Only checkbox. Naturally, when I enabled it, the checkbox positioned itself right on top of my Recent Folders control, since I had forgotten to account for checkbox in OnSize().

Finally, I tested pre-Win2K layout. There was not enough room for the static label "Recent folders", so for pre-Win2K layout, I moved all combo boxes to the right. Here is the new pre-Win2K layout:


CXFileDialog Features

Here are features in new CXFileDialog:

  • Recent folders MRU combo box - adds a combo box with list of most-recently used folders. Selecting a folder from the list will change the file display to that folder.
  • SetTitle() - Provides a convenient way to set title of dialog.
  • SetOsVersion() - Allows you to choose between old-style and Explorer-style dialog.
  • GetPath() - Gets path of selected file.

How to use

To integrate CXFileDialog into your app, you first need to add following files to your project:

  • XFileDialog.cpp
  • XFileDialog.h
  • XFileDialog.rc
  • XFileDialogRes.h
  • XHistoryCombo.cpp
  • XHistoryCombo.h

You also need to add XFileDialog.rc to project rc file - go to View | Resource Includes... and in the bottom listbox, scroll down to the end. Insert #include "XFileDialog.rc" right before the #endif:


Next, include header file XFileDialog.h in appropriate project files. Now you are ready to start using CXFileDialog.

Demo App

The XFileDialogTest.exe demo shows how to use CXFileDialog:



Revision history

  • Version 1.1 - 2003 June 25
    • Fixed some focus bugs;
    • Added check for directory existence.
  • Version 1.0 - 2003 June 24
    • Initial public release.


This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


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


About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
wa4-Feb-11 2:46
memberwa4-Feb-11 2:46 
QuestionExplorer Style Missing? Pin
Kyudos29-Oct-09 16:52
memberKyudos29-Oct-09 16:52 
AnswerRe: Explorer Style Missing? Pin
Hans Dietrich1-Nov-09 16:21
mvpHans Dietrich1-Nov-09 16:21 
GeneralRe: Explorer Style Missing? Pin
Kyudos3-Nov-09 15:57
memberKyudos3-Nov-09 15:57 
QuestionXFileOpen - any way to remember last View and Size of common dialog? Pin
matt200014-Aug-09 19:02
membermatt200014-Aug-09 19:02 
QuestionCFileDialog - store data Pin
vijayashree30-Apr-09 23:23
membervijayashree30-Apr-09 23:23 
GeneralFails on VS2008 and Vista - How To Resolve. Pin
Neville Franks9-Mar-09 14:09
memberNeville Franks9-Mar-09 14:09 
GeneralRe: Fails on VS2008 and Vista - How To Resolve. Pin
Hans Dietrich9-Mar-09 22:43
mvpHans Dietrich9-Mar-09 22:43 
GeneralI'm implementing something similar but OnSize and other events not firing [modified] Pin
dpackage10-Feb-09 4:49
memberdpackage10-Feb-09 4:49 
Generaldemo project does not work under visual studio 2005 [modified] Pin
effem7-Jan-07 6:56
membereffem7-Jan-07 6:56 
General[Message Removed] Pin
nompel6-Oct-08 9:17
membernompel6-Oct-08 9:17 
GeneralSetting the Initial Directory - code update Pin
Neville Franks23-Nov-06 14:56
memberNeville Franks23-Nov-06 14:56 
GeneralOnFileNameOK() warning - return value opposite!!! Pin
BaliDawg3-Aug-06 8:18
memberBaliDawg3-Aug-06 8:18 
GeneralAdding extra controls on CFileDialog Pin
Kiriko18-May-06 22:18
memberKiriko18-May-06 22:18 
GeneralRestore size, view, and column widths Pin
matt200010-May-06 6:10
membermatt200010-May-06 6:10 
General2 question about the project Pin
zhou_wz16-Feb-06 20:12
memberzhou_wz16-Feb-06 20:12 
QuestionAdding custom folder buttons Pin
matt200015-Feb-06 9:31
membermatt200015-Feb-06 9:31 
AnswerRe: Adding custom folder buttons Pin
Hans Dietrich21-Feb-06 21:22
memberHans Dietrich21-Feb-06 21:22 
QuestionTrying to catch OnSelChange of the FileNameCombo? Pin
Ralph27-Oct-05 0:46
memberRalph27-Oct-05 0:46 
QuestionHow to catch the Focus? Pin
taphan17-Oct-05 21:40
membertaphan17-Oct-05 21:40 
Questionoverwriting existing files Pin
gabriel92726-Aug-05 5:13
membergabriel92726-Aug-05 5:13 
Questionoverwriting existing files Pin
gabriel92726-Aug-05 5:13
membergabriel92726-Aug-05 5:13 
Generalwonderful article! Thank you! Pin
lucy13-Jun-05 12:16
memberlucy13-Jun-05 12:16 
GeneralEmbedding File Dialog In a Dialog Pin
Mihir Gore11-Dec-03 17:59
memberMihir Gore11-Dec-03 17:59 
GeneralRe: Embedding File Dialog In a Dialog Pin
CoderPond4-Jan-05 16:12
memberCoderPond4-Jan-05 16:12 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180321.1 | Last Updated 24 Jun 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid