2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Delphi】タスクバーアイコンにオーバーレイアイコンを表示する

2
Last updated at Posted at 2026-10-16

はじめに

たまにアプリケーションのタスクバーアイコンに小さなアイコンが表示されていると思うのですが、これを Delphi 製アプリに実装してみたいと思います。

image.png

正式名称は「オーバーレイアイコン」のようですね。Windows 7 から使えます。

実装

最近の Delphi だと特に難しい事はないです。Winapi.ShlObj を uses に追加すれば ITaskbarList3 が使えます。

オーバーレイアイコンを簡単に使えるようにするため、高度なレコード型で実装してみたいと思います。

ソースコード

以下、ソースコードです。

本ユニットはメインスレッドで COM が初期化されている前提(通常の VCL / FMX アプリ)で動作します。必要であればアプリケーション側で CoInitialize() / CoUninitialize() を記述してください。

Win.Taskbar.Progress.pas
unit Win.Taskbar.Progress;

interface

uses
  System.SysUtils, System.Classes, Winapi.Windows, Winapi.Messages,
  Winapi.ShlObj, Winapi.ActiveX, Winapi.ShellAPI;

type
  TTaskbarOverlay = record
  private
    class var FTaskbarList: ITaskbarList3;
    class function GetTaskbar: ITaskbarList3; static;
    class procedure InternalSetOverlay(aWnd: HWND; aIconHandle: HICON; const aDescription: string); static;
    class function LoadIconFromModule(aModule: HMODULE; const aResName: string): HICON; static;
  public
    class procedure Clear(aWnd: HWND); static;
    class procedure SetImageResIcon(aWnd: HWND; aIconIndex: Integer; const aDescription: string = ''); overload; static;
    class procedure SetImageResIcon(aWnd: HWND; const aResName: string; const aDescription: string = ''); overload; static;
    class procedure SetResourceIcon(aWnd: HWND; const aResName: string; const aDescription: string = ''); static;
    class procedure SetShell32Icon(aWnd: HWND; aIconIndex: Integer; const aDescription: string = ''); overload; static;
    class procedure SetShell32Icon(aWnd: HWND; const aResName: string; const aDescription: string = ''); overload; static;
    class procedure SetStockIcon(aWnd: HWND; aStockIconId: SHSTOCKICONID; const aDescription: string = ''); static;
    class procedure SetSystemIcon(aWnd: HWND; const aDllName: string; aIconIndex: Integer; const aDescription: string = ''); overload; static;
    class procedure SetSystemIcon(aWnd: HWND; const aDllName: string; const aResName: string; const aDescription: string = ''); overload; static;
  end;

implementation

{ TTaskbarOverlay }

const
  IMAGERES_DLLNAME = 'imageres.dll';
  SHELL32_DLLNAME = 'shell32.dll';

class procedure TTaskbarOverlay.Clear(aWnd: HWND);
begin
  if (aWnd = 0) or not IsWindow(aWnd) then
    Exit;
  InternalSetOverlay(aWnd, 0, '');
end;

class function TTaskbarOverlay.GetTaskbar: ITaskbarList3;
begin
  if FTaskbarList = nil then
  begin
    CoCreateInstance(CLSID_TaskbarList, nil, CLSCTX_INPROC_SERVER, ITaskbarList3, FTaskbarList);
    if FTaskbarList <> nil then
      FTaskbarList.HrInit;
  end;
  Result := FTaskbarList;
end;

class procedure TTaskbarOverlay.InternalSetOverlay(aWnd: HWND;
  aIconHandle: HICON; const aDescription: string);
begin
  if (aWnd = 0) or not IsWindow(aWnd) then
  begin
    if aIconHandle <> 0 then
      DestroyIcon(aIconHandle);
    Exit;
  end;
  var lDesc := aDescription;
  TThread.Queue(nil,
    procedure
    begin
      try
        if IsWindow(aWnd) then
        begin
          var lTaskbar := GetTaskbar;
          if lTaskbar <> nil then
            lTaskbar.SetOverlayIcon(aWnd, aIconHandle, PWideChar(lDesc));
        end;
      finally
        if aIconHandle <> 0 then
          DestroyIcon(aIconHandle);
      end;
    end);
