Class That Allows For Selecting Files or Folders
Posted
by Yoshioka Tsuneo
on July 29th, 1999
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);
if(dlg.DoModal()==IDOK)
{
CString pathname = dlg.GetPathName;
}
/*
CFileFolderDialog.h
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.
ex)
CFileFolderDialog dlg(TRUE);
if(dlg.DoModal()==IDOK){
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
{
DECLARE_DYNAMIC(CFileFolderDialog)
public:
CFileFolderDialog(BOOL bOpenFileDialog, // FileOpen(TRUE) or FileSave(FALSE)
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);
virtual CString GetPathName() const;
virtual CString GetFileName() const;
virtual CString GetFileExt() const;
virtual CString GetFileTitle() const;
protected:
// CString m_strPathName; // selected full path name
protected:
//{{AFX_MSG(CFileFolderDialog)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
#endif // !defined(AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_)
//////////////////////////////////////////////////////////////////////
// FileFolderDialog.cpp : Implementation File
//
#include "stdafx.h"
#include "FileFolderDialog.h"
#include
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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)
//{{AFX_MSG_MAP(CFileFolderDialog)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
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()
{
CFileDialog::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);
}

Comments
edt1 cannot be found in VS2005
Posted by Anarchi on 04/20/2009 02:03amSolution: Add #include
then change 'edt1' to 'cmb13'
Replysmall bug
Posted by crazytan on 07/14/2004 07:16amconsider the case below: CFileFolderDialog fdlg(...OFN_ALLOWMUTILSELECT,,); char buf[1024] = {0}; fdlg.m_ofn_lpstrFile = buf; fdlg.m_ofn_nMaxFile = 1024; if(fdlg.DoModel() == IDOK){ //if a dir selected, fdlg.GetFathName() returns an empty string .... } so, make GetPathName a little change CString CFileFolderDialog::GetPathName() const{ CString pathname(m_szFileName); if(CString(m_szFileTitle) == DUMMY_FILE){ if(pathname.IsEmpty()) pathname = m_ofn_lpstrFile; int slashpos = pathname.ReverseFind('\'); if(slashpos != -1){ pathname = pathname.Left(slashpos); } } return pathname; } now everything's ok
Replyokay, now what about Double Clicking
Posted by Legacy on 05/30/2003 12:00amOriginally posted by: JR
This works great, but what if the user double clicks on a file?
ReplySample asp files
Posted by Legacy on 10/30/2001 12:00amOriginally posted by: chandru
Will anyone please send me the sample ASP source files in order to browse for folder even with network path and select the particular folder into the text box by closing the dialog box.
ReplyExtendig the code a bit
Posted by Legacy on 10/16/2001 12:00amOriginally posted by: Andor Nagy
ReplyCFileFolderDialog Class
Posted by Legacy on 09/11/1999 12:00amOriginally posted by: Tim Seiferth
How do you disable the display of file names and only display folder names in the CFileFolderDialog class? I want to use this dialog to just display directories for selection by a user.
ReplyCFileFolderDialog Class
Posted by Legacy on 08/17/1999 12:00amOriginally posted by: Sreeni
How this could be modified to list only the folders, no files??
ReplyWhen One folder is selected, clicking OK should select it as current path and close the dialog.
Umm.. Where is edt1 defined
Posted by Legacy on 08/04/1999 12:00amOriginally posted by: Scott Aron Bloom
Where can I get a definition of edt1 for the
GetDlgTextItem call?
Thanks.
Scott
Reply