C#实现win7任务栏进度条

2012年04月14日 分类:学习笔记C#

用过win7的人都知道,当我们用IE或firefox下载某个东西是任务栏上就会出现进度条,这大大增加了界面的友好程度,如下图所示。那我们怎么用c#来实现呢?

win7 taskbar

任务栏进度条并不是在.net框架里的,当时我在.net里找了很久,走了不少弯路,惭愧啊!

由于.net是面向所有windows平台的,win7只是一个特例,微软也不会为了一个win7就增加.net的容量。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例子的一部分,还有其他功能就不一一列出来了,感兴趣的自己可以去看看。


// 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。

运行效果如下图:

win7 taskbar sample

当然还可以设置OverlayIcon、ThumbnailToolbar 、TabbedThumbnail 来美化我们的程序,在pack提供的sample中都有他们的使用例子。

最后一点,这种效果只有在win7或vista里才能使用(win8不知道,应该也行吧),所以在使用时要判断一下系统是不是支持。有三种方法,分别是:

//方法一
if (System.Environment.OSVersion.Version.Major >= 6) //make sure you are not on a legacy OS
{
//.........................
}

//方法二

CoreHelpers.ThrowIfNotWin7();//不符合就return

//方法三
if(TaskbarManager.IsPlatformSupported)
{
//...................................
} 

国外参考资料:

http://www.codeproject.com/Articles/49983/Task-Manager
Task Manager

http://www.codeproject.com/Articles/65185/Windows-7-Taskbar-C-Quick-Reference
Windows 7 Taskbar C# Quick Reference

http://msdn.microsoft.com/en-us/gg465001
Exercise: Experiment with the New Windows 7 Taskbar Features

作者:wuyuan 本文来自Wuyuan's Blog 转载请注明,谢谢! 文章地址: http://wuyuans.com/2012/04/csharp-win7-taskbar