-
April 14th, 2009, 09:37 AM
#16
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by ovidiucucu
Anyhow, this discussion is off-topic, the OP having nothing in common with a view or an active view.
Good point, as OnFileNew is a CWinApp function, not a CView function.
AfxGetApp()->OnFileNew() should do the trick.
-
April 14th, 2009, 09:38 AM
#17
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Returning on-topic.
As already stated here before (by GCDEF), usually in an MDI/SDI application, the wizard maps the ID_FILE_NEW command as follows...
Code:
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
//...
END_MESSAGE_MAP()
...which simply calls the base class OnFileNew method (CWinApp::OnFileNew).
If someone really wants to modify the default implementation and/or add something, it has to:
- modify ON_COMMAND macro removing the base class call
Code:
ON_COMMAND(ID_FILE_OPEN, &OnFileNew)
- declare and implement OnFileNew handler function in its own CWinApp-derived class
Code:
class CMyApp : public CWinApp
{
public:
afx_msg void OnFileNew();
//...
};
Code:
void CMyApp::OnFileNew()
{
CWinApp::OnFileNew(); // call base class method and / or...
// ...add other tasks here.
}
Further, if really, really wants to call CMyApp::OnFileNew anywhere in the application, simply has to write something like this
Code:
CMyApp* pApp = (CMyApp*)AfxGetApp();
if(pApp)
{
pApp->OnFileNew();
}
or
Code:
theApp.OnFileNew();
// no matter which is the active view, the framework will correctly do its job.
Last edited by ovidiucucu; April 14th, 2009 at 09:47 AM.
Reason: typo
-
April 14th, 2009, 11:26 AM
#18
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by GCDEF
Good point, as OnFileNew is a CWinApp function, not a CView function.
AfxGetApp()->OnFileNew() should do the trick.
Sorry,
error C2248: 'OnFileNew' : cannot access protected member declared in class 'CWinApp'
... I tried it, didn't work.
Rate this post if it helped you.
-
April 14th, 2009, 11:47 AM
#19
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by ADSOFT
![Confused](images/smilies/confused.gif) Sorry,
error C2248: 'OnFileNew' : cannot access protected member declared in class 'CWinApp'
Yes, of course!
Message handlers have to be protected member functions in a class.
Therefore cilu suggested you to use SendMessage!
Victor Nijegorodov
-
April 14th, 2009, 11:48 AM
#20
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by ADSOFT
![Confused](images/smilies/confused.gif) Sorry,
error C2248: 'OnFileNew' : cannot access protected member declared in class 'CWinApp'
... I tried it, didn't work.
Guess you need to override it then to call it directly, or as cilu pointed out in the first reply in this thread, send a message.
-
April 14th, 2009, 12:16 PM
#21
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by VictorN
Yes, of course!
Message handlers have to be protected member functions in a class.
Therefore cilu suggested you to use SendMessage! ![Cool](images/smilies/cool.gif)
No kidding Albert Einstien
Rate this post if it helped you.
-
April 15th, 2009, 03:09 AM
#22
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by ADSOFT
![Confused](images/smilies/confused.gif) Sorry,
error C2248: 'OnFileNew' : cannot access protected member declared in class 'CWinApp'
... I tried it, didn't work.
That's because, again, you do not (want to) read all the replies.
http://www.codeguru.com/forum/showth...05#post1832805
-
April 15th, 2009, 07:49 AM
#23
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Guys, thanks for all of ur replies..
But i asked how open a new document when a button in the dialog box is clicked...
And i am just a basic programmer in MFC..
I created my own dialog box with a button.. and need to open the new doc from there..
Can u help me with that?
-
April 15th, 2009, 08:04 AM
#24
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by krishna_kumar
Guys, thanks for all of ur replies..
But i asked how open a new document when a button in the dialog box is clicked...
And i am just a basic programmer in MFC..
I created my own dialog box with a button.. and need to open the new doc from there..
Can u help me with that?
That's been answered several times in this thread. The first reply told you what to do.
-
April 15th, 2009, 08:11 AM
#25
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Dear krishna_kumar!
As cilu already wrote in the post#2:
![Quote](images/misc/quote_icon.png) Originally Posted by cilu
You can post a WM_COMMAND message with ID_FILE_NEW to the main window.
For example, you could place this line somewhere within your dialog:
Code:
AfxGetMainWnd()->SendMessage(WM_COMMAND, ID_FILE_NEW);
Victor Nijegorodov
-
April 15th, 2009, 10:07 AM
#26
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by ovidiucucu
The point I was trying to make there is that his advise doesn't work, and that he should try the code before he posts it.
....obviously it's going to take more than sending a message from a non-member function because the OnFileNew base class function is PROTECTED!!!!!!!!!!!!!
Last edited by ADSOFT; April 15th, 2009 at 10:12 AM.
Rate this post if it helped you.
-
April 15th, 2009, 10:14 AM
#27
-
April 15th, 2009, 10:19 AM
#28
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by ADSOFT
![Roll Eyes (Sarcastic)](images/smilies/rolleyes.gif) The point I was trying to make there is that his advise doesn't work, and that he should try the code before he posts it.
....obviously it's going to take more than sending a message from a non-member function because the OnFileNew base class function is PROTECTED!!!!!!!!!!!!!
You're taking this a little too seriously. So, it was protected. Override it or send a message. What's the big deal? The point remains that OnFileNew is an app function, not a view function, and that maintaining a global pointer to a view wasn't a particularly good solution to the problem. Don't take it so personally.
-
April 15th, 2009, 10:35 AM
#29
Re: How to call OnFileNew() from a dialog box in MFC VC++?
![Quote](images/misc/quote_icon.png) Originally Posted by GCDEF
You're taking this a little too seriously. So, it was protected. Override it or send a message. What's the big deal? The point remains that OnFileNew is an app function, not a view function, and that maintaining a global pointer to a view wasn't a particularly good solution to the problem. Don't take it so personally.
I thought we all agreed that OnFileNew was an App function, it was an oversite on my part thinking it was part of the View, that's why we got back on topic. I had a simular situation arise when I had to print from Dialog boxes in my app, at first I sent messages then created a pointer to the View, so I could print from anywhere in the app; .... so sorry for the confusion on that part.
Yes the function is protected and if I would have addressed the problem without mentioning that, then some of these guys would have raised a storm.
Rate this post if it helped you.
-
April 15th, 2009, 12:40 PM
#30
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Thanks a lot guys...
I have to check the coding.. No, i have to implement that coding..
U guys cleared my problem... That means a lot to me...
Thank u...
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
|