end;

class function TTaskbarOverlay.LoadIconFromModule(aModule: HMODULE;
  const aResName: string): HICON;
begin
  if aResName = '' then
    Exit(0);
  var lCleanName := aResName;
  if lCleanName.StartsWith('#') then
    lCleanName := lCleanName.Substring(1);
  var lResId := StrToIntDef(lCleanName, -1);
  var lResPtr: PChar;
  if lResId >= 0 then
    lResPtr := MAKEINTRESOURCE(lResId)
  else
    lResPtr := PChar(aResName);
  Result := LoadImage(aModule, lResPtr, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
end;

class procedure TTaskbarOverlay.SetImageResIcon(aWnd: HWND; aIconIndex: Integer;
  const aDescription: string);
begin
  SetSystemIcon(aWnd, IMAGERES_DLLNAME, aIconIndex, aDescription);
end;

class procedure TTaskbarOverlay.SetImageResIcon(aWnd: HWND; const aResName: string;
  const aDescription: string);
begin
  SetSystemIcon(aWnd, IMAGERES_DLLNAME, aResName, aDescription);
end;

class procedure TTaskbarOverlay.SetResourceIcon(aWnd: HWND;
  const aResName: string; const aDescription: string);
begin
  if (aWnd = 0) or not IsWindow(aWnd) then
    Exit;
  var lSmallIcon := LoadIconFromModule(HInstance, aResName);
  if lSmallIcon <> 0 then
    InternalSetOverlay(aWnd, lSmallIcon, aDescription);
end;

class procedure TTaskbarOverlay.SetShell32Icon(aWnd: HWND; aIconIndex: Integer;
  const aDescription: string);
begin
  SetSystemIcon(aWnd, SHELL32_DLLNAME, aIconIndex, aDescription);
end;

class procedure TTaskbarOverlay.SetShell32Icon(aWnd: HWND;
  const aResName: string; const aDescription: string);
begin
  SetSystemIcon(aWnd, SHELL32_DLLNAME, aResName, aDescription);
end;

class procedure TTaskbarOverlay.SetStockIcon(aWnd: HWND;
  aStockIconId: SHSTOCKICONID; const aDescription: string);
begin
  if (aWnd = 0) or not IsWindow(aWnd) then
    Exit;
  var lSii: TSHStockIconInfo;
  FillChar(lSii, SizeOf(lSii), 0);
  lSii.cbSize := SizeOf(lSii);
  if SUCCEEDED(SHGetStockIconInfo(aStockIconId, SHGSI_ICON or SHGSI_SMALLICON, lSii)) then
    InternalSetOverlay(aWnd, lSii.hIcon, aDescription);
end;

class procedure TTaskbarOverlay.SetSystemIcon(aWnd: HWND;
  const aDllName: string; aIconIndex: Integer; const aDescription: string);
begin
  if (aWnd = 0) or not IsWindow(aWnd) then
    Exit;
  var lLargeIcon: HICON := 0;
  var lSmallIcon: HICON := 0;
  if ExtractIconEx(PChar(aDllName), aIconIndex, lLargeIcon, lSmallIcon, 1) > 0 then
  begin
    if lLargeIcon <> 0 then
      DestroyIcon(lLargeIcon);
    if lSmallIcon <> 0 then
      InternalSetOverlay(aWnd, lSmallIcon, aDescription);
  end;
end;

class procedure TTaskbarOverlay.SetSystemIcon(aWnd: HWND;
  const aDllName, aResName, aDescription: string);
const
  LOAD_LIBRARY_AS_IMAGE_RESOURCE = $00000020;
begin
  if (aWnd = 0) or not IsWindow(aWnd) then
    Exit;
  var lModule := LoadLibraryEx(PChar(aDllName), 0, LOAD_LIBRARY_AS_DATAFILE or LOAD_LIBRARY_AS_IMAGE_RESOURCE);
  if lModule <> 0 then
  begin
    try
      var lSmallIcon := LoadIconFromModule(lModule, aResName);
      if lSmallIcon <> 0 then
        InternalSetOverlay(aWnd, lSmallIcon, aDescription);
    finally
      FreeLibrary(lModule);
    end;
  end;
end;
end.

使い方

Win.Taskbar.Progress を uses に追加すれば使えます。

Application.MainFormOnTaskbar が False の場合には、各メソッドのパラメータとして Self.Handle ではなく Application.Handle を渡してください。

■ DLL 名とリソース識別子 / インデックスから

オーバーレイアイコンを表示する最も基本的なメソッドは フォームのハンドル, DLL 名, リソース名 or リソース ID, 説明文字列 を渡す SetSystemIcon() です。

  TTaskbarOverlay.SetSystemIcon(Self.Handle, 'imageres.dll', '#98', 'エラー');

FindResource() 等に倣い、数値を表す場合には先頭に # を付ける事が可能になっています (省略可能)。また、#$62や $62 は 16進数値として扱われます。

数値だけの文字列はリソース ID、数値以外が含まれるとリソース名として扱われます。記述が煩雑になるので、本記事ではリソース名とリソース ID を合わせてリソース識別子と呼ぶ事にします。

インデックスでアイコンを指定するオーバーロード関数も用意されています。

  TTaskbarOverlay.SetSystemIcon(Self.Handle, 'imageres.dll', 93, 'エラー');

アイコンリソースをインデックスで指定するとリソースエディタで表示される ID とズレる事があります。欠番 (空き番号) が存在するからです。

例えば ImageRes.dll(.mun) にあるエラーアイコンの ID は 98 ですが、インデックスだと 93 になります。

image.png

これをズレないようにするにはリソース識別子で指定します。

指定 型 説明
リソース識別子指定 String アイコンリソースのリソース識別子で指定する。
インデックス指定 Integer アイコンリソース登録順のインデックスで指定する。

■ Shell32.dll と ImageRes.dll のアイコンをリソース識別子 / インデックスから

イチイチ DLL 名を渡すのが面倒だと思ったので、Shell32.dll と ImageRes.dll の DLL 名を省略できるメソッドを追加してあります。

  • SetShell32Icon()
    Shell32.dll からリソース識別子 / インデックスでアイコンを指定する。
  • SetImageResIcon()
    ImageRes.dll からリソース識別子 / インデックスでアイコンを指定する。
  TTaskbarOverlay.SetImageResIcon(Self.Handle, '#98', 'エラー');
  TTaskbarOverlay.SetImageResIcon(Self.Handle, 93, 'エラー');

Shell32.dll / ImageRes.dll のアイコンリソースにリソース名は設定されていません。リソース ID かインデックスでアイコンを指定します。

■ 自 EXE に埋め込まれたアイコンリソースをリソース識別子から

自 EXE に埋め込まれたアイコンリソースをリソース識別子で指定するには SetResourceIcon() を使います。

  TTaskbarOverlay.SetResourceIcon(Self.Handle, 'MY_ICON', '未読あり');

自作アイコンリソースをインデックスで管理する事はないと思うので、インデックスを用いたオーバーロードメソッドは用意されていません。

■ ストックアイコンから

アイコンをシステム DLL から取得する場合、アイコンのリソース ID / インデックスを指定する方法だと Windows のバージョンによってはアイコンのリソース ID / インデックスがズレる可能性があります。これを防止するには SetStockIcon() を使います 1。

  TTaskbarOverlay.SetStockIcon(Self.Handle, SIID_ERROR, 'エラー'):
アイコン 定数 説明
image.png SIID_ERROR エラー
image.png SIID_WARNING 警告
image.png SIID_INFO 情報
image.png SIID_SHIELD 盾 (管理者権限)

SIID_ で始まる定数は Winapi.ShellAPI で定義されています。

■ オーバーレイアイコンを消す

オーバーレイアイコンを消すには、Clear() メソッドを実行します。

  TTaskbarOverlay.Clear(Self.Handle);

おわりに

アイコンを動的作成すれば未読数バッジ的なものも作れます。やってみたい人は...がんばってください (^^;A

See also:

索引

[ ↑ 目次へ ]

  1. サンプルの 4 つだけなら user32.dll から持ってくればズレない気はします。 ↩

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?