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?
Re: How to close an application from one of its views?
AfxGetMainWnd()->PostMessage(WM_CLOSE);
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);
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);
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.
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.
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.
Re: How to close an application from one of its views?
It can't be. From where are you calling AfxGetMainWnd()
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).
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.
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.
Re: How to close an application from one of its views?
What about ExitProcess()?
Any reason not to use it?
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!
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..