C# 如何为 Windows 7 编写进度条以在任务栏上进行自我更新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1146574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I code a progress bar for Windows 7 to also update itself on the taskbar?
提问by Goyuix
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.
Windows 7 有一个很棒的新功能,应用程序可以通过状态栏报告当前活动的进度。例如,当使用 Windows 资源管理器复制文件时,进度条位于任务栏中的应用程序图标的顶部,并在更新时显示进度。
What is the API for exposing the progress bar? Is there MSDN documentation on it?
暴露进度条的API是什么?有关于它的 MSDN 文档吗?
采纳答案by sigint
There's a good article in MSDN magazine about the new taskbar APIs. And yes, the feature is awesome :-)
MSDN 杂志中有一篇关于新任务栏 API的好文章。是的,这个功能很棒:-)
Essentially, it's all about implementing IFileOperation
. There's a good article about using it in managed code here.
从本质上讲,这一切都与实现IFileOperation
. 有一个关于在托管代码中使用它的好文章在这里。
回答by Wyatt O'Day
I've written an article about implementing the Windows 7 Taskbar progress API in C# (see: Windows 7 Taskbar Progress Bar with C# and .NET). The control is open source (BSD) and has example projects for C# and VB.NET.
我写了一篇关于在 C# 中实现 Windows 7 任务栏进度 API 的文章(请参阅:Windows 7 Taskbar Progress Bar with C# and .NET)。该控件是开源的 (BSD),并具有 C# 和 VB.NET 的示例项目。
This way you don't have to convert the C++ code from scratch.
这样您就不必从头开始转换 C++ 代码。
回答by Keeron
If you plan to use other Windows 7 Taskbar features, another approach would be to use the library from Microsoft: Windows API Code Pack for .NET Framework which is no longer available at the old link, but can be found on nuget.
如果您计划使用其他 Windows 7 任务栏功能,另一种方法是使用 Microsoft 的库:Windows API Code Pack for .NET Framework,旧链接不再提供,但可以在nuget上找到。
回答by Wilka
For below .NET 4, or WinForms in any .NET version
对于低于 .NET 4 或任何 .NET 版本的 WinForms
Using the Windows API Code Packfrom Microsoft (as Keeron mentioned), it's really simple. You just need to use the TaskbarManager
. E.g.
使用Microsoft的Windows API 代码包(正如 Keeron 提到的),这真的很简单。您只需要使用TaskbarManager
. 例如
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
对于 .NET 4 及更高版本的 WPF
You can use System.Windows.Shell.TaskbarItemInfo. E.g. in the Xaml for your main window, you'll need to add:
您可以使用System.Windows.Shell.TaskbarItemInfo。例如,在主窗口的 Xaml 中,您需要添加:
<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.
不要忘记,如果您在后台线程上执行“工作”(这对于长时间运行的任务可能是一个好主意),您将需要切换回 UI 线程来更新任务栏。