Search code examples
delphiwinapitaskbarsystem-traydelphi-10.4-sydney

How to hide my TaskBar icon from the TaskBar at run-time?


I have downloaded CoolTrayIcon v.4.4.0 from:

https://torry.net/files/vcl/system/trayicons/CoolTrayIcon.zip

After installing the component in Delphi 10.4.2, in the demo folder, I have opened the project "CoolTrayTest". In this project, in a button click-handler I execute this code:

if IsWindowVisible(Application.Handle) then
  ShowWindow(Application.Handle, SW_HIDE);

This HIDES the TaskBar icon.

But when I try this code in my own VCL Application, the TaskBar icon is NOT hidden.

What can I do to have my own VCL Application also hide the TaskBar icon with this code? (I don't want to hide the TaskBar icon from the program start on, but deliberately with the click of a button).

EDIT: Following the suggestion of Remy, I use this code:

procedure TForm1.btnTestClick(Sender: TObject);
var
  T: System.Win.Taskbar.TWinTaskbar;
begin
  T := System.Win.Taskbar.TWinTaskbar.Create;
  try
    if not FTaskBarButtonIsHidden then
    begin
      IsOK := T.DeleteTab(Self.Handle);
      CodeSite.Send('TForm1.btnTestClick: DeleteTab', IsOK);
      FTaskBarButtonIsHidden := IsOK;
    end
    else
    begin
      IsOK := T.AddTab(Self.Handle);
      CodeSite.Send('TForm1.btnTestClick: AddTab', IsOK);
      FTaskBarButtonIsHidden := not IsOK;
    end;
  finally
    T.Free;
  end;
end;

It seems to work. Does this have any side-effects?


Solution

  • When the Application.ShowMainFormOnTaskbar property is False, as is likely the case in the CoolTrayIcon demo, then the Taskbar button is owned by the hidden Application window.

    When ShowMainFormOnTaskbar is True instead, as is the case by default in modern Delphi projects, then the Taskbar button is owned by the Application.MainForm window rather than the Application window.

    To hide/show the Taskbar button, you need to hide/show its owner window.

    Alternatively, you can use the DeleteTab() and AddTab() methods of the Win32 ITaskbarList Shell interface.

    See the documentation about The Taskbar for more details. Particularly the sections on "Managing Taskbar Buttons" and "Modifying the Contents of the Taskbar".