11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7. Common Dialog Boxes<br />

“Help” button.<br />

OFN_HIDEREADONLY Set this bit to 0 to enable a Read only check box on the dialog box so that the<br />

user can specify if the file should be opened only for read.<br />

OFN_ALLOWMULTISELECT Set to allow multiple files to be selected at the same time.<br />

Dialog Box Title<br />

The default titles of file dialog boxes are “File Open” and “Save As”. We can change these titles by<br />

preparing text string in a buffer and assign its address to lpstrTitle member of structure OPENFILENAME.<br />

Retrieving Multiple Path Names and File Names<br />

If we allow the user to select more than one file, we need to call function CFileDialog::<br />

GetStartPosition() and CFileDialog::GetNextPathName(…) to retrieve path name for each selected file.<br />

Here, the first function will return a POSITION type value, which could be used to call the second function.<br />

The returned value of the second function is a CString type value, which contains a valid file path name.<br />

Also, when calling the second function, the POSITION value will also be updated, which again can be used<br />

to get the next selected file path name. If all the selected files have been enumerated, a NULL value will be<br />

stored in the variable that holds the POSITION value.<br />

However, there is no similar functions for retrieving all file names. To obtain all the selected file<br />

names, we need to access member lpstrFile of structure OPENFILENAME. After the user has made<br />

selections, the selected folder and file names will be stored in a buffer pointed by member lpstrFile. For<br />

Explorer-style dialog box, folder and file names are separated by ‘\0’ characters; for old style dialog box,<br />

they are separated by SPACE character. In either case, folder name is always the first item contained in the<br />

buffer, which is followed by separator (‘\0’ or SPACE), file name, separator, and file name.... The position<br />

of the first file name is specified by member nFileOffset.<br />

Sample<br />

Sample 7.2\CDB demonstrates these styles. It is based on sample 7.1\CDB, with two new commands<br />

File Dialog Box | Customized File Open and File Dialog Box | Customize File Open Old added to the<br />

application. The IDs of the two commands are ID_FILEDIALOGBOX_CUSTOMIZEDFILEOPEN and<br />

ID_FILEDIALOGBOX_CUSTOMIZEFILEOPENOLD respectively. Message handlers are added for them through<br />

using Class Wizard, the corresponding member functions are CCDBDoc::<br />

OnFiledialogboxCustomizedfileopen() and CCDBDoc::OnFiledialogboxCustomizefileopenold().<br />

For dialog box invoked by command File Dialog Box | Customized File Open, multiple file selection<br />

is enabled. Also, the dialog box has a “Help” button and a “Read only” check box. The following is the<br />

implementation of this command:<br />

void CCDBDoc::OnFiledialogboxCustomizedfileopen()<br />

{<br />

CFileDialog dlg(TRUE);<br />

CString szStr;<br />

POSITION posi;<br />

LPSTR lpstr;<br />

dlg.m_ofn.lpstrFilter="Source<br />

Files(*.C;*.CPP;*.H;*.HPP)\0*.C;*.CPP;*.H;*.HPP\0Document(*.DOC;*.HTML)\0*.DOC;*.HTML\0Al<br />

l(*.*)\0*.*\0";<br />

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

dlg.m_ofn.lpstrCustomFilter="DIB Files(*.BMP)\0*.BMP\0";<br />

dlg.m_ofn.nMaxCustFilter=26;<br />

dlg.m_ofn.Flags|=OFN_ALLOWMULTISELECT | OFN_SHOWHELP;<br />

dlg.m_ofn.Flags&=~OFN_HIDEREADONLY;<br />

dlg.m_ofn.lpstrTitle="Explorer Style Open";<br />

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

{<br />

szStr="File Name: ";<br />

lpstr=dlg.m_ofn.lpstrFile+dlg.m_ofn.nFileOffset;<br />

while(*lpstr != '\0')<br />

{<br />

174

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

Saved successfully!

Ooh no, something went wrong!