I just added the following OnClose handler to my application's main form:
I just added the following OnClose handler to my application's main form:
procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caMinimize;
end;
But when I press the close button, the form does not minimize but the application terminates.
WTF?
Here is the code from VCL.Forms that causes this:
procedure TCustomForm.Close;
var
CloseAction: TCloseAction;
begin
// ...
DoClose(CloseAction);
if CloseAction <> caNone then
if Application.MainForm = Self then
Application.Terminate
// ...
end;
When was this introduced? Or was it always the case that no matter what CloseAction you return from OnFormClose of your main form, if it's not caNone, the application terminates?
procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caMinimize;
end;
But when I press the close button, the form does not minimize but the application terminates.
WTF?
Here is the code from VCL.Forms that causes this:
procedure TCustomForm.Close;
var
CloseAction: TCloseAction;
begin
// ...
DoClose(CloseAction);
if CloseAction <> caNone then
if Application.MainForm = Self then
Application.Terminate
// ...
end;
When was this introduced? Or was it always the case that no matter what CloseAction you return from OnFormClose of your main form, if it's not caNone, the application terminates?
It's always been this way. When the main form is closed the application terminates.
ReplyDeleteunless there's something I'm missing, you first set:
ReplyDeleteAction := caMinimize;
and then:
if CloseAction <> caNone then -- CloseAction = caMinimize at this point
not sure what I'm missing...
It has been like this at least since Delphi 6. The TCustomForm.Close method hasn't changed at all. I think it strange too, that minimizing the main form should result in terminating the application, but that has been the case since about forever, apparently.
ReplyDeleteDorin Duminica The second part is not Thomas' code but from the VCL.
ReplyDeleteKenneth Cochran But I am not closing it but minimizing. That's what setting CloseAction to caMinimize is supposed to do according to the online help. So, in order to get the desired behaviour, I have to change the code to:
ReplyDeleteprocedure TMyForm.FormClose(
Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
WindowState := wsMinimized;
end;
Which is plain annoying.
Daniela Osterhagen thank you I knew I was missing something, too tired >.<
ReplyDeleteThomas Mueller stop whining and get over it. You have got a solution, move on!
ReplyDelete;-)
Daniela Osterhagen always the vanguard of courteousness, aren't we? ;-)
ReplyDeleteBut you are right, of course.
You're welcome. ;-)
ReplyDeleteDaniela Osterhagen A nice, light touch ;)
ReplyDeleteBTW, how is the application supposed to be closed? For another solution you can use the CloseQuery event, set CanClose to false and call Application.Minimize.
ReplyDeleteThis is what I ended up with:
ReplyDeleteprocedure TMyForm.FormClose(
Sender: TObject; var Action: TCloseAction);
begin
if CheckSomeCondition then begin
Action := caNone;
Hide;
end;
end;
I agree completely with the VCL implementation.
ReplyDeleteYou can't have an application that won't close in any way except Task Manager.
I think that is bordering on un-ethical and it certainly is un-friendly.
Kind Regards,
A
What about a tray icon application that closes through the exit entry in the popup menu?
ReplyDeleteThat shouldn't be on the X, should be on the minimize.
ReplyDeleteI hate applications that don't respect that kind of contract.
If I want to close it, then let me close it, don't oblige me to fumble across a gazillion icons to find the right one.
At the very least,give me an option to use the default behaviour and make sure it's the default.
A
Thomas Mueller Be careful when doing stuff like this, or you can cause the system to hang at shutdown, with your app refusing to close. Better make sure you handle those EndSession messages properly.
ReplyDeleteAndrea Raimondi I have never had a single user complaint about my app that minimizes to the taskbar and closes to the tray, with an option on the tray icon's popup menu to exit. They actually appreciate the way I designed it to work. While I don't agree that all apps should work this way, there are cases where this is a good design decision. Those cases are few and far between.
I hate the applications that do that, unless there's a good reason.
ReplyDeleteVery often, however, that is not the case.
I have seen this happen * so many times* I am totally annoyed by whoever does this.
Please don't do this.
A