简体   繁体   English

如何在运行时从任务栏中隐藏我的任务栏图标?

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

I have downloaded CoolTrayIcon v.4.4.0 from:我已从以下位置下载 CoolTrayIcon v.4.4.0:

https://torry.net/files/vcl/system/trayicons/CoolTrayIcon.zip 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".在 Delphi 10.4.2 中安装组件后,在演示文件夹中,我打开了项目“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.但是当我在自己的 VCL 应用程序中尝试此代码时,任务栏图标并未隐藏。

What can I do to have my own VCL Application also hide the TaskBar icon with this code?我该怎么做才能让我自己的 VCL 应用程序也使用此代码隐藏任务栏图标? (I don't want to hide the TaskBar icon from the program start on, but deliberately with the click of a button). (我不想从程序开始隐藏TaskBar图标,而是故意点击一个按钮)。

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?这有什么副作用吗?

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.Application.ShowMainFormOnTaskbar属性为False时,就像 CoolTrayIcon 演示中的情况一样,任务栏按钮归隐藏的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.ShowMainFormOnTaskbarTrue时,如现代 Delphi 项目中的默认情况,则任务栏按钮归Application.MainForm window 而不是Application window 所有。

To hide/show the Taskbar button, you need to hide/show its owner window.要隐藏/显示任务栏按钮,您需要隐藏/显示其所有者 window。

Alternatively, you can use the DeleteTab() and AddTab() methods of the Win32 ITaskbarList Shell interface.或者,您可以使用 Win32 ITaskbarList Shell 接口的DeleteTab()AddTab()方法。

See the documentation about The Taskbar for more details.有关更多详细信息,请参阅有关任务栏的文档。 Particularly the sections on "Managing Taskbar Buttons" and "Modifying the Contents of the Taskbar".特别是“管理任务栏按钮”和“修改任务栏内容”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM