0
votes

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.

3
Only visible top level windows show on the taskbar. You put the main form on the taskbar, and so when you hide it, it is removed from taskbar. I should say that setting MainFormOnTaskbar to False should be sufficient. That will use the Application window as the taskbar window.David Heffernan
It would help if you posted real code. For instance, when you said enable := False, there is no enable property. It would also help if you made it clear when the code runs. At startup only, or at a point where the main form is already showing.David Heffernan
Read my comment closely. You want to do this at startup only? Or at a point where the main for is already showing. If you can just make the question clear, you'll get an answer.David Heffernan
OK, I add the rar files (the source code). I want hide the form1 that is already showing, so when I hit the button, it will hide the form1 and show the form2, while keep the application icon on taskbar available.Galvion
I cannot look at this until tomorrow, but why don't you just minimize the form?David Heffernan

3 Answers

7
votes

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.

5
votes

You may toggle between the handles shown on the taskbar.
Show the application on hiding and the form on showing.

procedure TForm1.HideIt;
begin
  Visible := false;
  Application.MainFormOnTaskbar := false;
  ShowWindow(Application.Handle, SW_SHOW);
end;

procedure TForm1.ShowIt;
begin
  Visible := true;
  Application.MainFormOnTaskbar := true;
  ShowWindow(Application.Handle, SW_Hide);
end;

// overrride CreateParams:  procedure CreateParams(var Params: TCreateParams); override;
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.MainFormOnTaskbar := true;
end;
3
votes

Assuming Form1 being the main Form and Form1.Hide doing everything you want, except that the taskbar button should remain visible, then what you really want to do is minimize the application:

Application.Minimize;

Otherwise, you probably are looking for Hide the Main Form.