C#实现win7任务栏进度条
win7的任务栏进度条有一个专门的类TaskbarManager,他在Windows API Code Pack里面,下载地址:http://archive.msdn.microsoft.com/WindowsAPICodePack 里面包含了所有的源代码以及大量的示例,并附有文档。
为了使用TaskbarManager类,首先要引用Microsoft.WindowsAPICodePack.dll和Microsoft.WindowsAPICodePack.Shell.dll这两个库,他们在binaries文件夹里,然后在程序里申明 Microsoft.WindowsAPICodePack.Taskbar和Microsoft.WindowsAPICodePack.Shell。具体用法看下面的例子,这是Windows API Code Pack 1.1\source\Samples\Shell\TaskbarDemo例子的一部分,还有其他功能就不一一列出来了,感兴趣的自己可以去看看。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;
using System.Reflection;
namespace TaskbarDemo
{
public partial class ChildDocument : Form
{
// Keep a reference to the Taskbar instance
private TaskbarManager windowsTaskbar = TaskbarManager.Instance;
private JumpList childWindowJumpList;
private string childWindowAppId;
void ChildDocument_Shown(object sender, EventArgs e)
{
// Set our default
windowsTaskbar.SetProgressState(TaskbarProgressBarState.NoProgress, this.Handle);
}
#region Progress Bar
private void trackBar1_Scroll(object sender, EventArgs e)
{
// When the user changes the trackBar value,
// update the progress bar in our UI as well as Taskbar
progressBar1.Value = trackBar1.Value;
windowsTaskbar.SetProgressValue(trackBar1.Value, 100, this.Handle);
}
private void comboBoxProgressBarStates_SelectedIndexChanged(object sender, EventArgs e)
{
// Update the status of the taskbar progress bar
TaskbarProgressBarState state = (TaskbarProgressBarState)(Enum.Parse(typeof(TaskbarProgressBarState),
(string)comboBoxProgressBarStates.SelectedItem));
windowsTaskbar.SetProgressState(state, this.Handle);
// Update the application progress bar,
// as well disable the trackbar in some cases
switch (state)
{
case TaskbarProgressBarState.Normal:
if (trackBar1.Value == 0)
{
trackBar1.Value = 20;
progressBar1.Value = trackBar1.Value;
}
progressBar1.Style = ProgressBarStyle.Continuous;
windowsTaskbar.SetProgressValue(trackBar1.Value, 100, this.Handle);
trackBar1.Enabled = true;
break;
case TaskbarProgressBarState.Paused:
if (trackBar1.Value == 0)
{
trackBar1.Value = 20;
progressBar1.Value = trackBar1.Value;
}
progressBar1.Style = ProgressBarStyle.Continuous;
windowsTaskbar.SetProgressValue(trackBar1.Value, 100, this.Handle);
trackBar1.Enabled = true;
break;
case TaskbarProgressBarState.Error:
if (trackBar1.Value == 0)
{
trackBar1.Value = 20;
progressBar1.Value = trackBar1.Value;
}
progressBar1.Style = ProgressBarStyle.Continuous;
windowsTaskbar.SetProgressValue(trackBar1.Value, 100, this.Handle);
trackBar1.Enabled = true;
break;
case TaskbarProgressBarState.Indeterminate:
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;
trackBar1.Enabled = false;
break;
case TaskbarProgressBarState.NoProgress:
progressBar1.Value = 0;
trackBar1.Value = 0;
progressBar1.Style = ProgressBarStyle.Continuous;
trackBar1.Enabled = false;
break;
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
childWindowJumpList = JumpList.CreateJumpListForIndividualWindow(childWindowAppId, this.Handle);
((Button)sender).Enabled = false;
groupBoxCustomCategories.Enabled = true;
buttonRefreshTaskbarList.Enabled = true;
}
}
}
|
首先获得程序的一个TaskbarManager对象,上面是调用TaskbarManager.Instance实现的,这是一个静态方法,得到windowsTaskbar对象。TaskbarManager有一个SetProgressValue方法,通过调用它就可以设置任务栏进度条的Value,他有多种重载方式,请参考说明文档。SetProgressState方法用来设置任务栏进度条的状态,共有5种状态NoProgress、Indeterminate、Normal、Error、Paused。
运行效果如下图:
当然还可以设置OverlayIcon、ThumbnailToolbar 、TabbedThumbnail 来美化我们的程序,在pack提供的sample中都有他们的使用例子。
最后一点,这种效果只有在win7或vista里才能使用(win8不知道,应该也行吧),所以在使用时要判断一下系统是不是支持。有三种方法,分别是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//方法一
if (System.Environment.OSVersion.Version.Major >= 6) //make sure you are not on a legacy OS
{
//.........................
}
//方法二
CoreHelpers.ThrowIfNotWin7();//不符合就return
//方法三
if(TaskbarManager.IsPlatformSupported)
{
//...................................
}
|
专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com