How to call OnFileNew() from a dialog box in MFC VC++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 30

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

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    3

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

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jun 2004
    Posts
    1,329

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,297

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

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

  5. #5
    Join Date
    Jun 2004
    Posts
    1,329

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

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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

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

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center