Save as a file with a specific extension
2025-10-31
Written by: xiaobin
Use the CFileDialog to customize the file extension, title, and initial directory.
explicit CFileDialog(
BOOL bOpenFileDialog,
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL,
DWORD dwSize = 0,
BOOL bVistaStyle = TRUE);
The parameter that specifies what type of dialog box to create.
Set it to TRUE to construct a File Open dialog box.
Set it to FALSE to construct a File Save As dialog box.
Use OFN macro combinations.
Hides the Read Only check box.
Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.
For a description of these flags, see the OPENFILENAME structure in the Windows SDK.
strFileName.Format("%d%d%d%d%d%d", nYear, nMonth, nDay, nHour, nMinute, nSecond);
strFileName = "D" + strFileName + ".rtf";
CFileDialog fSaveDialog(FALSE, // Set to TRUE to construct a File Open dialog box or
// FALSE to construct a File Save As dialog box.
".rtf",
strFileName,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"Serial Comm Data (*.rtf)|*.rtf|Text (*.txt)|*.txt||",
this);
fSaveDialog.m_pOFN->lpstrTitle = "Serial Comm Data File (Save As...)";
fSaveDialog.m_pOFN->lpstrInitialDir = "D:";
if (fSaveDialog.DoModal() == IDOK)
strPathName = fSaveDialog.GetPathName();
else
return;
Customize the dialog box title and the initial directory.
fSaveDialog.m_pOFN->lpstrTitle = "Serial Comm Data File (Save As...)";
fSaveDialog.m_pOFN->lpstrInitialDir = "D:";
The path of the filename includes the file’s title plus the entire directory path.