TAGS :Viewed: 22 - Published at: a few seconds ago

[ How do I distinguish different ways of terminating my program? ]

I'm coding a program for Windows, using C++Builder 2007.

When my program is closed, it is supposed to behave differently depending on how it was closed:

  1. User clicked the 'X' of the application's window: return to the application's user login.
  2. Program is terminated via TaskManager: terminate the program then and there. This is especially important as the program must not block Windows from shutting down.

Getting one OR the other is easy: Implement a TForm::OnClose() or OnCloseQuery() to handle the event. However, both don't give me an indication of what caused the event. Is there another way to know what actually caused the close event?

Answer 1


You can make your main form lsiten to the WM_SYSCOMMAND message. If you receive this message with command type SC_CLOSE, then you know that the user has clicked the close button, pressed Alt+F4, or selected 'Close' from the system menu. Then you can do whatever you like (as opposed to letting the default action close the form).

For example, you could display the login dialog again.

This will not affect other ways of closing the main form/application, so you can still close the application from the (first tab) in the Task Manager.

[Previously, this Q was tagged Delphi. Because of this, the first version of my answer contains the Delphi implementation of this idea.]