CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ovidiucucu View Post
    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.

  2. #17
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #18
    Join Date
    Jun 2004
    Posts
    1,352

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by GCDEF View Post
    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.

  4. #19
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ADSOFT View Post
    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

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ADSOFT View Post
    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.

  6. #21
    Join Date
    Jun 2004
    Posts
    1,352

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by VictorN View Post
    Yes, of course!
    Message handlers have to be protected member functions in a class.
    Therefore cilu suggested you to use SendMessage!

    No kidding Albert Einstien
    Rate this post if it helped you.

  7. #22
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ADSOFT View Post
    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
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #23
    Join Date
    Apr 2009
    Posts
    3

    Unhappy 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?

  9. #24
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by krishna_kumar View Post
    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.

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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 View Post
    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

  11. #26
    Join Date
    Jun 2004
    Posts
    1,352

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ovidiucucu View Post
    That's because, again, you do not (want to) read all the replies.
    http://www.codeguru.com/forum/showth...05#post1832805
    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.

  12. #27
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ADSOFT View Post

    ....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?
    Victor Nijegorodov

  13. #28
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by ADSOFT View Post
    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.

  14. #29
    Join Date
    Jun 2004
    Posts
    1,352

    Re: How to call OnFileNew() from a dialog box in MFC VC++?

    Quote Originally Posted by GCDEF View Post
    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.

  15. #30
    Join Date
    Apr 2009
    Posts
    3

    Talking 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...

Page 2 of 2 FirstFirst 12

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
  •  





Click Here to Expand Forum to Full Width

Featured