WinFormアプリでJumpListを操作する
世の中便利になったもので、Windows APIを呼ぶにも
Windows API Code Packなるものを使えば
C#からあんなことやそんなこともできてしまうそうですよ。
というわけでWindows 7も近いことですし、C#のWinFormアプリでJumpListなど使ってみようという話です。
ちなみにWindows API Code PackはMS独自のライセンスで提供されているので
勝手にアプリケーションへ組み込む前に確認しておかないといけないですね。
- 先のWindows API Code Packをダウンロードして適当なディレクトリに展開します。
- Windows API Code Packはソースコードの形での提供なので、徐にビルドを実行します。
- ビルドの対象は WindowsAPICodePack/WindowsAPICodePack.sln です。
- 一応DebugとRelease両方ビルドします。
- WindowsAPICodePack/Shell/bin/Release以下の次のファイルが目当てのDLLです:
- とりあえずビルドしたファイルはさておき、いよいよWinFormアプリを作成します。
- Visual Studio 2008を起動して、
- [File]-[New]-[Project...]を選択、
- [New Project]ウィンドウで[Visual C#]-[Windows]と[Windows Forms Application]を選択、
- [Name]は適当に「MyJumpList」とでもつけます。[Create directory for solution]は好みに合わせてチェックを外すもよし。
- [OK]をクリック。
- Form1が表示されたならばPropertiesウィンドウにて雷のEventsアイコンを押してイベント一覧に切り替えて、
- [Shown]の行をダブルクリック。
- コードを書く前に参照設定します。
- そしてコードを書きます:
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が渡されています。
続く。