Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
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.
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.
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
GCDEF
Good point, as OnFileNew is a CWinApp function, not a CView function.
AfxGetApp()->OnFileNew() should do the trick.
:confused: Sorry,
error C2248: 'OnFileNew' : cannot access protected member declared in class 'CWinApp'
... I tried it, didn't work.
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
ADSOFT
:confused: 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! :cool:
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
ADSOFT
:confused: 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.
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
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:
No kidding Albert Einstien :rolleyes: :thumbd:
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
ADSOFT
:confused: 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
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?
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
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.
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Dear krishna_kumar!
As cilu already wrote in the post#2:
Quote:
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);
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
ovidiucucu
:rolleyes: 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!!!!!!!!!!!!!
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
ADSOFT
:rolleyes:
....obviously it's going to take more than sending a message from a non-member function because the OnFileNew base class function is PROTECTED!!!!!!!!!!!!!
What does "sending a message from a non-member function" have to do with the declaration type (public/protected/private) of CWinApp::OnFileNew message handler? :confused:
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
Originally Posted by
ADSOFT
:rolleyes: 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.
Re: How to call OnFileNew() from a dialog box in MFC VC++?
Quote:
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.
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...