Click here to Skip to main content
16,011,757 members
Articles / Desktop Programming / MFC
Article

Select Folder dialog with a difference

Rate me:
Please Sign up or sign in to vote.
4.22/5 (14 votes)
24 Aug 2000CPOL 171.4K   1.8K   38   26
The Windows 'Select Folder' dialog with some extra functionality.

Sample Image - SelectFolder.gif

Introduction

The SelectFolder.zip file contains the SelectFolder.exe whose output is shown in the above picture. The "Options of Select Folder dialog" dialog shown in the above picture, shows the capabilities of the CSelectFolder class.

This Select Folder dialog was originally done by Mihai Filimon. I have added the following features -

  1. Added an edit control where the user can type in the path
  2. If the path typed in the edit control does not exist then the user will be prompted as to whether he/she wants the path to be created
  3. Setting the flag bShowFilesInDir to TRUE will result in all the files in the current folder to be displayed in the dialog
  4. If you don't want to display all the files then you can use the file filter to display the file types you want to display

Some more additional features

  1. Calling API SetTitle with the desired title will set the Title of the dialog. This API has to be invoked before DoModal is called
  2. User can pass the Initial Folder to be displayed in the constructor of CSelectFolder

Constructor

CSelectFolderDialog(BOOL bShowFilesInDir = FALSE, LPCSTR
    lpcstrInitialDir = NULL, DWORD dwFlags = OFN_HIDEREADONLY |
    OFN_OVERWRITEPROMPT,LPCTSTR lpszFilter = NULL, 
    CWnd* pParentWnd = NULL);

Usage Examples

CSelectFolderDialog oSelectFolderDialog(FALSE,
        NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL, NULL);

CSelectFolderDialog oSelectFolderDialog(FALSE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,NULL, NULL);

CSelectFolderDialog oSelectFolderDialog(TRUE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,NULL, NULL);

CSelectFolderDialog oSelectFolderDialog(TRUE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,"Microsoft Word Documents
        (*.doc)|*.doc|Microsoft Excel Worksheets
        (*.xls)|*.xls|", NULL);

CSelectFolderDialog oSelectFolderDialog(TRUE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,"HTML Files (*.html,
        *.htm)|*.html;*.htm||", NULL);

Here's a complete example on how I got the select folder dialog shown in the above picture.

CSelectFolderDialog oSelectFolderDialog(TRUE,
    "C:\WINNT", 
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "Microsoft Word Documents (*.doc)|*.doc|"
    " Microsoft Excel Worksheets (*.xls)|*.xls|", 
    this);

    if (oSelectFolderDialog.DoModal() == IDOK)
        AfxMessageBox(oSelectFolderDialog.GetSelectedPath());

License

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


Written By
Software Developer (Senior) Philips Electronics India Limited
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncrash on visual studio 2010 Pin
kerfall1-Aug-11 3:16
kerfall1-Aug-11 3:16 
AnswerRe: crash on visual studio 2010 Pin
kerfall1-Aug-11 3:37
kerfall1-Aug-11 3:37 
GeneralGot an Exception when using SelectFolder dialog in visual c++ 2005 Pin
salimon24-Mar-10 5:55
salimon24-Mar-10 5:55 
GeneralRe: Got an Exception when using SelectFolder dialog in visual c++ 2005 Pin
Sridhar Rao13-Apr-10 22:30
Sridhar Rao13-Apr-10 22:30 
GeneralReally useful! Pin
DTardio25-Sep-08 3:11
DTardio25-Sep-08 3:11 
QuestionException using in VS2005 [modified] Pin
David_Leikis7-Mar-07 6:26
David_Leikis7-Mar-07 6:26 
We have included this class into an app that has been working for a couple of years now without error. After converting to VS2005, I get an error in the same area that has been mentioned in other posts. That error is an exception on CRect

Within:
OnInitDone()
rectCurrFolderEB;
pFD->GetDlgItem(edt1)->GetWindowRect(rectCurrFolderEB);

GetWindowRect tries to use edt1 to get it's rectangle and I get an assert because it is a value of 0x000000!

It seems that the object is not being created yet, (the dialog doesn't seem to be fully built) but I see no other message handler function that I can use since OnInitDone() is supposed to be after everthing is built and ready...


I have the latest edition that you have on this site and it seems to only be because of the conversion of VS 6.0 to VS2005.


Do you have any fixes?

Modified: I just was able to find the fix that you posted concerning non-function of OnInitDone() in WinXP and I tried it. It seems to work just fine. Thanks for that. Could you please incorporate those changes into your most recent version?

Regards,

David





-- modified at 17:25 Wednesday 7th March, 2007
GeneralCan't use in a library Pin
David_Leikis16-Feb-06 5:47
David_Leikis16-Feb-06 5:47 
GeneralBug when creating wrong folder in the Edit box Pin
Veronique72211-Aug-05 11:13
Veronique72211-Aug-05 11:13 
GeneralRe: Bug when creating wrong folder in the Edit box Pin
Rail Jon Rogut22-Nov-05 9:26
Rail Jon Rogut22-Nov-05 9:26 
GeneralCrashes on Windows XP Pin
Thorell25-Apr-03 1:02
Thorell25-Apr-03 1:02 
GeneralRe: Crashes on Windows XP Pin
Rail Jon Rogut6-Jul-03 21:06
Rail Jon Rogut6-Jul-03 21:06 
GeneralStandard dialog to browse folders Pin
Comet21-Jan-03 22:31
Comet21-Jan-03 22:31 
GeneralRe: Standard dialog to browse folders Pin
Benny 67318-Mar-04 3:57
sussBenny 67318-Mar-04 3:57 
GeneralDisable Create-Folder. Pin
TM-Mitu14-Nov-02 3:03
TM-Mitu14-Nov-02 3:03 
GeneralDon't work on VC7! Pin
Kondriks6-Jul-02 9:07
Kondriks6-Jul-02 9:07 
Questionmultiple directory select? Pin
Orgen Kl15-May-02 2:44
Orgen Kl15-May-02 2:44 
GeneralFit a dialog in a View of a Window Pin
17-Apr-02 8:47
suss17-Apr-02 8:47 
Questionworking without mouse -- impossible? Pin
14-Feb-02 18:17
suss14-Feb-02 18:17 
Generalnetwork name compatibility Pin
5-Jul-01 22:32
suss5-Jul-01 22:32 
Generalanother little error ... Pin
thom_as3-Jul-02 4:15
thom_as3-Jul-02 4:15 
GeneralRe: another little error ... Pin
Ales Krajnc29-Aug-02 3:00
Ales Krajnc29-Aug-02 3:00 
GeneralSmall Error Pin
10-May-01 10:47
suss10-May-01 10:47 
QuestionIs this OK on Win95/98/ME ? Pin
Jerry Evans4-Oct-00 8:28
Jerry Evans4-Oct-00 8:28 
AnswerRe: Is this OK on Win95/98/ME ? Pin
sridhar4-Oct-00 20:14
sridhar4-Oct-00 20:14 
GeneralRe: Is this OK on Win95/98/ME ? Pin
Jeremy Davis4-Oct-00 23:05
Jeremy Davis4-Oct-00 23:05 

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.