Click here to Skip to main content
15,867,999 members
Articles / Desktop Programming / MFC
Article

CFileDialogST v1.0

Rate me:
Please Sign up or sign in to vote.
4.89/5 (17 votes)
25 Jun 20013 min read 184.8K   4.9K   59   32
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


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionJapanese Language Pin
sherin_9-Nov-11 0:37
professionalsherin_9-Nov-11 0:37 
QuestionProblem and solution with using an existing file name Pin
CoreyCooper2-Oct-11 20:10
CoreyCooper2-Oct-11 20:10 
QuestionFile extension problem Pin
miniC.rl16-Mar-07 21:41
miniC.rl16-Mar-07 21:41 
GeneralOnFileNameOK() warning - return value opposite!!! Pin
Tornacious3-Aug-06 8:42
Tornacious3-Aug-06 8:42 
GeneralPre-selecting VIEW mode Pin
Alex Evans14-Nov-04 15:45
Alex Evans14-Nov-04 15:45 
GeneralVery nice class, but... Pin
sps-itsec4621-Jun-04 11:48
sps-itsec4621-Jun-04 11:48 
GeneralRe: Very nice class, but... Pin
Yu youngkuk27-Oct-06 15:46
Yu youngkuk27-Oct-06 15:46 
GeneralRe: Very nice class, but... Pin
sps-itsec4627-Oct-06 23:40
sps-itsec4627-Oct-06 23:40 
AnswerRe: Very nice class, but... Pin
PatrikE21-Nov-06 23:11
PatrikE21-Nov-06 23:11 
QuestionHow to resizing FileDialog? Pin
Shougo TODA6-May-04 20:46
Shougo TODA6-May-04 20:46 
AnswerRe: How to resizing FileDialog? Pin
Shougo TODA9-May-04 17:11
Shougo TODA9-May-04 17:11 
AnswerRe: How to resizing FileDialog? Pin
Shougo TODA9-May-04 17:12
Shougo TODA9-May-04 17:12 
GeneralI think people need upgrade this control to new version Pin
SamKu13-Apr-04 22:03
SamKu13-Apr-04 22:03 
GeneralSerialize Pin
Basty15-Dec-03 3:36
Basty15-Dec-03 3:36 
Questionhow to specify a starting directory for the "browse to dir" Pin
ns24-Sep-03 4:08
ns24-Sep-03 4:08 
GeneralDoModal seem fails... Pin
mindcracker8-Jan-03 9:40
mindcracker8-Jan-03 9:40 
Generalwinver problem... Pin
Mario M.10-Dec-02 3:59
Mario M.10-Dec-02 3:59 
GeneralRe: winver problem... Pin
mindcracker8-Jan-03 9:06
mindcracker8-Jan-03 9:06 
GeneralSimply implementation of a win2k style OPEN dialogbox... Pin
nilaysoft14-Oct-02 19:07
nilaysoft14-Oct-02 19:07 
QuestionHow do I select all files so I can drag them? Pin
Sharapov26-Sep-02 1:03
Sharapov26-Sep-02 1:03 
GeneralFirst chance exception Pin
18-Jun-02 8:03
suss18-Jun-02 8:03 
GeneralRe: First chance exception Pin
Davide Calabro18-Jun-02 21:22
Davide Calabro18-Jun-02 21:22 
GeneralRe: First chance exception Pin
21-Jun-02 4:57
suss21-Jun-02 4:57 
GeneralRe: First chance exception Pin
Anonymous14-Aug-02 21:52
Anonymous14-Aug-02 21:52 
GeneralDoModal always return IDCANCEL for SelectFolder Pin
18-Jun-02 4:34
suss18-Jun-02 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.