0

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.

Galvion
  • 1,353
  • 7
  • 23
  • 35
  • 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 Oct 26 '13 at 05:06
  • 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 Oct 27 '13 at 11:54
  • 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 Oct 28 '13 at 21:33
  • 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 Oct 30 '13 at 17:30
  • I cannot look at this until tomorrow, but why don't you just minimize the form? – David Heffernan Oct 30 '13 at 17:58

3 Answers3

7

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.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • Great it works :) the key is on Application.MainFormOnTaskbar := False ... I was mistaken with True... :P – Galvion Oct 30 '13 at 22:33
  • anyway how can I change the .tag value for TForm(Owner).Show; ? – Galvion Nov 10 '13 at 06:50
  • Add a variable for the form? That code of mine is just a quick and dirty example which you of course can modify to your needs. – NGLN Nov 11 '13 at 08:53
  • Yes, change the Form1.tag when closing the Form2. Should I use " TForm(Owner).Tag := 9; " OR " Form1.Tag := 9; " OR any other method ? – Galvion Nov 14 '13 at 00:52
  • I'm unsure about what you want but I think you are trying to ask a new question here. Please ask a new question, then we all can respond/help. – NGLN Nov 14 '13 at 05:05
5

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;
bummi
  • 27,123
  • 14
  • 62
  • 101
3

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.

Community
  • 1
  • 1
NGLN
  • 43,011
  • 8
  • 105
  • 200
  • What about if I add the button to show the Form2 ? your code work like what I wanted, only if I work with one form. When I work with with more than one form, I will the minimize the whole form... how to minimize or hide the other forms, while keep the latest form visible ? – Galvion Oct 27 '13 at 22:55
  • @SidhiCiang I really wish that you would edit the question as I asked in my second comment to the question. I feel that for sure we could give you the answer you need if you could tell us more about the context. – David Heffernan Oct 28 '13 at 11:19