How to override File Save and File Save As

Posted on 1999-07-29
Medium Priority
Last Modified: 2013-11-20
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.
Question by:okg

Expert Comment

ID: 1311890
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.

LVL 31

Expert Comment

ID: 1311891
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

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)


Expert Comment

by:Vinayak Kumbar
ID: 1311892

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.

Accepted Solution

speedup earned 100 total points
ID: 1311893
first, check your views to see if there's function responding to the file open message. if so, mainframe can't respond to it.

second, check if you've specified the on_afx_msg, and ON_MESSAGE in your codes.

and by the way, why not place your codes in your CYourDoc class to respond to the file open message? if you want to access mainframe, you can get the pointer to it by calling AfxGetMainWnd().

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question