CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  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,352

    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
    Hanover Germany
    Posts
    20,395

    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,352

    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
    Location
    Florida
    Posts
    12,635

    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.

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

  8. #8
    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
    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.

  9. #9
    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
    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.

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

  11. #11
    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
    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.

  12. #12
    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
    Code:
    gpMyView->Myfunction(); // Is a lot better. 
    gpMyView->MyVar = ...    // "     
    gpMyView->OnFileNew();               "
    Are you trying to teach us "C Programming in CPP files"?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  13. #13
    Join Date
    Jun 2004
    Posts
    1,352

    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.

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

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

  15. #15
    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
    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.

Page 1 of 2 12 LastLast

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