1

In a Delphi 10.4.2 Win32 VCL Application, I use this code to hide or show the application's TaskBar icon:

private
  { Private declarations }
  FTaskBarButtonIsHidden: Boolean;

procedure TForm1.btnTestClick(Sender: TObject);
var
  T: System.Win.Taskbar.TWinTaskbar;
  IsOK: Boolean;
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;

But it seems there is no way to CHECK whether the TaskBar Icon of my application is VISIBLE OR NOT.

I tried Winapi.Windows.IsWindowVisible(Self.Handle), but it does not work for this purpose: It always gives back True, independently of whether the TaskBar Icon is visible or not.

How to CHECK whether the TaskBar Icon of my application is visible or not?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 2
    A quick look at the [Microsoft Windows docs](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-itaskbarlist) suggests that it isn't possible to query this status using this interface or any of its current extensions. Also see this [old discussion](https://stackoverflow.com/questions/2262726/determining-if-a-window-has-a-taskbar-button). Why do you need this? It's your app and you seem to be controlling its taskbar button. Why not keep state yourself? (Also: why do you need this information at all? Perhaps your underlying problem has a better solution?) – Andreas Rejbrand Mar 21 '21 at 15:01
  • @AndreasRejbrand Yes, I keep the state myself with the `FTaskBarButtonIsHidden` variable, and there is no problem with it. But I believe (from experience) that querying the reality is slightly better than trusting my own variables. Isn't it? (I have stopped in blindly believing in determinism). – user1580348 Mar 21 '21 at 15:10
  • That depends. If you can mathematically guarantee that your internal variables are always correct, there's no problem in using them. If not, you might have a problem in any case! Still, if you really do need to know this particular information at some particular point in time and there must not be an error, I agree it would feel safer to simply ask the OS. For instance, a future version of the OS might change your tabs behind your back. But in any case, I get the feeling that you might be up to something that you shouldn't do. – Andreas Rejbrand Mar 21 '21 at 15:14
  • A crucial point here is that the taskbar doesn't belong to your app: it belongs to the system, and you are only invited to suggest some specific changes to your part of it. – Andreas Rejbrand Mar 21 '21 at 15:17

0 Answers0