开发者

How to get save as dialog in MFC

How do I make a "save as" dialog in MFC?

F开发者_C百科or example, when I click "save as" in MFC, a dialog appears. How do I replicate that?


Here is an example from MSDN for an open dialog box :

void CMyClass::OnFileOpen()
{
   // szFilters is a text string that includes two file name filters:
   // "*.my" for "MyType Files" and "*.*' for "All Files."
   TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

   // Create an Open dialog; the default file name extension is ".my".
   CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
      OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

   // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
   // returns IDOK.
   if(fileDlg.DoModal() == IDOK)
   {
      CString pathName = fileDlg.GetPathName();

      // Implement opening and reading file in here.

      //Change the window's title to the opened file's title.
      CString fileName = fileDlg.GetFileTitle();

      SetWindowText(fileName);
   }
}

For the Save As dialog box, just change the CFileDialog call by :

   CFileDialog fileDlg(FALSE, _T("my"), _T("*.my"),
        OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

Remarks:

  • Some arguments are optional.
  • szFilters contains the file extensions you need.


Like this:

CFileDialog dlg(FALSE);
dlg.DoModal();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