11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7. Common Dialog Boxes<br />

DWORD nMaxFile;<br />

LPTSTR lpstrFileTitle;<br />

DWORD nMaxFileTitle;<br />

LPCTSTR lpstrInitialDir;<br />

LPCTSTR lpstrTitle;<br />

DWORD Flags;<br />

WORD nFileOffset;<br />

WORD nFileExtension;<br />

LPCTSTR lpstrDefExt;<br />

DWORD lCustData;<br />

LPOFNHOOKPROC lpfnHook;<br />

LPCTSTR lpTemplateName;<br />

} OPENFILENAME;<br />

It has 20 members, which can all be used to customize the dialog box. In this and the following<br />

sections, we are going to see how to use them to change the default behavior of the file dialog box.<br />

File Extension Filter<br />

One of the most important members in this structure is lpstrFilter, which lets us specify a user<br />

defined filter for displaying files. Only those files whose extensions match one of the filters will be<br />

displayed in the dialog box. We can specify as many filters as we want. Each filter is made up of two parts:<br />

the text description and the filter string. The text description is used to give the user an idea about the type<br />

of the files, the filter string usually contains wildcard characters that can be used to specify file types. For<br />

example, if we want to display only bitmap files, the description could be “Bitmap File (*.bmp)” and the<br />

filter string should be “*.bmp”. Filters are separated by zeros. Within a filter, the description text and the<br />

filter string are also separated by a zero. For example, if we want to specify two filters, one is “*.cpp”,<br />

another is for “*.htm”, we should set the filter like this:<br />

lpstrFilter=“CPP File(*.cpp)\0*.cpp\0HTML File(*.htm)\0*.htm\0”;<br />

In the above statement, “CPP File(*.cpp)\0*.cpp\0” is the first filter and “HTML File(*.htm)<br />

\0*.htm\0” is the second filter.<br />

A filter can select more than one type of files. If we specify this type of filter, the different file types<br />

should be separated by a ‘;’ character. For example, in the above example, if we want the first filter to<br />

select both “*.cpp” and “*.h” file, its filter string should be “*.cpp;*.h”.<br />

Besides the standard filter, we can also specify a custom filter. In the file dialog boxes, the custom<br />

filter will always be displayed in the first place of the filter list. To specify a custom filter, we can store the<br />

filter string in a buffer, use member lpstrCustomFilter to store the buffer’s address, and use member<br />

nMaxCustFilter to store the buffer’s size.<br />

If we have a list of filters, we can use only one of them at any time. Member nFilterIndex lets us<br />

specify which filter will be used as the initial one. Here the index to the first file filter is 1.<br />

Retrieving File Names<br />

After function CFileDialog::DoModal() is called, we can use CFileDialog::GetFileName() or<br />

CFileDialog::GetPathName() to retrieve the file name selected by the user. The difference between the<br />

two functions is that CFileDialog::GetFileName() returns full file name (including extension), and<br />

CFileDialog::GetFilePath() returns full path name (file name plus directory names). There are some<br />

other member functions that we can call to retrieve file extension, file title, etc.<br />

File Open<br />

Sample 7.1\CDB demonstrates how to use file dialog box and how to customize its standard styles. It<br />

is a standard SDI application generated by Application Wizard. After the application is generated, a new<br />

sub- menu File Dialog Box is added to mainframe menu IDR_MAINFRAME between View and Help. Two<br />

new commands File Dialog Box | File Open and File Dialog Box | File Save are added to this sub-menu,<br />

whose IDs are ID_FILEDIALOGBOX_FILEOPEN and ID_FILEDIALOGBOX_FILESAVE respectively. Two<br />

WM_COMMAND message handlers are added to class CCDBDoc (the document class) for the new commands<br />

171

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!