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-28

はじめに

ジャンプリスト(Jump List) とはタスクバーやスタートメニューにあるアプリのアイコンを「右クリック」したときに表示されるコンテキストメニューのことです。

アプリケーションを起動していない時にも使えるので、タスクバーにピン留めして使うと効果的です。

image.png

ジャンプリストには次のような機能があります。

  • ファイルをピン留め可能な [最近使った項目] (拡張子の関連付けが必要)
  • ファイルをピン留め可能な [よく使う項目] (拡張子の関連付けが必要)
  • [タスク]
  • カスタムタスク
  • [タスクバーにピン留めする]
  • [ウィンドウを閉じる]

ジャンプリストは Windows 7 から使えます。

TJumpList の使い方

Delphi の VCL フレームワークには TJumpList というジャンプリストコンポーネントが用意されています 1。

image.png

TJumpList をメインフォームあるいはデータモジュールに貼り付けて使います。

ジャンプリストそのものはメインフォーム (あるいはデータモジュール) の OnCreate イベントハンドラで動的に作成する事になるかと思います。

TJumpList の使用例

使用例です。ジャンプリストの設定はコードで書いた方が簡単です。

  // パラメータ設定
  JumpList1.ApplicationID := 'MyCompany.MyProduct.1'; // アプリケーション ID
  JumpList1.AutoRefresh   := True;                    // 自動更新
  JumpList1.ShowRecent    := True;                    // [最近使った項目] を表示
  JumpList1.ShowFrequent  := False;                   // [よく使う項目] を表示
  // [タスク] カテゴリに追加
  JumpList1.AddTask('新規作成', ParamStr(0), '/new' , ParamStr(0));
  JumpList1.AddTaskSeparator;
  JumpList1.AddTask('編集'    , ParamStr(0), '/edit', ParamStr(0));
  // [カスタムタスク] カスタムカテゴリを追加
  var TaskCategory := TJumpCategoryItem(JumpList1.CustomCategories.Add);
  TaskCategory.CategoryName := 'カスタムタスク';
  // タスクアイテムを追加 (新規)
  with TJumpListItem(TaskCategory.Items.Add) do
  begin
    FriendlyName := '新規作成';
    Path := ParamStr(0); // 自分自身
    Arguments := '/new'; // 新規
    Icon := ParamStr(0); // 自分自身のアイコンを使う
  end;
  // タスクアイテムを追加 (編集)
  with TJumpListItem(TaskCategory.Items.Add) do
  begin
    FriendlyName := '編集';
    Path := ParamStr(0);  // 自分自身
    Arguments := '/edit'; // 編集
    Icon := ParamStr(0);  // 自分自身のアイコンを使う
  end;
  // ジャンプリストを有効化
  JumpList1.Enabled := True;

ApplicationID プロパティで指定可能な AppUserModelID (AUMID) はアプリケーション固有のものを指定する必要があります 2。

未指定でも動作する (内部で自動生成される) のですが、トラブルを避けるため明示的に指定しておいたほうがいいでしょう。

上記コードで初期化するとジャンプリストは次のようになります。

image.png

注意点もあります。

  • アイコンをインデックスで指定できない (内部で 0 固定になっている)
  • セパレーターは組み込み [タスク] でしか使えない (カスタムタスクでは使えない)

[最近使った項目] に追加する

[最近使った項目] に追加するには、クラスメソッド AddToRecent() をファイルオープン時に呼び出します。

TJumpList.AddToRecent('フルパスファイル名');

アプリケーションがファイルを TOpenDialog を利用して開いた場合にはこの操作は不要です。

[よく使う項目] は [最近使った項目] のリストの中から繰り返し使われるものが自動で選ばれます。特に操作は不要です。[最近使った項目] と [よく使う項目] はどちらか片方の利用が推奨されています。

[最近使った項目] を削除する

[最近使った項目] をすべてあるいは一部削除するためのクラスメソッドが用意されています。

  class function RemoveFromRecent(const Path: string; const AppModelID: string): Boolean; overload;
  class function RemoveFromRecent(const ShellItem: IUnknown; const AppModelID: string): Boolean; overload;
  class function RemoveFromRecent(const JumpItem: TJumpListItem; const AppModelID: string): Boolean; overload;
  class function RemoveAllFromRecent(const AppModelID: string): Boolean;

[最近使った項目] をすべて削除するには次のようなコードを記述します。

  JumpList1.RemoveAllFromRecent(JumpList1.ApplicationID);

FireMonkey でジャンプリストを表示する

Vcl.JumpList は FireMonkey でもそのまま使えます。ユニット内部で使っている VCL 関連ユニットも Vcl.Consts だけです。

FireMonkey 環境下では TJumpList をメインフォームあるいはデータモジュールに貼り付ける事が出来ませんが、手間はそれほど掛かりません。以下使用例です。

