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.

4

3 回答 3

7

好的,既然你想要什么已经很清楚了,首先要做两件事:

  • 创建的第一个表单自动成为 MainForm,
  • 应用程序不能没有 MainForm;当 MainForm 关闭时,应用程序关闭,无论显示什么其他形式,
  • 您可以隐藏 MainForm,
  • 默认情况下(无论如何在较旧的 Delphi 版本中)应用程序的窗口显示在任务栏 ( Application.MainFormOnTaskbar = False) 上。只要应用程序处于活动状态,并且至少显示一个表单,此图标/窗口就会显示在任务栏中。
  • 当 时Application.MainFormOnTaskbar = True,MainForm 的图标/窗口显示在任务栏中。当 MainForm 被隐藏时,图标消失。显示另一个表单不会导致另一个任务栏图标/窗口,因此根本没有图标。

因此,很明显您需要Application.MainFormOnTaskbar := False在项目文件中进行设置。

此外,以下方法组合似乎可以按您的意愿工作:

Unit1/Form1/MainForm:

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

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

单元 2/表格 2:

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;

单元 3/表格 3:

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

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

请注意,在这些更改期间,任务栏按钮的标题保持不变。如果您想将其与正在显示的表单的标题同步,请设置Application.Title.

于 2013-10-30T18:07:40.573 回答
5

您可以在任务栏上显示的手柄之间切换。
显示隐藏的应用程序和显示的表单。

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;
于 2013-10-26T07:29:29.043 回答
3

假设Form1是主窗体并Form1.Hide做你想做的一切,除了任务栏按钮应该保持可见,那么你真正想做的就是最小化应用程序:

Application.Minimize;

否则,您可能正在寻找Hide the Main Form

于 2013-10-26T08:07:47.927 回答