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 />

}<br />

}<br />

szStr+=lpstr;<br />

szStr+="; ";<br />

lpstr+=strlen(lpstr);<br />

lpstr++;<br />

}<br />

AfxMessageBox(szStr);<br />

szStr.Empty();<br />

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

posi=dlg.GetStartPosition();<br />

while(posi != NULL)<br />

{<br />

szStr+=dlg.GetNextPathName(posi);<br />

szStr+="; ";<br />

}<br />

AfxMessageBox(szStr);<br />

Two flags OFN_ALLOWMULTISELECT and OFN_SHOWHELP are set, this will enable multiple file selection<br />

and display the “Help” button. Also, flag OFN_HIDEREADONLY is disabled, this will display “Read only”<br />

check box in the dialog box. If the dialog box returns value IDOK (This indicates the user has pressed “OK”<br />

button), the first file name is obtained by doing the following:<br />

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

Because the file names are separated by ‘\0’ characters, we can use the following statement to access<br />

next file name:<br />

lpstr+=strlen(lpstr);<br />

Path names are obtained through calling function CFileDialog::GetStartPosition() and<br />

CFileDialog::GetNextPathName(…). The selected file and path names will be displayed in a message box.<br />

Command File Dialog Box | Customize File Open Old has the same functionality, except that the<br />

dialog box is implemented in the old style. Before the dialog box is invoked, flag OFN_LONGNAMES is<br />

disabled, which will convert all long file names to “8.3 format”. Because the file names are separated by<br />

SPACE rather than ‘\0’ characters, the procedure of obtaining file names is a little different:<br />

……<br />

……<br />

void CCDBDoc::OnFiledialogboxCustomizefileopenold()<br />

{<br />

LPSTR lpstr;<br />

LPSTR lpstrNx;<br />

char buf[256];<br />

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

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

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

{<br />

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

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

while(TRUE)<br />

{<br />

lpstrNx=strchr(lpstr, ' ');<br />

if(lpstrNx == NULL)<br />

{<br />

szStr+=lpstr;<br />

break;<br />

}<br />

else<br />

{<br />

memset(buf, 0, 256);<br />

strncpy(buf, lpstr, lpstrNx-lpstr);<br />

szStr+=buf;<br />

}<br />

szStr+="; ";<br />

lpstr=lpstrNx;<br />

175

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

Saved successfully!

Ooh no, something went wrong!