Delphi XE2: minimized application restoring by opening a file in Windows

297 Views Asked by At

The problem is: How to hide additional form when application minimizes because after application restores, additional form can't be closed. Attached code shows the behavior. First I open additional form by pressing button. It has set Form Style set fsStayOnTop. Then I press timer button and minimize main form. After timer restores forms the additional one can't be closed.

program MINIBUG;

uses
  Vcl.Forms,
  MainForm in 'MainForm.pas' {Form7},
  AddForm in 'AddForm.pas' {Form8};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm7, Form7);
  Application.CreateForm(TForm8, Form8);
  Application.Run;
end.

unit AddForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm8 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

end.

unit MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AddForm, Vcl.ExtCtrls;

type
  TForm7 = class(TForm)
    btnAddForm: TButton;
    tmr1: TTimer;
    Button1: TButton;
    procedure btnAddFormClick(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form7: TForm7;


implementation

{$R *.dfm}

procedure TForm7.btnAddFormClick(Sender: TObject);
begin
  Form8.Show;
end;

procedure TForm7.Button1Click(Sender: TObject);
begin
  tmr1.Enabled := True;
end;

procedure TForm7.tmr1Timer(Sender: TObject);
begin
  tmr1.Enabled := False;
  form8.Close;
  Application.Restore;
end;

end.
1

There are 1 best solutions below

0
On

Testcase error?

I'm not sure your test case is correct. If the timer event simulates the act of doubleclicking an associated file, why would that lead to a Form8.Close action? You said, a part of the problem was that the additional form became visible (together with the main form) when the associated file was opened, so the hiding (Form.Hide) should take place when you start the timer, and at OnTimer the form should be shown (`Form.Show).

Answer

Anyway, the answer to your actual question, How to hide additional form when application minimizes is that you don't have to do anything special. The additional form will be hidden too, without any actions.

If you for some reason want to or must actively hide the additional form, do so by adding a TApplicationEvents component to your main form, and use its OnMinimize event to call Form8.Hide and OnRestore event to call Form8.Show.

Also consider

Btw, there is a difference if you choose Form.Close or Form.Hide. Close goes through a procedure to call CloseQuery() while Hide simply sets the Visible property yo False.