Introduction

For developers who use MFC, this is a freeware DDX routine which subclasses an edit control to allow you to easily specify a directory (also known as a folder in Windows 95 parlance). When the edit control is correctly setup, it will look like:

The button beside the edit control sports a tooltip when the button is over it and when the button is hit, the following dialog is displayed:

The caption of the dialog can be customized through the DDX routine. Another flag allows the developer to customize the dialog so that it uses the older style common dialog which looks like:

The routine is most useful when you want to specify a folder from somewhere in the user interface such as specifying a directory where a bunch of output files will be placed.

To use DDXFolder in your project, simply include ddxfolder.cpp, dlgdir.cpp, the 3 string resources and the dialog resource IDD_FILEOPEN_OLD from the test application in your application. Then #include ddxfolder.h in whichever of your modules requires it and make the appropriate calls. Included is a simple dialog based application which shows how to use it. Basically all you need to do is add to your DoDataExchange function as follows:

Used internally by ddxgetfolder() is another class which you may find helpful. Its name is CChooseDirDlg which implements a standalone directory picker dialog which can be used independently of the DDX/DDV functions.

v1.2 (16th September 1998)

Updated all the documentation to be HTML based as with my other shareware / freeware.

Unicode enabled all the code and provision of Unicode build configurations.

VC 5 workspace files provided now as standard.

Provision of a DDV function.

General tidy up of the sample app, including removing all the AppWizard generated comments.

All code now compiles cleanly at warning level 4.

Replaced all TRACE0 calls with TRACE.

Changed name of main function from DDX_GetFolder() to DDX_FolderControl().

Module name has been changed from ddxgetfolder to ddxfolder.

Sample app now allows read only state of widget to be toggled.

Addition of a DDX_FolderValue() function.

Changed the IDs of the strings in the resource table which the code uses.

v1.21 (17th September 1998)

Minor update to some code comments and the online documentation.

v1.22 (30th September 1998)

Fixed a compiler problem in the .rc file. I was accidentally shipping the wrong resource.h file.

7th February 1999

Minor bug fix in choosedirdlg.cpp.

v1.23 (3rd April 1999)

Fixed another resource compiler problem.

Updated the copyright messages.

General code tidy up.

v1.24 (11th December 1999)

Cosmetic item where now a 1 pixel space is included between the edit control and the "..." button.

pDX -- The usual CDataExchange object which will be passed into your DoDataExchange function.

rCGetFolderControl -- The folder control to get or set the directory for.

dwFlags -- The flags to use to perform validation. Currently only GFLDR_FOLDER_MUST_EXIST is supported which requires you to enter a valid directory / folder. Normally you would use this flag except in the case where creating the directory may be required such as in an install program.

Remarks:

Associates an existing edit control with dialog ID nIDC to a compound directory picker control. Normally you would put calls to this function in your dialog class' DoDataExchange member function.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

This is very old code but still works great in the year 2013.I used this in a couple projects and i now wanted a way to adda button so people can make a new folder if needed into the folder picker UI.Google searched it and result #1 has the answer..A user at CodeGuru named mournerslament had the perfect answer

Any way all i had to do was add a flag to the BROWSEINFO StructOpen ChooseDirDlg.cpp and navigate to the DoModal() function.

Find the following line..

bi.ulFlags = m_dwFlags;

And then add the following below that line..

bi.ulFlags += BIF_USENEWUI;

I tested this on Windows 7 x64 and it seems to work really well What i was trying to do with that is leave the code alone but add to it..So if that m_dwFlags variable has existing flags in it (i never checked)then what i hoped is that the += operator would add / append the Flag i wanted.

I am using an edit control on my Main Dialog to select a directory to save files to (just like in your sample). I threw together a neat way to drop folders into the edit box as an easy way to get the folder name into the control.

I went into ClassWizard and added a new class based on CEdit (CEditLocalPath). Inside CEditLocalPath I created this function.

I then added this code to the OnInitDialog section of my Main Dialog cpp (the one that holds the Edit Control) :

EditLocalPath.SubclassDlgItem(IDC_LOCAL_PATH,this);

The problem is that when I add your code everything compiles just fine, but when I try to run the program I get an Assert in Wincore.cpp - line 312. Here is the wincore function that fails (line 312 is the first Assert) (BTW - in the debug window this = {CEditLocalPath hWnd = 0x000000}):

If I rem out 'EditLocalPath.SubclassDlgItem(IDC_LOCAL_PATH,this);' then your routine works just fine. I have been going crazy trying to make your code work before I realized the error was inside the CEditLocalPAth class. Removing the subclass fixed everything, but there has to be a way to chain the subclasses together?

in CTestgetfolderDlg::OnRequirevalid(), the compiler gives a warning message for dwFlags wich is intialized but unused.Did you make a mistake and the m_dwValidationFlags should be used instead ? because this variable is modified by the requirevalidation condition : if (bRequireValid) m_dwValidationFlags |= GFLDR_FOLDER_MUST_EXIST; else m_dwValidationFlags &= ~GFLDR_FOLDER_MUST_EXIST;

Do you confirm the m_dwValidationFlags = m_ctrlGetFolder.GetFlags();in place ofDWORD dwFlags = m_ctrlGetFolder.GetFlags();

J've made two improvements in AddEditButton function for :- Setting tabstop to button and correct order in dialog items- Use the string text of button to calculate the width of the created item (but limit it to the dialog size)