CFileDialog save/replace problem?

Hi

I am working with CFileDialog in MFC SDI application. When I open a file for ex: test.txt from File->Open menu, the contents of the file are read and displayed on the view. When I select, File->Save option  to save the contents of the file, a CFileDialog is displayed. When the selected file from CFileDialog is the same file as the opened one i.e test.txt, then a special message should be displayed to the user and the default windows warning message asking for replacing the file should not be displayed. However if any different already existing file is selected, then the default windows warning message should be displayed. In other words, I want to ovveride the function that is called when we click on CFileDialog's save button.

Can anyone please give me some inputs for doing this?

Thanks
osi-sysAsked:
Who is Participating?
 
nonubikCommented:
BOOL CLogFileDialog::OnFileNameOK()
{
   CString path_name = GetPathName();
    if(path_name.GetLength() > 0)
   {
      if(m_LogFileName.CompareNoCase(path_name) == 0)
      {
           AfxMessageBox(_T("The file you selected is the current log file opened, please choose another file"));      
           return 1; //need to return here 1, too
      }      
      else
      {
         //check if the file exist and display the "standard" message box
         if(FileExists(path_name))
            if(AfxMessageBox(_T("Overwrite file... ?"), MB_YESNO) == IDYES)
              return CFileDialog::OnFileNameOK();
            else return 1; //forbid

         return CFileDialog::OnFileNameOK();
      }
   }
}


The FileExist() function needs to be written. You can check for example with GetFileAttributes and if it returns 0xffffffff it is a failure (e.g. file not found).
0
 
nonubikCommented:
You can derive your own class from CFileDialog and override CFileDialog::OnFileNameOK(). There you can decide to display the mesasge box or not.
0
 
osi-sysAuthor Commented:
Hi nonubik ,

Thanks for your timely response.
Yes, I exactly did the same way as said by you.  

I derived a class from CFileDialog and overrided CFileDialog::OnFileNameOK().

m_LogFileDialog = new CLogFileDialog(FALSE, _T("log"), path, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Log Files (*.log)|*.log||"));

BOOL CLogFileDialog::OnFileNameOK()
{
   CString path_name = GetPathName();
    if(path_name.GetLength() > 0)
   {
      if(m_LogFileName.CompareNoCase(path_name) == 0)
           AfxMessageBox(_T("The file you selected is the current log file opened, please choose another file"));      
           
      else
         return CFileDialog::OnFileNameOK();
   }
}


When I clicked on save button on CFileDialog, the default window warning message saying, “ File name already exists, Do you want to replace the existing file” is displayed first  
and then my message AfxMessageBox(_T("The file you selected is the current log file opened, please choose another file")) is shown next.

The I removed OFN_OVERWRITEPROMPT style during the creation of FileDialog, then only my message was shown. But what I want is that, when I try to save to the file that is currently opened, then only my message should be shown, and in all other cases when an already existing file is selected (other than the currently opened file), the  default window warning message should be displayed.

Can you please help me furher now ?

Thanks
Madhavi  
0
 
osi-sysAuthor Commented:
Thanks nonubik, It's working fine now
0
 
nonubikCommented:
Glad to hear that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.