Solved

Override OnFileNew

Posted on 1997-09-17
6
583 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prevent this page from creating additional dialogs. 3 416
has77  challenge 9 73
Annoying "thing" blocks my view 4 83
how to split multiple lines delimiter : 8 80
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now