-
April 13th, 2009, 09:35 AM
#1
How to call OnFileNew() from a dialog box in MFC VC++?
I am using a MDI application project.
I need to open an new file/document when a button in a dialog box is clicked..
How to call the OnFileNew() or is there any other way to open a new document?
(I also have some other actions to be performed when the button is clicked like closing the dialog..
So, creating a new document is to be 1 of the statement)
I am running into a deadline to finish my project..
Please help me.... Thank u...
-
April 13th, 2009, 12:55 PM
#2
Re: How to call OnFileNew() from a dialog box in MFC VC++?
You can post a WM_COMMAND message with ID_FILE_NEW to the main window.
-
April 14th, 2009, 04:02 AM
#3
Re: How to call OnFileNew() from a dialog box in MFC VC++?
You can also set a pointer to your View Class and then call FileOpen, however if you FileOpen function is set to Protected you must make it public.
Cilu approach is easier, but you are limited to only access the FileOpen, and you have to transmit a message for every function, and I'm pretty sure you are limited to only funtions that have a message attached to them.
If you want access to all the functions in View Class and it's variables (The ones that are or are made to be public) then having a pointer to the view class is cool.
I always create a global pointer to my View, defined just beneath the
"theApp" global var , class so that I can access any Function, Var or even another Dialog Box that
is a member of the View. Having access to a View pointer pretty much gives you access to the whole App.
i.e
Code:
In the App file
CMyApp theApp;
CMyView *gpView; //Define a Global pointer to my view class
In the View Cpp file:
extern CAUTOWEBView *gpView;
void CMyView::OnInitialUpdate()
{
...
::gpView = this; //Initialize the Global Pointer
}
Don't forget to in Include header files in you to your View Class in
your Dialog CPP files and the Extern statement too.
If you have a lot of Dialog boxes in a App with a CView derived Class as the Client Area, I found this to come in handy
Last edited by ADSOFT; April 14th, 2009 at 04:05 AM.
Rate this post if it helped you.
-
April 14th, 2009, 06:40 AM
#4
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
You can also set a pointer to your View Class and then call FileOpen, however if you FileOpen function is set to Protected you must make it public.
...
If you want access to all the functions in View Class and it's variables (The ones that are or are made to be public) then having a pointer to the view class is cool.
I always create a global pointer to my View
What a bad idea to "create a global pointer to ... View" class!
The fact it might work in some rare situations (like in a simple SDI application with ONLY one View class) does NOT mean this approach is reliable.
In fact it is NOT! This won't work in either MDI or in SDI app with multiple Views.
It also contract the basic OOP principle
So, never just try it!
Victor Nijegorodov
-
April 14th, 2009, 07:11 AM
#5
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by VictorN
What a bad idea to "create a global pointer to ... View" class!
The fact it might work in some rare situations (like in a simple SDI application with ONLY one View class) does NOT mean this approach is reliable.
In fact it is NOT! This won't work in either MDI or in SDI app with multiple Views.
It also contract the basic OOP principle
So, never just try it!
I think you're confused and you have to think before you type.
Obviously if you have multiple Views you are going to need multiple pointers to your views. And for your information nothing in life works for every situation, so .... get a clue!!!!
And furthermore if you have multiple views they each have their own OnPrint function!!!! .... stick that in your pipe and smoke it.
So if you want to access OnPrint, you have to know which View's OnPrint your dealing with, Sending a message works, but it's only going to work for that particular View, if you have multiple views that is not GUARNTEED to work either.
... YOU HAVE TO KNOW WHAT VIEW YOU'RE TALKING ABOUT
Rate this post if it helped you.
-
April 14th, 2009, 07:19 AM
#6
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
I think you're confused and you have to think before you type.
Obviously if you have multiple Views you are going to need multiple pointers to your views. And for your information nothing in life works for every situation, so .... get a clue!!!!
And furthermore if you have multiple views they each have their own OnPrint function!!!! .... stick that in your pipe and smoke it.
So if you want to access OnPrint, you have to know which View's OnPrint your dealing with, Sending a message works, but it's only going to work for that particular View, if you have multiple views that is not GUARNTEED to work either.
... YOU HAVE TO KNOW WHAT VIEW YOU'RE TALKING ABOUT
He does know what he's talking about. A global pointer to a view is a really bad idea. Views come and go as documents are open and closed. Trying to keep the global pointer current sounds like way more trouble than it's worth. MFC provides mechanisms to get the active view from anywhere in the app, and to get all the views associated with a document. It's folly to try to maintain that yourself when MFC does it for you and does it reliably.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|