Using the Common Dialog Boxes

To support long filenames (and to save some time), I used two of the Windows 95 common dialog boxes to open and save the house-listing information. I was actually able to use some code I had written for the Windows 3.1 common dialog boxes; when I recompiled, the application displayed the new dialog boxes. I had to strip off the filename extension (TXT, in this case) before I set the caption text for the main window. As you can see in Figure 7-3 on the next page, the Open common dialog box has no problem with long filenames, such as Listing for Puget Sound or Another Saved Listing.

You'll see some differences between the code used to handle common dialog boxes in CHICOAPP and that used in MFCEXP. MFC has built-in support for the File Open and File New functions. The AppWizard tool adds entries to the message map for the application in the main module's CPP file—in this case, MFCEXP.CPP:

BEGIN_MESSAGE_MAP (CMfcexpApp, CWinApp)
// {{AFX_MSG_MAP (CMfcexpApp)
ON_COMMAND (ID_APP_ABOUT, OnAppAbout)
// NOTE: ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
// }}AFX_MSG_MAP
// Standard file-based document commands
// ON_COMMAND (IDM_NEW, CWinApp::OnFileNew)
// ON_COMMAND (IDM_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP ()

Figure 7-3.

The Open common dialog box used in CHICOAPP.

In my MFCEXP sample, the file input/output commands are handled in the view class. In response to a command to open or save a file, the application calls the common dialog box directly through the CFileDialog class. When creating the class, the application passes initialization information that is used to fill out the OPENFILENAME structure. This structure can be accessed directly through the m_ofn member variable:

void CMfcexpView::OnOpen ()
{
CFileDialog Dlg (TRUE, "*.txt", m_lpstrFile,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilter);

if (Dlg.DoModal () == IDOK)
{
HANDLE hFile;
DWORD dwBytesRead;
DWORD dwFileSize;

if ((hFile = CreateFile ((LPCTSTR) Dlg.m_ofn.lpstrFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL)) == (HANDLE)(-1))
{
AfxMessageBox ("File open failed.");
return;
}

// Get the size of the file.
dwFileSize = GetFileSize (hFile, NULL);
if (dwFileSize == 0xFFFFFFFF)
{
AfxMessageBox ("GetFileSize failed!");
return;
}

// Allocate a buffer to read the file into.
m_lpBufPtr = (char *) malloc (dwFileSize);
if (m_lpBufPtr == NULL)
{
AfxMessageBox ("malloc failed!");
CloseHandle (hFile);
return;
}

// Read the file contents into the buffer.
ReadFile (hFile, (LPVOID)m_lpBufPtr, dwFileSize, &dwBytesRead, NULL);

if (dwBytesRead == 0)
{
AfxMessageBox ("Zero bytes read.");
return;
}

// Close the file.
CloseHandle (hFile);

// Parse the file buffer.
if (ParseFile ());
{
lstrcpy (m_lpstrFile, Dlg.m_ofn.lpstrFileTitle);
// Reset the title in the title bar.
GetParentFrame() -> SetWindowText(m_lpstrFile);
// Redraw the title bar.
GetParentFrame() -> Invalidate(TRUE);
}

}
}