Class That Allows For Selecting Files or Folders

CFileFolderDialog class

This class extends the basic CFileDialog class. However, using this class, you can
switch between viewing only files or viewing only folders. This way, you don’t
have to programmatically use both the CFileDialog and the CFolderDialog classes.
The interface to the CFileFolderDialog class is nearly identical to the CFileDialog class.

CFileFolderDialog dlg(TRUE);
CString pathname = dlg.GetPathName;

This class subclasses the window function in order to hook the “Open”(IDOK) button.

by tsuneo 1999/07/08
Extended Common DialogBox for Open/Save Filename or Folder.
if textbox is empty and “open” button is pushed, selected folder is returned.
The usage is nearly same as CFileDialog.
CFileFolderDialog dlg(TRUE);
CString pathname = dlg.GetPathName;
#if !defined(AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_)
#define AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_

class CFileFolderDialog : public CFileDialog

CFileFolderDialog(BOOL bOpenFileDialog, // FileOpen(TRUE) or FileSave(FALSE)
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);
virtual CString GetPathName() const;
virtual CString GetFileName() const;
virtual CString GetFileExt() const;
virtual CString GetFileTitle() const;
// CString m_strPathName; // selected full path name

virtual BOOL OnInitDialog();


#endif // !defined(AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_)

// FileFolderDialog.cpp : Implementation File

#include “stdafx.h”
#include “FileFolderDialog.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

// CFileFolderDialog
/* dummy file name for end dialog */
#define DUMMY_FILE “__dummyfile__”

IMPLEMENT_DYNAMIC(CFileFolderDialog, CFileDialog)

CFileFolderDialog::CFileFolderDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
BEGIN_MESSAGE_MAP(CFileFolderDialog, CFileDialog)

WNDPROC oldWindowProc; /* original window function to save*/
/* hooked window funcction */
static LRESULT CALLBACK SubclassWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
if(uMsg == WM_COMMAND){
if(LOWORD(wParam) == IDOK){
/* if OK button is pushed and text box is empty, enter dummy filename for end dialog */
char filename[1000];
if(GetDlgItemText(hwnd, edt1, filename, sizeof(filename)) == 0){
SetDlgItemText(hwnd, edt1, DUMMY_FILE);
return CallWindowProc(oldWindowProc, hwnd, uMsg, wParam, lParam);

BOOL CFileFolderDialog::OnInitDialog()
oldWindowProc = (WNDPROC)::SetWindowLong(GetParent()->m_hWnd, GWL_WNDPROC, (DWORD)SubclassWindowProc);
return TRUE;

CString CFileFolderDialog::GetPathName() const{
CString pathname(m_szFileName);
if(CString(m_szFileTitle) == DUMMY_FILE){
int slashpos = pathname.ReverseFind(‘\’);
if(slashpos != -1){ pathname = pathname.Left(slashpos); }
return pathname;
CString CFileFolderDialog::GetFileName() const{
CString pathname = GetPathName();
int slashpos = pathname.ReverseFind(‘\’);
return pathname.Mid(slashpos + 1);
CString CFileFolderDialog::GetFileExt() const{
CString filename = GetFileName();
int dotpos = filename.ReverseFind(‘.’);
return (dotpos != -1) ? filename.Mid(dotpos+1) : “”;
CString CFileFolderDialog::GetFileTitle() const{
CString filename = GetFileName();
int dotpos = filename.ReverseFind(‘.’);
return filename.Left(dotpos);

More by Author

Must Read