Besides its name, the most important detail of a file is its extension. This is 1 or a combination of 2, 3, 4, or more characters that end the full name of a file. The extension is separated from the actual name of a file by a period. For example, the extension of a Microsoft Word file is .docx. The extension of a Perl file is pl. When you display a File dialog box to the user, you have many options to specify a default file extension to filter the types of files the user would deal with. To assist you, the CFileDialog class provides a second argument: lpszDefExt. If you decide to use it. Pass the desired extension. This is mostly useful when saving a file, in which case if the user gives only a name to the file but doesn't select an extension, when the user clicks Save, the operating system would apply the value of the lpszDefExt argument to it. Here is an example of specifying a default file extension: void CExerciseDlg::OnBnClickedFileBtn() { CFileDialog *dlg = new CFileDialog(FALSE, L".txt"); dlg->DoModal(); } If you don't want to specify a default file extension, pass the second argument as NULL. After dealing with the File dialog box (whether opening or saving a file), if the user clicks the Save or the Open button, to let you find out the extension of that file, you can call the CFileDialog::GetFileExt() member function. Its syntax is: CString GetFileExt() const;
You can create an application that deals with only one type of file and you specify the type of extension. Or you can create application that can handle many types of files. In this case, you would allow a user to specify the type of file to open or save. In reality, a file extension is the primary means of a user to categorize a file. If your application can deal with various types of files, when opening or saving a file, you can assist the user by giving an extension to the file. In fact, you can create a series of file extensions that the user would select from. A filter is one or more strings that specify or restrict the types of files the user can use when opening files or when saving one. For a text-based application, you may allow only text files, that is, files with a txt extension. For a rich text-based application, you may allow only Rich Text Format files, which are files with rtf extension. On the other hand, if you are creating an application for web files, you can allow as many file extensions as necessary, such as htm, html, php, asp, etc. As you may realize, text files or web files are all text-based files. This means that if you create a text-based or rich-text based application, you should allow the users to decide whether the file they are trying to open can be "read" by a text-based control. To create a list of file extensions for your File dialog box, create one or a combinations of values where each uses the following formula: Prompt|Extension If saving a file, the Prompt is a section that defines what the user would see in the Save As Type combo box: If opening the file, the Prompt would display in a combo box on the right side of the File Name box: An example would be Text Files. Such a string does not let the user know what actual extension the file would use. Therefore, as a courtesy, you can specify, between parentheses, the extension that would be applied if this extension is used. Therefore, the Prompt can be Text Files (*.txt). In this case, the extension used would be .txt. The asterisk * lets the user know that whatever is provided as the file name would be used in place of the asterisk. The period indicates the separation from the file to its extension. This means that the characters on the left of the period would be the file name, the characters on the right side of the period would be used as the actual file extension. To specify the extension that the operating system would use to associate to the file, you provide a second part of the string as Extension. In Microsoft Windows, most extensions are made of three characters. Some applications use a 2-letter extensions (for example Perl files have a .pl extension and C# files use .cs) and some others use 4 letters (such as .html for some HTML files). This depends. An example of a string that species an extension is: Text Files (*.txt)|*.txt You should end a file with ||. If you want to provide various extensions to your Save dialog box, you can separate them with a | symbol. An example would be: HTML Files (*.htm)|*.htm|Active Server Pages (*.asp)|*.asp|Perl Script (*.pl)|*.pl|| To specify the filter used by the File dialog box, you can pass its string as the fourth argument to the CFileDialog constructor. Here is an example: void CExerciseDlg::OnBnClickedFileBtn()
{
CFileDialog *dlg = new CFileDialog(TRUE, L"txt", NULL, NULL,
L"HTML Files (*.htm)|*.htm|"
L"Active Server Pages (*.asp)|*.asp|"
L"Apache Files (*.php)|*.php|"
L"Perl Script (*.pl)|*.pl|"
L"All Files||");
dlg->DoModal();
}
This would produce:
By default, the File dialog box allows a user to select one file and open it. If you want, you can make it possible to open many files at once. To start, add the OFN_ALLOWMULTISELECT flag to either the appropriate member of the OPENFILENAME variable or add it to the fourth argument of the CFileDialog constructor. Here is an example: void CExerciseDlg::OnBnClickedFileBtn()
{
CFileDialog *dlg = new CFileDialog(TRUE, L"txt", NULL, OFN_ALLOWMULTISELECT);
dlg->DoModal();
}
When this has been done, the user can press the Ctrl or Shift keys to select the files. |
|
|||||||
|