Delphi hide the form, not hide the application icon on taskbar

delphi

I use the following code :

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.visible := false;
  Form2.show;
end;

Yes, the form1 got hidden and the form2 show up. But why the application icon in the taskbar also got hidden….

I use the following codes and still can not show the icon on the taskbar, while hide the form1.

      visible := false;
{
      enable := false;
      Application.MainFormOnTaskbar := True;
      ShowWindow(Application.Handle, SW_SHOW);
      SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
}

How to keep the application icon on the taskbar while I want to hide the form ?

I want to do it in the unit files , not the DPR file.

The Files that I want to do keep the system taskbar are at : http://sidhiciang.com/myfiles/ShowHideForms.rar

Unit1.pas
  If Form1.btShowForm2Click() , then 
    Hide Form1 and Show Form2 ( actHideForm1execute(self)).
  If Form1.btCloseForm1Click(), then 
    Close the application

Unit2.pas
  If  Form2.btShowForm3Click(), then 
    Hide Form2 and Show Form3 ( actHideForm2execute(self)).
  If Form2.btCloseForm2Click(), then 
    Show the Form1 and Form2.close (actShowForm1execute(self))

Unit3.pas
  If btCloseFrom3Click(), then
    Show Form2 and Close Form3

In all of the Unit1 / Unit2 / Unit3, Keep the application icon on taskbar available. Because if I use .visible := false, the system taskbar also become hidden.

PS: I use Delphi 2010 and running on Windows XP and 7 Enviorment.

Best Answer

Ok, now that it is clear what you want, first a couple of things:

  • The first Form created is automatically the MainForm,
  • An application cannot without a MainForm; when the MainForm closes, the application closes, whatever other forms are shown,
  • You can hide the MainForm,
  • By default (in older Delphi versions anyway) the application's window is shown on the taskbar (Application.MainFormOnTaskbar = False). As long as the application is active, and as long as there is at least one form showing, this icon/window is shown in the taskbar.
  • When Application.MainFormOnTaskbar = True, then the icon/window of the MainForm is shown in the taskbar. When the MainForm is hiden, the icon dissapears. Showing another form does not result in another taskbar icon/window, so there is no icon at all then.

So, it is clear you need to set Application.MainFormOnTaskbar := False in the project file.

Furthermore, the following combination of methods seems to work as you want:

Unit1/Form1/MainForm:

procedure TForm1.CloseButtonClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.OpenForm2ButtonClick(Sender: TObject);
begin
  TForm2.Create(Self).Show;
  Hide;
end;

Unit2/Form2:

procedure TForm2.CloseButtonClick(Sender: TObject);
begin
  Close;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  TForm(Owner).Show;
  Action := caFree;
end;

procedure TForm2.OpenForm3ButtonClick(Sender: TObject);
begin
  TForm3.Create(Self).Show;
  Hide;
end;

Unit3/Form3:

procedure TForm3.CloseButtonClick(Sender: TObject);
begin
  Close;
end;

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  TForm(Owner).Show;
  Action := caFree;
end;

Note that the caption of the taskbar button remains the same during these changes. If you want to synchronize that with the caption of the form being shown, set Application.Title.

Related Topic