Windows 7 全新任務列整合了應用啟動、視窗切換、常用任務存取、狀態回報與遠端控制等核心使用情境。對開發者而言,善用任務列 API 能讓應用在使用者眼中脫穎而出,提供「不切換視窗即可操作」的高效體驗。
範例專案包含多個分頁,每個對應一項任務列功能。使用 TaskbarManager.Instance 靜態類別存取所有功能。
在任務列按鈕右下角顯示 16x16 狀態圖示(如 Messenger 線上狀態、Outlook 新郵件)。
if (ShowOverlay.IsChecked.Value) {
Icon icon = iconsList.SelectedItem as Icon;
if (icon != null)
TaskbarManager.Instance.SetOverlayIcon(icon, "icon" + iconsList.SelectedIndex);
} else {
TaskbarManager.Instance.SetOverlayIcon(null, null);
}
直接在任務列按鈕上顯示進度與狀態(正常/錯誤/暫停/不確定),如檔案複製、下載進度。
if (ShowProgressBar.IsChecked.Value) {
TaskbarManager.Instance.SetProgressValue((int)progressSlider.Value, 100);
TaskbarManager.Instance.SetProgressState((TaskbarProgressBarState)ProgressStateSelection.SelectedItem);
} else {
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
}
狀態包含:Normal(綠)、Error(紅)、Paused(黃)、Indeterminate(循環動畫)。
滑鼠懸停任務列按鈕顯示的即時縮圖上方,附加最多 7 個工具列按鈕,實現遠端控制(如 Media Player 播放控制)。
buttonFirst = new ThumbnailToolbarButton(Resources.first, "First Image");
buttonFirst.Enabled = false;
buttonFirst.Click += buttonFirst_Click;
// ... 建立 Previous, Next, Last 按鈕
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(
new WindowInteropHelper(Application.Current.MainWindow).Handle,
buttonFirst, buttonPrevious, buttonNext, buttonLast);
按鈕啟用/停用狀態可動態更新。
預設縮圖顯示整個視窗。透過 TabbedThumbnail 可:
TabbedThumbnailBitmapRequested 事件回傳即時影像InvalidatePreview() 更新Vector offset = Utilities.GetOffset(mainWindow, trickyBorder);
TabbedThumbnail preview = new TabbedThumbnail(mainWindow, trickyBorder, offset);
preview.TabbedThumbnailBitmapRequested += OnThumbnailRequested;
preview.Tooltip = "按需生成的縮圖";
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
不想自繪縮圖,只想顯示視窗特定矩形區域(如文字編輯器游標附近幾行):
Rectangle clipRect = new Rectangle((int)offset.X, (int)offset.Y,
(int)mediaElement.ActualWidth, (int)mediaElement.ActualHeight);
TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(mainWindowHandle, clipRect);
右鍵任務列按鈕顯示的選單,支援三類內容:
Recent(最近開啟)或 Frequent(常用),需檔案類型關聯string sys = Environment.GetFolderPath(Environment.SpecialFolder.System);
JumpList.AddUserTasks(
new JumpListLink(Path.Combine(sys, "notepad.exe"), "開啟記事本") { IconReference = new IconReference(Path.Combine(sys, "notepad.exe"), 0) },
new JumpListLink(Path.Combine(sys, "calc.exe"), "開啟計算機") { IconReference = new IconReference(Path.Combine(sys, "calc.exe"), 0) },
new JumpListSeparator(),
new JumpListLink(Path.Combine(sys, "mspaint.exe"), "開啟繪圖") { IconReference = new IconReference(Path.Combine(sys, "mspaint.exe"), 0) }
);
JumpList.Refresh();
JumpList.KnownCategoryToDisplay = showRecent.IsChecked.Value
? JumpListKnownCategoryType.Recent
: JumpListKnownCategoryType.Frequent;
JumpList.Refresh();
_currentCategory = new JumpListCustomCategory("重要");
JumpList.AddCustomCategories(_currentCategory);
JumpList.Refresh();
// 加入 ShellItem(僅檔案路徑)
_currentCategory.AddJumpListItems(new JumpListItem(GetTempFileName("活動報告")));
JumpList.Refresh();
// 加入 ShellLink(含參數、圖示等捷徑資訊)
_currentCategory.AddJumpListItems(new JumpListLink(GetTempFileName("連結項目"), "連結標題"));
JumpList.Refresh();
Windows 7 任務列 API 讓應用程式突破視窗邊界,直接在使用者最常關注的任務列上提供狀態回報、遠端控制、內容預覽與快速任務存取。善用 Windows API Code Pack,.NET 開發者能以極少代碼整合這些強大功能,大幅提升應用的專業度與使用者黏著度。