0

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?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Robin
  • 1,658
  • 15
  • 26
  • 1
    From [`this post`](http://stackoverflow.com/a/10745981/960757) you should read your answer. – TLama Mar 25 '13 at 16:08
  • 1
    C++ Builder2007 != Delphi –  Mar 25 '13 at 16:43
  • 1
    Task Manager offers two ways to stop programs. One is to select a task on the application tab and choose "End Task"; the other is to select an item from the process tab and choose "End Process." You cannot detect the latter at all. – Rob Kennedy Mar 25 '13 at 16:47
  • @Robin Actually, program terminating via `Task Manager -> End Task` is allowed to prompt. It's when Windows informs the app that the session is shutting down that it's '_not allowed to_' prompt. These two situations are distinguishable within your app. Refer to linked questions on the right for more information. – Disillusioned Jan 08 '17 at 02:44

1 Answers1

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

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384