procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if ... then
   CanClose:=false
 else
   CanClose:=true;
end;