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 14. Views<br />

Original mapping:<br />

ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)<br />

New mapping:<br />

ON_COMMAND(ID_FILE_OPEN, OnFileOpen)<br />

Customizing “Save As” Dialog Box<br />

Besides file open, we also need to think about file saving. This is more complex than file open,<br />

because we need to allow the user to save the file being edited with a different format. In case the user<br />

changes data format, we must also change the original file extension (from “rtf” to “txt” or vice versa).<br />

To customize file saving to support multiple file formats, we need to override function CDocument::<br />

DoSave(…). This is an undocumented member function of <strong>MFC</strong>. Unfortunately, because file dialog box is<br />

implemented within this function, we have no other choice to support multiple file format without<br />

modifying it. Although using undocumented functions is not recommendable, sometimes we have to do so<br />

in order to make our applications perfect.<br />

Function CDocument::DoSave(…) has two parameters:<br />

BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace);<br />

The first parameter is a pointer to the buffers containing the file name, the second is a Boolean type<br />

variable indicating if the file name should be changed. Actually, this parameter is always set to TRUE so<br />

we can neglect its value.<br />

Pointer lpszPathName gives us the file name that should be used for saving data. But this pointer can<br />

also be NULL. If the file being edited is created through File | New command and the user has selected File<br />

| Save or File | Save As command, lpszPathName will be NULL. The following table lists the values of<br />

lpszPathName and bReplace under different situations:<br />

File creation command Save command Value of lpszPathName<br />

File | New File | Save NULL<br />

File | New File | Save As NULL<br />

File | Open File | Save Actual File Path<br />

File | Open File | Save As NULL<br />

Based on the above analysis, we can override function CDocument::DoSave(…) as follows: obtaining<br />

the file name from lpszPathName, if it is NULL, we implement the “Save As” dialog box with multiple file<br />

filters. After the user has selected a file name, we need to add extension to it according to the filter selected<br />

by the user. Also, we need to set data format for file saving. Then we can call function CDocument::<br />

OnSaveDocument(…) and pass the file name to it to implement file saving.<br />

In the derived function CWordPadDoc::DoSave(…), we need to implement the customized “Save As”<br />

dialog box and also, add file extension, change file format if necessary. The following is a portion of this<br />

function:<br />

……<br />

dlg.m_ofn.lpstrFilter=STRING_FILTER;<br />

dlg.m_ofn.nFilterIndex=1;<br />

if(dlg.DoModal() == IDOK)newName=dlg.GetPathName();<br />

else return FALSE;<br />

if(dlg.m_ofn.nFilterIndex == 1)m_bRTF=TRUE;<br />

else m_bRTF=FALSE;<br />

if(bReplace)<br />

{<br />

CString szFind;<br />

if(dlg.m_ofn.nFilterIndex == 1)<br />

{<br />

432

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

Saved successfully!

Ooh no, something went wrong!