Solved

Override OnFileNew

Posted on 1997-09-17
6
589 Views
Last Modified: 2013-11-19
Hi everyone
I,m working on an MDI app in VC 4.0.
I've made a dialog box which I've connected to a toolbar-button.
I would like the dialog to show everytime I open a new document in my application.
That is, when I push the "new" button I would like the dialog to show and a new empty document to open.
Something like:
void CMyView::OnFileNew()
{
      CWinApp::OnFileNew;
        OnMyButton();
}
The problem is that I'm not allowed to do that because OnFileNew is protected in CWinApp.
What should I do?

The approach above may be completely incorrect. If there is another way to achive what I want I would be more then happy to know.

This cannot be too difficult.

Thanks in advance,

Barney
0
Comment
Question by:barneyman
6 Comments
 
LVL 1

Accepted Solution

by:
kargo earned 200 total points
ID: 1306408
Check out TN022.  This is a tech note that explains the entire process.  In basic; you don't need to overload the function in the base class, you just need to handle the command message.  The technote explains it all.
Ken

0
 

Author Comment

by:barneyman
ID: 1306409
I do not understand the tech note.
0
 
LVL 1

Expert Comment

by:kargo
ID: 1306410
In basic: What you do is use ClassWizard and create a message handler in CWinApp for ID_FILE_NEW.  This will create a function that is called each time File New is hit by the user.  Now you can do whatever you want to do in place of the default handling.  You should look at the source for CWinApp::OnFileNew() and use it as a template since you will propably want to link the calling of new into a document.  I would suggest copying the CWinApp implementation and modifying it to suit your needs.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1306411
Another choice would be to provide a public member function that calls the protected member function in your CWinApp derived class.

For example,

  class CMyWinApp : public CWinApp
  {
  public:
    void NewFile() { CWinApp::OnFileNew(); }
    // other stuff
  };

Then your view can use

  ((CMyWinApp*)theApp)->NewFile();

to create a new document.


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1306412
Why are you handling file new in a VIEW ????

It SHOULD be handled in an application object, not a view.  In fact, creating a new document from a view does not make much sense, because the view is a view OF A DOCUMENT, so creating a new document will mean the view object will have to kill itself.  It can be done ... but just seems a bit strange.

Also, if you want to get the current application, don't use "theApp", use ::AfxGetApp() instead (this doesn't rely on how you named you app object).

Also again, you cannot call a member function of a non-base class from within a deirved class - ie. you cannot say CWinApp::OnFileNew from within a CView, becaase CView doe NOT derive from CWinApp.  You have to get an object of type CWinApp (from ::AfxGetApp()) and call through that,

0
 

Author Comment

by:barneyman
ID: 1306413
thanks, everyone, yuo're great!!
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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