The File Dialog |
|
Microsoft Windows provides a series of objects called Common Dialog boxes. One of them is intended for file processing. The beauty of these dialog boxes is that they are highly configured to handle their intended job but you can still customize them as you see fit. The File Dialog box is a control used to open or save a file. To use a File Dialog, you can first declare a CFileDialog variable using its constructor whose syntax is: |
CFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL ); The CFileDialog constructor requires one argument: the bOpenFileDialog. The second argument, lpszDefExt, is the default extension that should be used or applied on the file. The dwFlags parameter specifies the options to use on the dialog box. The lpszFilter string specifies the types of files that will be dealt with. The pParentWnd parameter is a pointer to the parent of the dialog box, if the dialog box has one.
The only member variable that the File Dialog has is called m_ofn. It is an object derived from a Win32 class (or structure) called OPENFILENAME. In fact, the MFC doesn't do miracles than the Win32 would. The MFC only provides some type of "translation" of the Win32 library on this issue. The OPENFILENAME class is defined as follows: typedef struct tagOFN { DWORD lStructSize; HWND hwndOwner; HINSTANCE hInstance; LPCTSTR lpstrFilter; LPTSTR lpstrCustomFilter; DWORD nMaxCustFilter; DWORD nFilterIndex; LPTSTR lpstrFile; DWORD nMaxFile; LPTSTR lpstrFileTitle; DWORD nMaxFileTitle; LPCTSTR lpstrInitialDir; LPCTSTR lpstrTitle; DWORD Flags; WORD nFileOffset; WORD nFileExtension; LPCTSTR lpstrDefExt; LPARAM lCustData; LPOFNHOOKPROC lpfnHook; LPCTSTR lpTemplateName; #if (_WIN32_WINNT >= 0x0500) void * pvReserved; DWORD dwReserved; DWORD FlagsEx; #endif // (_WIN32_WINNT >= 0x0500) } OPENFILENAME, *LPOPENFILENAME; |
File Dialog Operations |
In order to use a File Dialog box, the user must open it. This can be done by calling the CFileDialog::DoModal() method. When it comes up, the File Dialog displays two buttons regularly available on a normal dialog box. If the file is being opened, the buttons are Open and Cancel. If the user is saving a file, the buttons are Save and Cancel. After using the dialog box, either to open or to save a file, the user can click Open/Save or Cancel. If the user clicks Open or Save, the dialog box is (partly) equipped to validate the operation. For example, if the user clicks Open, the dialog box is configured to make sure that the user selected a file. If the user clicks Save, he or she should have given a name to the file. To dismiss the dialog box without continuing with the intended operation, the user can click Cancel. If the user clicks Cancel, the dialog box throws a CommDlgExtendedError. This error allows you to find out what happens: maybe the user didn't click Cancel, maybe the dialog box was not even created, maybe this, maybe that. This error allows you to diagnose a potential problem and deal with it. If you don't want to go through this, you can just cancel the operation yourself. After displaying the File Dialog box, in other to effectively perform an intended operation, the user must provide a name for the file. To find out what name the user provided, you can call the CFileDialog::GetFileName() method. Its syntax is: |
CString GetFileName( ) const;
A file name is indeed made of various sections. In the strict sense, the file name is a long string that starts from its drive all the way to its "normal" name. This is also referred to as its path. To get the path of the being dealt with, you can call the CFileDialog::GetPathName() method. Its syntax is: |
CString GetPathName( ) const;
In the traditional sense, the name of a file ends with 2, 3, or 4 characters known as its extension. For example, the extension of a Microsoft Word file is doc. The extension of a Perl file is pl. After the user has dealt with a file or while dealing with it, to find out the extension of that file, you can call the CFileDialog::GetFileExt() method. Its syntax is: |
CString GetFileExt( ) const;
Practical Learning: Using the File Dialog Box |
|
|
Copyright © 2004-2015 FunctionX, Inc. |
|