CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    How to close an application from one of its views?

    Hi, I have multiple frames in my application and I wish to get rid of the MainFrame Window's caption/title bar/sysmenu etc. etc. and just have a clean thick frame. It can be achieved by adding code to PreCreateWindow() but this also gets rid of the exit (X) button. So I will have to have a button somewhere in one of my views that would close the application. I tried such a thing in a dialog base application and I was able to close the dialog by calling OnOK(). What function should I call to close an MDI multi-frame application?
    Software Development is an art, not a job.

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: How to close an application from one of its views?

    AfxGetMainWnd()->PostMessage(WM_CLOSE);

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

    Re: How to close an application from one of its views?

    Quote Originally Posted by ur_unholyness
    Hi, I have multiple frames in my application and I wish to get rid of the MainFrame Window's caption/title bar/sysmenu etc. etc. and just have a clean thick frame. It can be achieved by adding code to PreCreateWindow() but this also gets rid of the exit (X) button. So I will have to have a button somewhere in one of my views that would close the application. I tried such a thing in a dialog base application and I was able to close the dialog by calling OnOK(). What function should I call to close an MDI multi-frame application?
    Code:
    AFxGetMainWnd()->PostMessage(WM_CLOSE);

  4. #4
    Join Date
    Nov 2001
    Posts
    323

    Re: How to close an application from one of its views?

    try overriding the views OnClose function and then in it add
    PostQuitMessage(WM_CLOSE);
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  5. #5
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: How to close an application from one of its views?

    Quote Originally Posted by VictorN
    Code:
    AFxGetMainWnd()->PostMessage(WM_CLOSE);
    error C2065: 'AFxGetMainWnd' : undeclared identifier
    error C2227: left of '->PostMessageA' must point to class/struct/union

    I tried this and it worked

    Code:
     ((CMainFrame*)AfxGetMainWnd())->OnClose();
    Thanks for the help anyways.
    Software Development is an art, not a job.

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

    Re: How to close an application from one of its views?

    Quote Originally Posted by ur_unholyness
    error C2065: 'AFxGetMainWnd' : undeclared identifier
    error C2227: left of '->PostMessageA' must point to class/struct/union

    I tried this and it worked

    Code:
     ((CMainFrame*)AfxGetMainWnd())->OnClose();
    Thanks for the help anyways.
    Come one. That was an typing mistake. You should have taken a look in MSDN about AFxGetMainWnd(). That would have shown you that the actual name is AfxGetMainWnd().

    Many global functions start with the prefix "Afx", but some, such as the dialog data exchange (DDX) functions and many of the database functions, deviate from this convention. All global variables start with "afx" as a prefix.
    Marius Bancila
    Home Page
    My CodeGuru articles

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

  7. #7
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: How to close an application from one of its views?

    I am not pointing to the typing error, AfxGetMainWnd() is actually not being recognised in my program withut using the (CMainFrame*) part. I had a similar trouble using AfxGetApp() function too. Sorry that I over looked the typing error before copying it into my program and also before quoting.
    Last edited by ur_unholyness; March 21st, 2005 at 05:15 PM.
    Software Development is an art, not a job.

  8. #8
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: How to close an application from one of its views?

    It can't be. From where are you calling AfxGetMainWnd()
    If there is no love sun won't shine

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: How to close an application from one of its views?

    Besides all that strange hassle with AfxGetMainWnd() being mistyped and/or not found: As Zim327 (almost correctly) stated, the official way to close your app is to call PostQuitMessage(0).

  10. #10
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: How to close an application from one of its views?

    gstercken, when i used PostQuitMessage(0) in order to close down my MFC app I got memory leaks of the objects CMainFrame, CViews etc. But I have not tried it in the OnClose() function of the view.

    I think the safe way is to get the main wnd and post a WM_CLOSE message. When iI used this i no longer had the memory leaks.
    If there is no love sun won't shine

  11. #11
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: How to close an application from one of its views?

    Quote Originally Posted by miteshpandey
    It can't be. From where are you calling AfxGetMainWnd()
    I got it to work, and I don't know why was it not being recognized by my compiler. A lillte while earlier, I had a similar problem with AfxGetApp(). Well, when I used them in a different project, they both work absolutely fine. I am still not sure why was it not being deteced in the old project.
    Software Development is an art, not a job.

  12. #12
    Join Date
    Feb 2001
    Location
    TN
    Posts
    290

    Re: How to close an application from one of its views?

    What about ExitProcess()?

    Any reason not to use it?

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

    Re: How to close an application from one of its views?

    Quote Originally Posted by RickCrone
    What about ExitProcess()?

    Any reason not to use it?
    It is NOT clean, at least for MFC application.
    Try to use it in Debug mode and you will see a lot of "memory leaks!" messages and also "Unhandled exception in <...>.exe (MFC42D.DLL): 0xC0000005: Access Violation."

    The only "clean" way to close a (window) application is to post WM_CLOSE message to its main frome window!

  14. #14
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: How to close an application from one of its views?

    Quote Originally Posted by gstercken
    the official way to close your app is to call PostQuitMessage(0).
    This wouldn't close/save opened documents though..

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