WPF实现任务栏同步进度

一、概要

本篇文章主要分享使用TaskbarItemInfo对象(WPF)在window操作系统的任务栏中同步任务进度的功能。

什么是TaskbarItemInfo对象?

TaskbarItemInfo类为 Windows 7 任务栏功能提供托管包装。有关 Windows shell 和本机任务栏 Api 的详细信息,其中taskbar的缩略图操作界面和任务进度更新就是其中的两个部分功能。.

WPF实现任务栏同步进度

WPF实现任务栏同步进度

  • 参考资料:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.shell.taskbariteminfo?f1url=%3FappId%3DDev16IDEF1%26l%3DZH-CN%26k%3Dk(System.Windows.Shell.TaskbarItemInfo);k(VS.XamlEditor)%26rd%3Dtrue&view=net-5.0

  • 源码地址:https://github.com/JusterZhu/2021PlanJ

二、实现

xaml代码

<Window
x:Class="TaskProgressBar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TaskProgressBar"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d"
>
<Window.TaskbarItemInfo>    
<TaskbarItemInfo 
ProgressState="Normal"
 />
</Window.TaskbarItemInfo>  
<Grid>    
<StackPanel 
HorizontalAlignment="Center"
VerticalAlignment="Center"
>       
<ProgressBar            
x:Name="MyProgressBar"
Width="400"
Height="30"
/>     
<Button            
x:Name="MyBtn"
Width="80"
Height="25"
Margin="10"
Click="MyBtn_Click"            
Content="Start"
/>
</StackPanel>
</Grid>
</Window>

TaskbarItemInfo对象中ProgressState枚举字段。我们这里只是正常的显示进度那么枚举为Normal即可。

  • Error 3
    任务栏按钮中显示红色的进度指示器。

  • Indeterminate 1
    任务栏按钮中显示闪烁的绿色进度指示器。

  • None 0
    任务栏按钮中未显示进度指示器。

  • Normal 2
    任务栏按钮中显示绿色的进度指示器。

  • Paused 4
    任务栏按钮中显示黄色的进度指示器。

c#代码

WPF实现任务栏同步进度