Click here to Skip to main content
Click here to Skip to main content

CFileDialogST v1.0

By , 25 Jun 2001
Rate this:
Please Sign up or sign in to vote.

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

About the Author

Davide Calabro
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
QuestionJapanese Language Pinmembersherin_9-Nov-11 0:37 
QuestionProblem and solution with using an existing file name [modified] PinmemberCoreyCooper2-Oct-11 20:10 
QuestionFile extension problem PinmemberminiC.rl16-Mar-07 21:41 
GeneralOnFileNameOK() warning - return value opposite!!! PinmemberBaliDawg3-Aug-06 8:42 
GeneralPre-selecting VIEW mode PinmemberAlex Evans14-Nov-04 15:45 
Hello
 
If I wanted this FileDialog to come up in VIEW mode for thumbnails, how can I go about doing this?
 
Any help...
 
Thanks
Alex
GeneralVery nice class, but... PinmembermYkel21-Jun-04 11:48 
GeneralRe: Very nice class, but... PinmemberYu youngkuk27-Oct-06 15:46 
GeneralRe: Very nice class, but... Pinmembermykel27-Oct-06 23:40 
AnswerRe: Very nice class, but... PinmemberPatrikE21-Nov-06 23:11 
QuestionHow to resizing FileDialog? PinmemberShougo TODA6-May-04 20:46 
AnswerRe: How to resizing FileDialog? PinmemberShougo TODA9-May-04 17:11 
AnswerRe: How to resizing FileDialog? PinmemberShougo TODA9-May-04 17:12 
GeneralI think people need upgrade this control to new version PinmemberSamGw13-Apr-04 22:03 
GeneralSerialize PinmemberBasty15-Dec-03 3:36 
Questionhow to specify a starting directory for the "browse to dir" Pinmemberns24-Sep-03 4:08 
GeneralDoModal seem fails... Pinmembermindcracker8-Jan-03 9:40 
Generalwinver problem... PinmemberMario M.10-Dec-02 3:59 
GeneralRe: winver problem... Pinmembermindcracker8-Jan-03 9:06 
GeneralSimply implementation of a win2k style OPEN dialogbox... PinmemberLi Zhaoming14-Oct-02 19:07 
QuestionHow do I select all files so I can drag them? PinmemberSharapov26-Sep-02 1:03 
GeneralFirst chance exception PinmemberSibilant18-Jun-02 8:03 
GeneralRe: First chance exception PinmemberDavide Calabro18-Jun-02 21:22 
GeneralRe: First chance exception PinmemberAnonymous21-Jun-02 4:57 
GeneralRe: First chance exception PinsussAnonymous14-Aug-02 21:52 
GeneralDoModal always return IDCANCEL for SelectFolder PinmemberAnonymous18-Jun-02 4:34 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 26 Jun 2001
Article Copyright 2001 by Davide Calabro
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid