?
Solved

How to overwrite ID_APP_EXIT function ?

Posted on 2004-11-24
2
Medium Priority
?
1,446 Views
Last Modified: 2013-11-20
Hi,

I am working with MDI application. I want to ovverwrite ID_APP_EXIT, File->Exit menu function of Mainframe menu. When I click on this menu option or on the close icon button of Main frame, I should check for a particular condition and then allow to exit the application if the condition is true or else I should not allow the user to exit.

I am able to overwrite and check the condition, but unable to close the application if condition is met. If I used PostQuitMessage(0), it's getting closed but I'm getting lot of memory leaks due to this function.

Can anyone please give some inputs?

Thanks
Madhavi
0
Comment
Question by:l_madhavi
2 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 12663973
>> it's getting closed but I'm getting lot of memory leaks due to this function.

You are probably not waiting for a thread to terminate if you have any running.  Check this, and then try

AfxGetMainWnd()->PostMessage(WM_CLOSE);
0
 
LVL 40

Accepted Solution

by:
evilrix earned 120 total points
ID: 12670336
Hi,

PostQuitMessage() terminates the applications message pump, which has the side effect of terminating the application but this isn't really the best way. Normal frame closedown messages (such as WM_CLOSE) are skipped.

I've found it is generally easier to override the OnClose() message handler for WM_CLOSE rather than OnAppExit(). When ID_APP_EXIT is send to the main window it generates a WM_CLOSE message, which is fired to the main frame. This will invoke the OnClose() function. In here you can place your closedown code. If you need to prevent termination of the application just don't call the base class implementation of OnClose().

For example: -

void CMainFrame::OnClose()
{
      BOOL bOkToClose = this->CloseDownFunc();
      
      if(bOkToClose)
      {
            CFrameWnd::OnClose();
      }
}

The other advantage of placing your code in this function is that it will trap ALL attempts to close your application (e.g. via the Windows close gadget). This would not be the case if you use OnAppExit() as this will only trap attempts to exit via the File/Exit menu item.

I hope this helps.

EvilRix.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question