首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 7 任務列整合開發實戰:進度列、覆蓋圖示、縮圖工具列、動態預覽與 Jump List

     編輯:關於.NET

前言:任務列成為應用程式的新前門

Windows 7 全新任務列整合了應用啟動、視窗切換、常用任務存取、狀態回報與遠端控制等核心使用情境。對開發者而言,善用任務列 API 能讓應用在使用者眼中脫穎而出,提供「不切換視窗即可操作」的高效體驗。

開發環境需求

  • Visual Studio 2008 SP1
  • Windows 7 RC 或更新版本
  • Windows API Code Pack 0.90+(.NET 封裝庫)
  • 支援 Aero 的顯示卡

實驗專案:TaskbarConcepts (WPF/C#)

範例專案包含多個分頁,每個對應一項任務列功能。使用 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);

功能六:Jump List(跳躍清單)

右鍵任務列按鈕顯示的選單,支援三類內容:

  • 使用者任務:應用程式自訂命令(如 Messenger 狀態切換)
  • 系統分類: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 開發者能以極少代碼整合這些強大功能,大幅提升應用的專業度與使用者黏著度。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved