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);
if(dlg.DoModal()==IDOK)
{
 CString pathname = dlg.GetPathName;
}
This class subclasses the window function in order to hook the "Open"(IDOK) button.

/*
	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:03am

    Solution: Add #include then change 'edt1' to 'cmb13'

    Reply
  • small bug

    Posted by crazytan on 07/14/2004 07:16am

    consider 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

    Reply
  • okay, now what about Double Clicking

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: JR

    This works great, but what if the user double clicks on a file?

    Reply
  • Sample asp files

    Posted by Legacy on 10/30/2001 12:00am

    Originally 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.

    Reply
  • Extendig the code a bit

    Posted by Legacy on 10/16/2001 12:00am

    Originally posted by: Andor Nagy

    This code is great !
    
    

    Although I need to extend the SubclassWindowProc below the SetDlgItemText API function with the

    ::PostMessage(hwnd, uMsg, wParam, lParam);

    row in order to close the dialog and get the dir path. (if you choose only dirs)

    Regards!

    Reply
  • CFileFolderDialog Class

    Posted by Legacy on 09/11/1999 12:00am

    Originally 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.

    Reply
  • CFileFolderDialog Class

    Posted by Legacy on 08/17/1999 12:00am

    Originally posted by: Sreeni

    How this could be modified to list only the folders, no files??
    When One folder is selected, clicking OK should select it as current path and close the dialog.

    Reply
  • Umm.. Where is edt1 defined

    Posted by Legacy on 08/04/1999 12:00am

    Originally posted by: Scott Aron Bloom

    Where can I get a definition of edt1 for the
    GetDlgTextItem call?

    Thanks.

    Scott

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds