I need that "x" button on any form would not close the form but instead open another 3 random forms on delphi, i have no idea how to do that, please help
CodePudding user response:
Just use the form's OnCloseQuery
event to detect the user's trying to close your form (by clicking the close button in the top-right corner, by double-clicking the form's title bar icon, by selecting the Close system menu item, by pressing Alt F4, etc.).
Then set CanClose
to False
and instead open your three new forms:
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := False;
Form2.Show;
Form3.Show;
Form4.Show;
end;
CodePudding user response:
As suggested by @AndreasRejbrand's answer, you could use the Form's OnCloseQuery
event. But, the problem with that approach is that the event is also triggered during system reboot/shutdown, and you don't want to block that. If OnCloseQuery
returns CanClose=False
during a system shutdown, the shutdown is canceled.
Another option is to use the Form's OnClose
event instead, setting its Action
parameter to caNone
, eg:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Form2.Show;
Form3.Show;
Form4.Show;
end;
However, the best option would to be to handle only user-initiated closures (the X
button, ALT-F4
, etc) by having the Form handle the WM_SYSCOMMAND
message looking for SC_CLOSE
notifications, eg:
procedure TForm1.WndProc(var Message: TMessage);
begin
if (Message.Msg = WM_SYSCOMMAND) and (Message.WParam and $FFF0 = SC_CLOSE) then
begin
Message.Result := 0;
Form2.Show;
Form3.Show;
Form4.Show;
end
else
inherited;
end;
This way, system-initiated closures are unhindered.