uses
  ..., Vcl.JumpList;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  // JumpList1 を作成 (追加)
  var JumpList1 := TJumpList.Create(Self);

  // パラメータ設定
  JumpList1.ApplicationID := 'MyCompany.MyProduct.1'; // アプリケーション ID
  JumpList1.AutoRefresh   := True;                    // 自動更新
  JumpList1.ShowRecent    := True;                    // [最近使った項目] を表示
  JumpList1.ShowFrequent  := False;                   // [よく使う項目] を表示
  // [タスク] カテゴリに追加
  JumpList1.AddTask('新規作成', ParamStr(0), '/new' , ParamStr(0));
  JumpList1.AddTaskSeparator;
  JumpList1.AddTask('編集'    , ParamStr(0), '/edit', ParamStr(0));
  // [カスタムタスク] カスタムカテゴリを追加
  var TaskCategory := TJumpCategoryItem(JumpList1.CustomCategories.Add);
  TaskCategory.CategoryName := 'カスタムタスク';
  // タスクアイテムを追加 (新規)
  with TJumpListItem(TaskCategory.Items.Add) do
  begin
    FriendlyName := '新規作成';
    Path := ParamStr(0); // 自分自身
    Arguments := '/new'; // 新規
    Icon := ParamStr(0); // 自分自身のアイコンを使う
  end;
  // タスクアイテムを追加 (編集)
  with TJumpListItem(TaskCategory.Items.Add) do
  begin
    FriendlyName := '編集';
    Path := ParamStr(0);  // 自分自身
    Arguments := '/edit'; // 編集
    Icon := ParamStr(0);  // 自分自身のアイコンを使う
  end;
  // ジャンプリストを有効化
  JumpList1.Enabled := True;

  // ジャンプリストを更新 (追加)
  JumpList1.UpdateList;
end;

違いはインスタンスの生成と UpdateList() の呼び出しだけです。

Windows プラットフォーム依存なのでこうなっているのでしょうが、System.Win.JumpList とかでよかった気が...

独自のジャンプリストクラス

XE6 以前でも使えるフレームワーク非依存のクラスを作ってみました。XE6 以前でしか使うメリットがないので、インライン変数宣言等は使っていません。

Win.Taskbar.JumpList
unit Win.Taskbar.JumpList;

interface

uses
  System.Classes, System.SysUtils, System.Win.ComObj, Winapi.Windows, Winapi.ActiveX,
  Winapi.ShlObj, Winapi.PropSys, Winapi.ObjectArray, Winapi.PropKey;

type
  TSimpleJumpList = class(TComponent)
  private
    FApplicationID: string;
    FShowFrequent: Boolean;
    FShowRecent: Boolean;
    FTasks: IObjectCollection;
    function CreateShellLink(const Title, Path, Arguments, IconPath: string;
      IconIndex: Integer; IsSeparator: Boolean = False): IShellLink;
    procedure SetApplicationID(const Value: string);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure AddTask(const Title, Path: string; const Arguments: string = '';
      const IconPath: string = ''; IconIndex: Integer = 0);
    procedure AddTaskSeparator;
    class procedure AddToRecent(const FilePath: string); static;
    function DeleteList: Boolean;
    class function RemoveAllFromRecent(const AppModelID: string = ''): Boolean; static;
    procedure UpdateList;
    property ApplicationID: string read FApplicationID write SetApplicationID;
    property ShowFrequent: Boolean read FShowFrequent write FShowFrequent default False;
    property ShowRecent: Boolean read FShowRecent write FShowRecent default False;
  end;

implementation

{ TSimpleJumpList }

constructor TSimpleJumpList.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FApplicationID := '';
  FShowRecent := False;
  FShowFrequent := False;
  CoCreateInstance(CLSID_EnumerableObjectCollection, nil, CLSCTX_INPROC_SERVER, IObjectCollection, FTasks);
end;

function TSimpleJumpList.DeleteList: Boolean;
var
  DestList: ICustomDestinationList;
begin
  if Failed(CoCreateInstance(CLSID_DestinationList, nil, CLSCTX_INPROC_SERVER, ICustomDestinationList, DestList)) then
    Exit(False);
  if FApplicationID <> '' then
    Result := Succeeded(DestList.DeleteList(PChar(FApplicationID)))
  else
    Result := Succeeded(DestList.DeleteList(nil));
end;

destructor TSimpleJumpList.Destroy;
begin
  FTasks := nil;
  inherited Destroy;
end;

class function TSimpleJumpList.RemoveAllFromRecent(
  const AppModelID: string): Boolean;
var
  AppDests: IApplicationDestinations;
begin
  if Failed(CoCreateInstance(CLSID_ApplicationDestinations, nil, CLSCTX_INPROC_SERVER, IApplicationDestinations, AppDests)) then
    Exit(False);
  if AppModelID <> '' then
    AppDests.SetAppID(PChar(AppModelID));
  Result := Succeeded(AppDests.RemoveAllDestinations);
end;

procedure TSimpleJumpList.AddTask(const Title, Path, Arguments,
  IconPath: string; IconIndex: Integer);
var
  Link: IShellLink;
begin
  if not Assigned(FTasks) then
    Exit;
  Link := CreateShellLink(Title, Path, Arguments, IconPath, IconIndex);
  if Assigned(Link) then
    FTasks.AddObject(Link);
end;

procedure TSimpleJumpList.AddTaskSeparator;
var
  Link: IShellLink;
begin
  if not Assigned(FTasks) then
    Exit;
  Link := CreateShellLink('', '', '', '', 0, True);
  if Assigned(Link) then
    FTasks.AddObject(Link);
end;

class procedure TSimpleJumpList.AddToRecent(const FilePath: string);
begin
  SHAddToRecentDocs(SHARD_PATHW, PChar(FilePath));
end;

function TSimpleJumpList.CreateShellLink(const Title, Path, Arguments,
  IconPath: string; IconIndex: Integer; IsSeparator: Boolean): IShellLink;
var
  LPath, LIconPath: string;
  PropStore: IPropertyStore;
  PropVar: TPropVariant;
  ShellLink: IShellLink;
begin
  CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, ShellLink);
  PropStore := ShellLink as IPropertyStore;
  if IsSeparator then
  begin
    InitPropVariantFromBoolean(True, PropVar);
    PropStore.SetValue(PKEY_AppUserModel_IsDestListSeparator, PropVar);
    PropVariantClear(PropVar);
  end
  else
  begin
    LPath := Path;
    if LPath = '' then
      LPath := ParamStr(0);
    LIconPath := IconPath;
    if LIconPath = '' then
      LIconPath := LPath;
    ShellLink.SetPath(PChar(LPath));
    if Arguments <> '' then
      ShellLink.SetArguments(PChar(Arguments));
    ShellLink.SetIconLocation(PChar(LIconPath), IconIndex);
    InitPropVariantFromString(PChar(Title), PropVar);
    PropStore.SetValue(PKEY_Title, PropVar);
    PropVariantClear(PropVar);
    if FApplicationID <> '' then
    begin
      InitPropVariantFromString(PChar(FApplicationID), PropVar);
      PropStore.SetValue(PKEY_AppUserModel_ID, PropVar);
      PropVariantClear(PropVar);
    end;
  end;
  PropStore.Commit;
  Result := ShellLink;
end;

procedure TSimpleJumpList.SetApplicationID(const Value: string);
begin
  if FApplicationID = Value then
    Exit;
  FApplicationID := Value;
  if FApplicationID <> '' then
    SetCurrentProcessExplicitAppUserModelID(PChar(FApplicationID));
end;

procedure TSimpleJumpList.UpdateList;
var
  DestList: ICustomDestinationList;
  MaxSlots: UINT;
  RemovedObjs: IObjectArray;
  TaskCount: Cardinal;
begin
  if not Assigned(FTasks) then
    Exit;
  CoCreateInstance(CLSID_DestinationList, nil, CLSCTX_INPROC_SERVER, ICustomDestinationList, DestList);
  if FApplicationID <> '' then
    DestList.SetAppID(PChar(FApplicationID));
  DestList.BeginList(MaxSlots, IID_IObjectArray, RemovedObjs);
  if FShowRecent then
    DestList.AppendKnownCategory(KDC_RECENT);
  if FShowFrequent then
    DestList.AppendKnownCategory(KDC_FREQUENT);
  var TaskArray := FTasks as IObjectArray;
  if Succeeded(TaskArray.GetCount(TaskCount)) and (TaskCount > 0) then
    DestList.AddUserTasks(TaskArray);
  DestList.CommitList;
end;
end.
  • アイコンインデックスの指定が可能です
  • カスタムタスクは作れません

XE7 以降から Vcl.JumpList.pas をコピーして持ってくれば...(ゴニョゴニョ)

使い方

使い方はこんな感じです。メソッドやプロパティは本家に寄せてあります。

  var SJL := TSimpleJumpList.Create(Self);
  SJL.ApplicationID := 'MyCompany.MyProduct.1';
  SJL.ShowRecent   := True;
  SJL.ShowFrequent := False;
  SJL.AddTask('新規作成', ParamStr(0), '/new',  ParamStr(0), 0);
  SJL.AddTaskSeparator;
  SJL.AddTask('編集'    , ParamStr(0), '/edit', ParamStr(0), 0);
  SJL.UpdateList;

おわりに

うまく使えると便利かと思います。

複数のジャンプリストを用意しておき、起動時オプションによって表示するジャンプリストを切り替える事もできます。

See also:

索引

[ ↑ 目次へ ]

  1. Delphi XE7 以降で利用可能です。 ↩

  2. バージョン情報にある ProgramID とは無関係です。同一のものにする必要はありません。 ↩

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?