-
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.
-
April 14th, 2009, 07:50 AM
#7
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by GCDEF
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.
If you get a pointer to the active view you still have to type cast it to your view type.
How is MFC going to know what particular functions you have in your View?
You can always initialize you pointer to NULL and know if it's been initialized.
And by the way, if you want to access a particular view, now that I think about it, who says it has to be active??? It just has have been created! It could be sitting in memory somewhere and you are accessing, some of it's components.
What I'm getting at is that if you do decide to get the Active View anywhere in the App via:
Code:
::AfxGetApp()->GetMainWnd()->GetParentFrame()->GetActiveView();
... what is your next step?
Rate this post if it helped you.
-
April 14th, 2009, 07:59 AM
#8
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
If you get a pointer to the active view you still have to type cast it to your view type.
How is MFC going to know what particular functions you have in your View?
You can always initialize you pointer to NULL and know if it's been initialized.
And by the way, if you want to access a particular view, now that I think about it, who says it has to be active??? It just has have been created! It could be sitting in memory somewhere and you are accessing, some of it's components.
What I'm getting at is that if you do decide to get the Active View anywhere in the App via:
Code:
::AfxGetApp()->GetMainWnd()->GetParentFrame()->GetActiveView();
... what is your next step?
Why does MFC have to know what functions are in your view?
As I said, you can get all the views not just the active one, but usually that's the one you want.
How does initializing to NULL help? Every time the framework opens or closes a document, changes the active view, etc. you need to update your pointers. It's really sounding like you're not really sure how the doc/view architecture works and how to use it properly.
-
April 14th, 2009, 08:27 AM
#9
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by GCDEF
Why does MFC have to know what functions are in your view?
As I said, you can get all the views not just the active one, but usually that's the one you want.
How does initializing to NULL help? Every time the framework opens or closes a document, changes the active view, etc. you need to update your pointers. It's really sounding like you're not really sure how the doc/view architecture works and how to use it properly.
Why does MFC have to know?? If you want to access a function that you defined in for your view you're going to have to tell MFC. So you have to have to type cast the active view everytime you access it view MFC general functions. If I have to access the Active View several times within a dialog box, after a while you're going to realize that it would be lot easier to just have a pointer to the View Class. Why call all those MFC functions everytime and then do a type cast. A pointer would save you time and save the app from computational overhead.
And regarding the null pointer, once the View is activated, it will not return a null, so what I ment was that checking for a Null value on the pointer will let you know if it's active.
When you have mutliple views you have to keep track of your pointers anyway. MFC is not going to manage your views for you.
It sounds to me like you haven't done much coding in situations where you have many dialog boxes at multiple levels initiated from a view class. Having a pointer to your view comes in very handy. I'm not going to go through:
Code:
(MyView*)(:AfxGetApp()->GetMainWnd()->GetParentFrame()->GetActiveView())->MyFunction();
... or something close to that, evertime I have to access a particular view from a dialog box
Code:
gpMyView->Myfunction(); // Is a lot better.
gpMyView->MyVar = ... // "
gpMyView->OnFileNew(); "
That's what I'm talking about!
Last edited by ADSOFT; April 14th, 2009 at 08:35 AM.
Rate this post if it helped you.
-
April 14th, 2009, 08:35 AM
#10
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
What I'm getting at is that if you do decide to get the Active View anywhere in the App via:
Code:
::AfxGetApp()->GetMainWnd()->GetParentFrame()->GetActiveView();
... what is your next step?
Next step:
Code:
if(pActiveView && pActiveView->IsKindOf(RUNTIME_CLASS(CMyDesiredView)))
{
AfxMessageBox(_T("Boo!!! ;-)"));
((CMyDesiredView*)pActiveView)->...
}
Last edited by ovidiucucu; April 14th, 2009 at 08:38 AM.
-
April 14th, 2009, 08:37 AM
#11
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
Why does MFC have to know?? If you want to access a function that you defined in for your view you're going to have to tell MFC. So you have to have to type cast the active view everytime you access it view MFC general functions. If I have to access the Active View several times within a dialog box, after a while you're going to realize that it would be lot easier to just have a pointer to the View Class. Why call all those MFC functions everytime and then do a type cast. A pointer would save you time and save the app from computational overhead.
And regarding the null pointer, once the View is activated, it will not return a null, so what I ment was that checking for a Null value on the pointer will let you know if it's active.
When you have mutliple views you have to keep track of your pointers anyway. MFC is not going to manage your views for you.
It sounds to me like you haven't done much coding in situations where you have many dialog boxes at multiple levels initiated from a view class. Having a pointer to your view comes in very handy. I'm not going to go through:
Code:
(MyView*)(:AfxGetApp()->GetMainWnd()->GetParentFrame()->GetActiveView())->MyFunction();
... or something close to that, evertime I have to access a particular view from a dialog box
Code:
gpMyView->Myfunction(); // Is a lot better.
gpMyView->MyVar = ... // "
gpMyView->OnFileNew(); "
That's what I'm talking about!
Or you could just write a function that uses the framework to get your view and does the cast in the function. You call your function, no casting necessary.
The point I'm trying to make is MFC manages the pointers for you. You close a document and open a document, you have a different view object. You have to manage that yourself. Why duplicate what MFC is already doing?
OnFileNew is typically handled by the app class. If you're doing it from the view, you're overriding standard doc/view behavior, and while that may work for you, it's probably not a good general purpose solution.
Last edited by GCDEF; April 14th, 2009 at 08:40 AM.
-
April 14th, 2009, 08:53 AM
#12
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
Code:
gpMyView->Myfunction(); // Is a lot better.
gpMyView->MyVar = ... // "
gpMyView->OnFileNew(); "
Are you trying to teach us "C Programming in CPP files"?
-
April 14th, 2009, 09:11 AM
#13
Re: How to call OnFileNew() from a dialog box in MFC VC++?
GCDEF
I agree that having a global pointer to a view class is not going to work in MDI case, it does cut down a lot of work in SDI's which is what I mostly work on.
Having a function to return a pointer in every dialog box that returns a pointer to it's view class is too much overhead.
Creating a member variable in each Dialog that points to it's base View might work better.
Rate this post if it helped you.
-
April 14th, 2009, 09:21 AM
#14
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Anyhow, this discussion is off-topic, the OP having nothing in common with a view or an active view.
-
April 14th, 2009, 09:36 AM
#15
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Originally Posted by ADSOFT
GCDEF
I agree that having a global pointer to a view class is not going to work in MDI case, it does cut down a lot of work in SDI's which is what I mostly work on.
Having a function to return a pointer in every dialog box that returns a pointer to it's view class is too much overhead.
Creating a member variable in each Dialog that points to it's base View might work better.
I didn't say put a function in every dialog, I said, or meant, set up a global function that uses the framework to get the view pointer, returning the type that's appropriate for your app. Done properly, it will work for SDI or MDI.
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
|