CFileDialog::CFileDialog
Updated: July 2010
Call this function to construct a standard Windows file dialog box.
explicit CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL, DWORD dwSize = 0, BOOL bVistaStyle = TRUE );
Either a File Open or File Save As dialog box is constructed, depending on the value of bOpenFileDialog.
To enable the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before you call DoModal. You must supply your own file name buffer to store the returned list of multiple file names. Do this by replacing m_ofn.lpstrFile with a pointer to a buffer you have allocated, after you construct the CFileDialog, but before you call DoModal. Additionally, you must set m_ofn.nMaxFile with the number of characters in the buffer pointed to by m_ofn.lpstrFile. If you set the maximum number of files to be selected to n, the necessary buffer size is n*(_MAX_PATH + 1) + 1. For example:
// Create dialog to open multiple files. CFileDialog dlg(TRUE, _T("txt"), _T("*.txt"), OFN_ALLOWMULTISELECT); // Create buffer for file names. const DWORD numberOfFileNames = 100; const DWORD fileNameMaxLength = MAX_PATH + 1; const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1; TCHAR* filenamesBuffer = new TCHAR[bufferSize]; // Initialize beginning and end of buffer. filenamesBuffer[0] = NULL; filenamesBuffer[bufferSize-1] = NULL; // Attach buffer to OPENFILENAME member. dlg.m_ofn.lpstrFile = filenamesBuffer; dlg.m_ofn.nMaxFile = bufferSize; // Create array for file names. CString fileNameArray[numberOfFileNames]; if(dlg.DoModal() == IDOK) { // Retrieve file name(s). POSITION fileNamesPosition = dlg.GetStartPosition(); int iCtr = 0; while(fileNamesPosition != NULL) { fileNameArray[iCtr] = dlg.GetNextPathName(fileNamesPosition); iCtr++; } } // Release file names buffer. delete[] filenamesBuffer;
To enable the user to resize an Explorer-style dialog box by using either the mouse or keyboard, set the OFN_ENABLESIZING flag. Setting this flag is necessary only if you provide a hook procedure or custom template. The flag works only with an Explorer-style dialog box; old-style dialog boxes cannot be resized.
The lpszFilter parameter is used to determine the type of file name a file must have to be displayed in the file list. The first string in the string pair describes the filter; the second string indicates the file name extension to use. Multiple extensions may be specified by using a semicolon (the ';' character) as the delimiter. The string ends with two '|' characters, followed by a NULL character. You can also use a CString object for this parameter.
For example, Microsoft Excel allows users to open files that have extensions .xlc (chart) or .xls (worksheet), among others. The filter for Excel could be written as:
However, if you plan to use this string to directly update the OPENFILENAME structure, you should delimit your strings with the null character, '\0', instead of the vertical bars ('|').
The bVistaStyle parameter is applicable only if the application is compiled in Visual Studio 2008 or later and running under Windows Vista or later. Under earlier versions of Windows, this parameter is ignored. If bVistaStyle is set to TRUE, the new Vista style File Dialog will be used. Otherwise, the previous MFC style File Dialog will be used. See CFileDialog Class for more information.
Dialog templates are not supported on dialogs that have the bVistaStyle.
See the example for CFileDialog::DoModal.
This statement is wrong.
It does not matter whether you compile on XP, Vista or 7.
If you set bVistaStyle to FALSE, the old behaviour of XP can be forced. You can use customized open dialogs as used under previous Windows Versions.
Important is that the new behaviour of Vista and 7 will be used if you compile using Visual Studio 2008 or higher due to MFC 9 because the CFileDialog implementation in MFC 9 introduces the new parameter bVistaStyle which is defaulted to TRUE.
- 11/4/2009
- Gökhan Akca