Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

c# - How do I code a progress bar for Windows 7 to also update itself on the taskbar?

Windows 7 has an AWESOME new feature that applications can report the progress of the current activity through the status bar. For example, when copying file(s) using Windows Explorer, a progress bar is layered on top of the application icon in the task bar and the progress is shown as it updates.

What is the API for exposing the progress bar? Is there MSDN documentation on it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For below .NET 4, or WinForms in any .NET version

Using the Windows API Code Pack from Microsoft (as Keeron mentioned), it's really simple. You just need to use the TaskbarManager. E.g.

To start the progress:

TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);

To update the progress:

TaskbarManager.Instance.SetProgressValue(currentValue, maxProgressValue);

And when when you're done, to end the progress:

TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);

There is more you can do, but that should get you started and might be all you need.

For .NET 4 and above with WPF

You can use System.Windows.Shell.TaskbarItemInfo. E.g. in the Xaml for your main window, you'll need to add:

<Window.TaskbarItemInfo>
    <TaskbarItemInfo x:Name="taskBarItemInfo" />
</Window.TaskbarItemInfo>

Then to update the progress, you would do something like:

taskBarItemInfo.ProgressState = TaskbarItemProgressState.Normal;

for (int i = 0; i < 100; i++)
{
    taskBarItemInfo.ProgressValue = i / 100.0;
    Thread.Sleep(50); // whatever the 'work' really is
}

taskBarItemInfo.ProgressState = TaskbarItemProgressState.None;

Don't forget that if you're doing the 'work' on a background thread (which is probably a good idea for long running tasks), you will need to switch back to the UI thread to update the taskbar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...