Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CFileDialogST v1.0

0.00/5 (No votes)
25 Jun 2001 2  
A CFileDialog implementation using APIs.

Sample Image - CFileDialogST.jpg

Abstract

CFileDialogST is a re-implementation of the MFC CFileDialog class made using the SDK APIs. The first valuable feature is the ability to show the new Windows 2000 Open/Save common dialog! Also CFileDialogST includes a function to easily show the common dialog used to select a folder.

The class supports Unicode and is fully compatible with the original MFC implementation. Constructor and functions have the same name and argument list, so it should be painless to use the new one.

CFileDialogST functions

CFileDialogST(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL)

Constructs a CFileDialogST object. Most frequently used parameters can be passed on the argument list.

// Parameters:

//  [IN]    bOpenFileDialog

//           Set to TRUE to construct a File Open dialog box or

//           FALSE to construct a File Save As dialog box.

//  [IN]    lpszDefExt

//           The default filename extension.

//           If the user does not include an extension in the Filename edit box,

//           the extension specified by lpszDefExt is automatically appended

//           to the filename.

//           If this parameter is NULL, no file extension is appended.

//  [IN]    lpszFileName

//           The initial filename that appears in the filename edit box.

//           If NULL, no filename initially appears

//  [IN]    dwFlags

//           A combination of one or more flags that allow 

//           you to customize the dialog box.

//  [IN]    lpszFilter

//           A series of string pairs that specify filters you can apply to the file.

//           If you specify file filters, only selected files will appear in the

//           Files list box.

//  [IN]    pParentWnd

//           Pointer to the owner window for the dialog box. Can be NULL.

//

CFileDialogST(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, 
  LPCTSTR lpszFileName, DWORD dwFlags, 
  LPCTSTR lpszFilter, CWnd* pParentWnd)

CFileDialogST()

Constructs a CFileDialogST object. All required parameters must be initialized by hand accessing the m_ofn and m_bOpenFileDialog public members.

DoModal()

This function displays the file selection dialog box and allows the user to make a selection. All required fields of the m_ofn public structure must be filled. This can be done using the class constructor or accessing directly the structure. Also, the public variable m_bOpenFileDialog must be set to TRUE to get an open dialog box or to FALSE to get a save dialog box.

// Return value:

//  IDOK

//    The user has selected a filename.

//  IDCANCEL

//    The user has closed the dialog without selecting any filename.

//

int DoModal()

CString GetPathName() const

This function returns the full path of the selected file.

// Return value:

//   A CString object containing the full path of the file.

//

CString GetPathName() const

CString GetFileName() const

This function returns the filename of the selected file.

// Return value:

//   A CString object containing the name of the file.

//

CString GetFileName() const

CString GetFileTitle() const

This function returns the title of the selected file.

// Return value:

//   A CString object containing the title of the file.

//

CString GetFileTitle() const

CString GetFileExt() const

This function returns the extension of the selected file.

// Return value:

//   A CString object containing the extension of the file.

//

CString GetFileExt() const

CString GetFileDir() const

This function returns the directory (without drive) of the selected file.

// Return value:

//   A CString object containing the directory (without drive) of the file.

//

CString GetFileDir() const

CString GetFileDrive() const

This function returns the drive of the selected file.

// Return value:

//   A CString object containing the drive of the file.

//

CString GetFileDrive() const

POSITION GetStartPosition() const

This function returns the position of the first element of the filename list.

// Return value:

//   A POSITION value that can be used for iteration.

//   NULL if the list is empty.

//

POSITION GetStartPosition() const

CString GetNextPathName(POSITION& pos) const

This function returns the full path of the next selected file.

// Parameters:

//   [IN]    pos

//            A reference to a POSITION value 

//            returned by a previous GetNextPathName 

//            or GetStartPosition function call. 

//            NULL if the end of the list has been reached.

//

// Return value:

//   A CString object containing the full path of the file.

//

CString GetNextPathName(POSITION& pos) const

int SelectFolder(LPCTSTR lpszTitle = NULL, LPCTSTR lpszStartPath = NULL, UINT ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS, CWnd* pParentWnd = NULL)

This function lets the user to select a folder.

// Parameters:

//   [IN]    lpszTitle

//             Address of a null-terminated string that is displayed above the 

//             tree view control in the dialog box. This string can be used to 

//             specify instructions to the user. Can be NULL.

//   [IN]    lpszStartPath

//             Address of a null-terminated string containing the initial folder

//             to open. Can be NULL.

//   [IN]    ulFlags

//             Flags specifying the options for the dialog box.

//   [IN]    pParentWnd

//             Pointer to the owner window for the dialog box. Can be NULL.

//

// Return value:

//   IDOK

//     The user has selected a folder and pressed OK. A call

//     to GetSelectedFolder() will return the selected folder.

//   IDCANCEL

//     The user has closed the dialog without selecting any folder.

//

int SelectFolder(LPCTSTR lpszTitle, LPCTSTR lpszStartPath, 
                               UINT ulFlags, CWnd* pParentWnd)

CString GetSelectedFolder() const

This function returns the folder selected by the user with a call to SelectFolder.

// Return value:

//    A CString object containing the selected folder.

//    Without a previous call to SelectFolder this string can be empty or

//    reflect the last selected folder.

//

CString GetSelectedFolder() const

Example

The CFileDialogST demo application shows how to open files (even with multiple-selection), to ask for a filename to save and how to browse for a folder.

Want to include CFileDialogST in a DLL ?

CFileDialogST is ready to be used from inside a DLL. You need to export from your DLL CFileDialogST. Include in your DLL's project, the following files:

  • FileDialogST.h
  • FileDialogST.cpp

Add to your DLL's project settings, the following defines:

  • _CMLHTDLL_NOLIB_
  • _CMLHTDLL_BUILDDLL_

From FileDialogST.h, comment the following line:

#define _FILEDIALOGST_NODLL_

Then, update the various #pragma comment(lib, "???") according to your DLL produced .lib files.

Remarks

This architecture makes possible to add other features to the class. It would be possible, for example, to add the support for the select-computer command dialog. If someone implements new features, I will be happy to include his code in the next CFileDialogST demo application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here