Class That Allows For Selecting Files or Folders

Thursday Jul 29th 1999 by Yoshioka Tsuneo
Share:

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);
}
Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved