WinFormアプリでJumpListを操作する

世の中便利になったもので、Windows APIを呼ぶにも
Windows API Code Packなるものを使えば
C#からあんなことやそんなこともできてしまうそうですよ。

というわけでWindows 7も近いことですし、C#のWinFormアプリでJumpListなど使ってみようという話です。

ちなみにWindows API Code PackはMS独自のライセンスで提供されているので
勝手にアプリケーションへ組み込む前に確認しておかないといけないですね。

  1. 先のWindows API Code Packをダウンロードして適当なディレクトリに展開します。
  2. Windows API Code Packはソースコードの形での提供なので、徐にビルドを実行します。
    • ビルドの対象は WindowsAPICodePack/WindowsAPICodePack.sln です。
    • 一応DebugとRelease両方ビルドします。
  3. WindowsAPICodePack/Shell/bin/Release以下の次のファイルが目当てのDLLです:
  4. とりあえずビルドしたファイルはさておき、いよいよWinFormアプリを作成します。
    1. Visual Studio 2008を起動して、
    2. [File]-[New]-[Project...]を選択、
    3. [New Project]ウィンドウで[Visual C#]-[Windows]と[Windows Forms Application]を選択、
    4. [Name]は適当に「MyJumpList」とでもつけます。[Create directory for solution]は好みに合わせてチェックを外すもよし。
    5. [OK]をクリック。
    6. Form1が表示されたならばPropertiesウィンドウにて雷のEventsアイコンを押してイベント一覧に切り替えて、
    7. [Shown]の行をダブルクリック。
    8. コードを書く前に参照設定します。
      1. Solution Explorerのプロジェクトツリーにある[References]を右クリックして[Add Reference...]を選択、
      2. [Add Reference]ウィンドウで[Browse]タブを選択、先ほどビルドしたWindows API Code Packの2つのDLLを選択します。DebugとReleaseどちらでもいいと思いますがとりあえずDebug版でもいいと思います:
      3. [OK]で閉じます。
  5. そしてコードを書きます:
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace MyJumpList
{
    public partial class Form1 : Form
    {
        private JumpList myList;

        private TaskbarManager windowsTaskbar = TaskbarManager.Instance;

        public Form1()
        {
            InitializeComponent();
            windowsTaskbar.ApplicationId = "app1";
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);

            myList = JumpList.CreateJumpList();

            myList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "notepad.exe"), "Open Notepad")
            {
                IconReference = new IconReference(Path.Combine(systemFolder, "notepad.exe"), 0)
            });
        }
    }
}

このコードをF5キーでビルドしつつ実行するとJumpListが表示されたことと思います。

JumpList.CreateJumpList()を呼んでJumpListのインスタンスを作成した後に、
AddUserTasksを呼んでやるだけでJumpListの項目が増えるのかなと思いつつ、
実はTaskbarManagerがいないとうまく動きません。

先のWindowsAPICodePack.slnに戻ってコードを眺めてみることには、
Shell/Taskbar/JumpList.csに次のコードが見つかります:

        /// <summary>
        /// Create a JumpList for the application's taskbar button.
        /// </summary>
        /// <returns>A new JumpList that is associated with the app id of the main application window</returns>
        /// <remarks>If there are any other child (top-level) windows for this application and they don't have
        /// a specific JumpList created for them, they all will share the same JumpList as the main application window.
        /// In order to have a individual JumpList for a top-level window, use the overloaded method CreateJumpListForIndividualWindow.</remarks>
        public static JumpList CreateJumpList()
        {
            return new JumpList(TaskbarManager.Instance.ApplicationId);
        }

曰く、
「メインウィンドウとアプリケーションIDで識別されるJumpListのインスタンスを作成します。
ただし独自のJumpListが結びつけられていない(トップレベルの)子ウィンドウがいる場合、
それらにはメインウィンドウと同じJumpListが関連づけられます」
というコメントがありつつ、JumpListのコンストラクタにTaskbarManagerのApplicationIdが渡されています。

続く。