Click here to Skip to main content
15,887,596 members
Articles / Web Development / HTML
Tip/Trick

Using MFC CFolderPickerDialog

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
24 May 2015CPOL 25.1K   8   3  
Using CFolderPickerDialog

Introduction

Searching across the net, I was not able to find any clear examples on how to use CFolderPickerDialog. Therefore, I thought I would share the following example with you.

Background

Being comfortable using CFileDialog will help, but it is not essential.

Using the Code

The code can be copied and pasted from the section below. The code was used in VS 2013 Community Edition in a MFC dialog based application.

C++
// Example code
CFolderPickerDialog m_dlg;
CString m_Folder;
 
m_dlg.m_ofn.lpstrTitle = _T("Put your title here");
m_dlg.m_ofn.lpstrInitialDir = _T("C:\\");
if (m_dlg.DoModal() == IDOK) {
       m_Folder = m_dlg.GetPathName();   // Use this to get the selected folder name 
                                         // after the dialog has closed
 
       // May need to add a '\' for usage in GUI and for later file saving, 
       // as there is no '\' on the returned name
       m_Folder += _T("\\");
       UpdateData(FALSE);   // To show updated folder in GUI
 
       // Debug
       TRACE("\n%S", m_Folder);
}
// End of example

Points of Interest

Previously, I thought OFN had to be declared and used separately, whereas now it appears to be part of the dialog object. Maybe I’m thinking too far back as I last used VC++ 6.0 and have only recently moved to the VS2013 Community.

History

  • 1.0 Initial release
  • 1.1 Corrected HTML translation typos in the example code

License

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


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --