Link to home
Start Free TrialLog in
Avatar of okg
okg

asked on

How to override File Save and File Save As

Dear experts

I'm using VC++5.0.
I'd like to override File Save and File Save As which are
called by File menu item.
For this, I installed override function using ID_FILE_SAVE and ID_FILE_SAVE_AS of File menu on CMainFrame.
And in override function, OnFileSave(), I called pDoc->OnFileSave(). But when I select File Save menu item, CMainFrame::OnFileSave() is never called.
How can I call CMainFrame::OnFileSave() and finally pDoc->OnFileSave() when I select File Save ?

Thanks in advance.
Avatar of vachooho
vachooho
Flag of United States of America image

This may happen if you already have OnFileSave() and OnFileSaveAs() handlers in your view class.

Check all view classes in your project.
Hope you will find where you have message handler for ID_FILE_SAVE

and call OnFileSave() of your CMainFrame within that handler

Note  that some MFC classes has build-in OnfileSave,
actualy CEditView and CRichEditView classes has ID_FILE_SAVE
message handled.


Avatar of Zoppo
Hi okg,

to vachooho: I think it's not totally correct what you wrote:

The ID_FILE_SAVE(_AS) messages are handled in CDocument's OnFileSave() and OnFileSaveAs(). These functions aren't virtual, so you cannot override them, but they both call virtual function one can override in his CDocument derived class:

void CDocument::OnFileSave() // message handler for ID_FILE_SAVE
{
 DoFileSave();
}

void CDocument::OnFileSaveAs() // message handler for ID_FILE_SAVE_AS
{
 if (!DoSave(NULL))
  TRACE0("Warning: File save-as failed.\n");
}

BOOL CDocument::DoFileSave() // virtual function called by OnFileSave
{
 ...
}

BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace) // virtual function called by OnFileSaveAs
{
 ...
}

So, override DoSave() and DoFileSave() of the document class to change behavior for FileSave(As)

ZOPPO
Avatar of Vinayak Kumbar
Vinayak Kumbar

Hi,

Map the ID_FILE_SAVE and ID_FILE_SAVE_AS menu options to your view class instead of CMainFrame class. The control will come tomthat function first, and then you can do whatever the functionality you want to do in that function in view class.
Hope it helps you.
ASKER CERTIFIED SOLUTION
Avatar of speedup
speedup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial