2

I want my application to minimize to the system tray, and not be visible on the taskbar. I followed the suggestions from this and this answer and changed the MainFormOnTaskBar property in the project source:

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.MainFormOnTaskBar := False;
  Application.Run;
end.

Next I tried this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Self.Hide;
  WindowState := wsMinimized;
  TrayIcon1.Visible := True;
end;

and this variant:

procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
  Self.Hide;
  WindowState := wsMinimized;
  TrayIcon1.Visible := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Minimize;
end;

but while the tray icon shows correctly the application still shows in the taskbar. What am I doing wrong?

Community
  • 1
  • 1
Joris Groosman
  • 771
  • 8
  • 23

1 Answers1

2

David suggests that what I see in the taskbar is not my main form, but my application. Following his advice I hid that using ShowWindow:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Self.Hide;
  WindowState := wsMinimized;
  TrayIcon1.Visible := True;

  ShowWindow(Application.Handle, SW_Hide);
end;

Problem solved. Thanks, David.

Joris Groosman
  • 771
  • 8
  • 23
  • When `Application.MainFormOnTaskBar` is True, the `Application.MainForm` window controls the taskbar button (amongst other things). When False instead, the `Application` window controls the taskbar button. You should strive to leave `MainFormOnTaskbar` to True, it is really needed for proper OS interactions on Vista+, setting it to False has MANY subtle consequences throughout the VCL, as MANY things have a (misguided) dependency on `MainFormOnTaskbar` being True. You should not have to set it to False just to accomplish what you are asking for. – Remy Lebeau Jan 13 '16 at 17:45