C# 任务栏中的 Windows 7 进度条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1295890/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:48:44  来源:igfitidea点击:

Windows 7 progress bar in taskbar in C#?

c#windows-7progress-bar

提问by

If you've noticed in the Windows 7 beta, if you copy files or other system actions, the windows explorer icon in the taskbar will fill up with a green progress bar equivalent to the progress bar on the form. Is there a way that, in my C# forms, I can force my taskbar progress bar to match the progress of whatever task I'm doing? Converting, transferring, generating, there are so many uses for that progress bar.

如果您在 Windows 7 测试版中注意到,如果您复制文件或其他系统操作,任务栏中的 Windows 资源管理器图标将填充一个绿色进度条,相当于表单上的进度条。有没有办法在我的 C# 表单中强制我的任务栏进度条与我正在执行的任何任务的进度相匹配?转换、传输、生成,那个进度条有很多用途。

回答by Alex

回答by arul

Yes, Microsoft covered the new taskbar functions in the following document (sources included): Windows 7 taskbar: Developer Resources

是的,Microsoft 在以下文档(包括来源)中介绍了新的任务栏功能:Windows 7 任务栏:开发人员资源

回答by Mark Lakata

For people who want to skip reading the documentation and just get something that works...

对于那些想跳过阅读文档而只想得到一些有用的东西的人......

  • Download the Windows API Code Pack for Microsoft .Net Framework.
  • Run the installer which creates a zip file
  • Extract the following dlls from the binaries folder of the zip file:
    • Microsoft.WindowsAPICodePack.dll
    • Microsoft.WindowsAPICodePack.Shell.dll
  • Add a reference to them in your project
  • Put the following code into your app and modify as necessary:
  • 下载适用于 Microsoft .Net FrameworkWindows API 代码包
  • 运行创建 zip 文件的安装程序
  • 从 zip 文件的二进制文件夹中提取以下 dll:
    • Microsoft.WindowsAPICodePack.dll
    • Microsoft.WindowsAPICodePack.Shell.dll
  • 在您的项目中添加对它们的引用
  • 将以下代码放入您的应用程序并根据需要进行修改:

--

——

  int max = 100;
  var prog = Microsoft.WindowsAPICodePack.Taskbar.TaskbarManager.Instance;
  prog.SetProgressState(Microsoft.WindowsAPICodePack.Taskbar.TaskbarProgressBarState.Normal);
  for(int i=0;i<max;i++) {
     prog.SetProgressValue(i, max);
     Thread.Sleep(100);     
  }
  prog.SetProgressState(Microsoft.WindowsAPICodePack.Taskbar.TaskbarProgressBarState.NoProgress);

回答by blondii

I found a beautiful article (Link) that provides a simple solution to the taskbar progress bar problem. In summary, it instructs you to download the Windows API pack from the MSDN website, adding a reference to the Microsoft.WindowsAPICodePack.Shell.dll that it contains, and finally add three lines of code to your application:

我找到了一篇漂亮的文章(链接),它为任务栏进度条问题提供了一个简单的解决方案。总之,它指示您从 MSDN 网站下载 Windows API 包,添加对其中包含的 Microsoft.WindowsAPICodePack.Shell.dll 的引用,最后向您的应用程序添加三行代码:

Imports Microsoft.WindowsAPICodePack
Imports Microsoft.WindowsAPICodePack.Taskbar
// ...
TaskbarManager.Instance.SetProgressValue(X, 100)

where X is the progress you want to display.

其中 X 是您要显示的进度。

回答by WhoIsRich

I just wanted to add some taskbar progress animation to my WinForms application, without having to download code packs or switch to WPF to use TaskbarItemInfo.

我只是想向我的 WinForms 应用程序添加一些任务栏进度动画,而无需下载代码包或切换到 WPF 以使用 TaskbarItemInfo。

The solution was a class that uses the ITaskbarList3 interface:

解决方案是一个使用 ITaskbarList3 接口的类:

using System;
using System.Runtime.InteropServices;

public static class TaskbarProgress
{
    public enum TaskbarStates
    {
        NoProgress    = 0,
        Indeterminate = 0x1,
        Normal        = 0x2,
        Error         = 0x4,
        Paused        = 0x8
    }

    [ComImport()]
    [Guid("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private interface ITaskbarList3
    {
        // ITaskbarList
        [PreserveSig]
        void HrInit();
        [PreserveSig]
        void AddTab(IntPtr hwnd);
        [PreserveSig]
        void DeleteTab(IntPtr hwnd);
        [PreserveSig]
        void ActivateTab(IntPtr hwnd);
        [PreserveSig]
        void SetActiveAlt(IntPtr hwnd);

        // ITaskbarList2
        [PreserveSig]
        void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);

        // ITaskbarList3
        [PreserveSig]
        void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
        [PreserveSig]
        void SetProgressState(IntPtr hwnd, TaskbarStates state);
    }

    [ComImport()]    
    [Guid("56fdf344-fd6d-11d0-958a-006097c9a090")]
    [ClassInterface(ClassInterfaceType.None)]
    private class TaskbarInstance
    {
    }

    private static ITaskbarList3 taskbarInstance = (ITaskbarList3)new TaskbarInstance();
    private static bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);

    public static void SetState(IntPtr windowHandle, TaskbarStates taskbarState)
    {
        if (taskbarSupported) taskbarInstance.SetProgressState(windowHandle, taskbarState);
    }

    public static void SetValue(IntPtr windowHandle, double progressValue, double progressMax)
    {
        if (taskbarSupported) taskbarInstance.SetProgressValue(windowHandle, (ulong)progressValue, (ulong)progressMax);
    }
}

Example of how easy it is to use:

它是多么容易使用的例子:

TaskbarProgress.SetState(this.Handle, TaskbarProgress.TaskbarStates.Indeterminate);

or

TaskbarProgress.SetValue(this.Handle, 50, 100);
TaskbarProgress.SetState(this.Handle, TaskbarProgress.TaskbarStates.Error);