Using MFC CFolderPickerDialog
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.
// 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