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

发布于 2024-08-02 09:31:15 字数 159 浏览 1 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

荒人说梦 2024-08-09 09:31:15

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

解决方案是使用 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);
    }
}

使用起来如何简单的示例:

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

or

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

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.

The solution was a class that uses the ITaskbarList3 interface:

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);
诗化ㄋ丶相逢 2024-08-09 09:31:15

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

  • 下载 适用于 Microsoft .Net Framework 的 Windows 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);

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:

--

  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);
酒绊 2024-08-09 09:31:15

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

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

满地尘埃落定 2024-08-09 09:31:15

使用 Windows 10.0.19044 进行测试

Easily add taskbar progress to the taskbar icon of an existing form.
No NuGet package, no complex coding... just copy/paste.

您需要添加到表单的唯一代码是调用更新图标的新表单属性。 (既然可以避免,为什么还要用 API 调用、过程和枚举来破坏表单呢?)

将以下代码放入项目中的新类模块中。

using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
    [ToolboxBitmap(typeof(Form))]
    public class ProgressForm : Form
    {
        private ThumbnailProgressState m_State = ThumbnailProgressState.NoProgress;
        private int m_Maximum = 100;
        private int m_Value = 0;

        //
        // Summary:
        //     Gets or sets the state in which progress should be indicated on the task
        //     bar.
        //
        // Returns:
        //     One of the System.Windows.Forms.ThumbnailProgressState values. The default is System.Windows.Forms.ThumbnailProgressState.NoProgress
        //
        // Exceptions:
        //   T:System.ComponentModel.InvalidEnumArgumentException:
        //     The value is not a member of the System.Windows.Forms.ThumbnailProgressState enumeration.
        //
        // History:
        //   An old Microsoft article
        //   July 2022 - Verified with Win10, reworded/reformatted ThumbnailProgressState.
        //   Argument exception of property Value is more granular.
        //   Windows7orGreater - no longer relevant, but unchanged in case there is a need to distinguish versions beyond Windows 10
        //   
        // Usage:
        //   Add class with this code to your project
        //   Add the inheritance "ProgressForm" to the form that will show progress.
        //   Example: public partial class Form1 : ProgressForm
        //   
        [Browsable(true)]
        [DefaultValue(ThumbnailProgressState.NoProgress)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public ThumbnailProgressState State
        {
            get { return m_State; }
            set
            {
                switch (value)
                {
                    case ThumbnailProgressState.NoProgress:
                    case ThumbnailProgressState.Indeterminate:
                    case ThumbnailProgressState.Normal:
                    case ThumbnailProgressState.Error:
                    case ThumbnailProgressState.Paused:
                        m_State = value;
                        OnStateChanged(new EventArgs());
                        break;
                    default:
                        throw new InvalidEnumArgumentException("The value is not a member of the System.Windows.Forms.ThumbnailProgressState enumeration.");
                }
            }
        }

        //
        // Summary:
        //     Gets or sets the current position of the progress bar.
        //
        // Returns:
        //     The position within the range of the progress bar. The default is 0.
        //
        // Exceptions:
        //   T:System.ArgumentException:
        //   • The value specified is greater than the value of the System.Windows.Forms.ProgressForm.Maximum property.
        //   • The value specified is less than 0.
        [Browsable(true)]
        [DefaultValue(0)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public int Value
        {
            get { return m_Value; }
            set
            {
                if (value > m_Maximum)
                {
                    throw new ArgumentException("The value specified is greater than the value of the System.Windows.Forms.ProgressForm.Maximum property.");
                }
                else if(value < 0)
                {
                    throw new ArgumentException("The value specified is less than 0.");
                }
                else
                {
                    m_Value = value;
                    OnValueChanged(new EventArgs());
                }
            }
        }

        //
        // Summary:
        //     Gets or sets the maximum value of the range of the control.
        //
        // Returns:
        //     The maximum value of the range. The default is 100.
        //
        // Exceptions:
        //   T:System.ArgumentException:
        //     The value specified is less than 0.
        [Browsable(true)]
        [DefaultValue(100)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public int Maximum
        {
            get { return m_Maximum; }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("The value specified is less than 0.");
                }
                else
                {
                    m_Maximum = value;
                    if (value < m_Value) m_Value = value;
                    OnMaximumChanged(new EventArgs());
                }
            }
        }

        protected virtual void OnStateChanged(EventArgs e)
        {
            if (Windows7orGreater) SetProgressState();
        }

        protected virtual void OnValueChanged(EventArgs e)
        {
            if (Windows7orGreater) SetProgressValue();
        }

        protected virtual void OnMaximumChanged(EventArgs e)
        {
            if (Windows7orGreater) SetProgressValue();
        }

        protected override void WndProc(ref Message m)
        {
            if (Windows7orGreater)
            {
                // if taskbar button created or recreated, update progress status
                if (m.Msg == WM_TaskbarButtonCreated) SetProgressState();
            }
            base.WndProc(ref m);
        }

        private void SetProgressState()
        {
            // must be Windows7orGreater
            TaskbarList.SetProgressState(Handle, m_State);
            SetProgressValue();
        }
        private void SetProgressValue()
        {

            switch (m_State)
            {
                case ThumbnailProgressState.Normal:
                case ThumbnailProgressState.Error:
                case ThumbnailProgressState.Paused:
                    TaskbarList.SetProgressValue(Handle, (ulong)m_Value, (ulong)m_Maximum);
                    break;
            }
        }

        private static int WM_TaskbarButtonCreated = -1;
        private static int _winVersion = -1;
        internal static bool Windows7orGreater
        {
            get
            {
                if (_winVersion < 0)
                {
                    Version osVersion = Environment.OSVersion.Version;
                    if ((osVersion.Major == 6 && osVersion.Minor > 0) || (osVersion.Major > 6))
                    {
                        // Taskbar progress indicator requires Windows 7 Or Greater
                        _winVersion = 1;

                        // register taskbar creation window message
                        WM_TaskbarButtonCreated = RegisterWindowMessage(@"TaskbarButtonCreated");
                    }
                    else
                    {
                        _winVersion = 0;
                    }
                }
                return (_winVersion > 0);
            }
        }

        private static ITaskbarList3 _taskbarList = null;
        internal static ITaskbarList3 TaskbarList
        {
            get
            {
                if (_taskbarList == null)
                {
                    lock (typeof(ProgressForm))
                    {
                        if (_taskbarList == null)
                        {
                            _taskbarList = (ITaskbarList3)new CTaskbarList();
                            _taskbarList.HrInit();
                        }
                    }
                }
                return _taskbarList;
            }
        }

        [DllImport("user32.dll")]
        internal static extern int RegisterWindowMessage(string message);

        [ComImportAttribute()]
        [GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        internal 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
            void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
            void SetProgressState(IntPtr hwnd, ThumbnailProgressState tbpFlags);
        }

        [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
        [ClassInterfaceAttribute(ClassInterfaceType.None)]
        [ComImportAttribute()]
        internal class CTaskbarList { }
    }

    public enum ThumbnailProgressState
    {
        /// <summary>
        /// No progress is displayed.<br>
        /// yourFormName.Value is ignored.</br> </summary>
        NoProgress = 0,
        /// <summary>
        /// Normal progress is displayed.<br>
        /// The bar is GREEN.</br> </summary>
        Normal = 0x2,
        /// <summary>
        /// The operation is paused.<br>
        /// The bar is YELLOW.</br></summary>
        Paused = 0x8,
        /// <summary>
        /// An error occurred.<br>
        /// The bar is RED.</br> </summary>
        Error = 0x4,
        /// <summary>
        /// The progress is indeterminate.<br>
        /// Marquee style bar (constant scroll).</br> </summary>
        Indeterminate = 0x1
    }
}

ProgressForm 添加到需要显示进度的表单:

public partial class Form1 : ProgressForm

将此示例代码添加到您的表单并逐步执行它以观察其工作情况:

    if (this.State == ThumbnailProgressState.NoProgress)
    {
        // Show the progress with GREEN
        this.State = ThumbnailProgressState.Normal;
    }
    this.Maximum = 512; // set to any integer above zero (defaults to 100)
    this.Value = 0;     // Visually equivalent to this.State = ThumbnailProgressState.NoProgress;
    this.Value = 256;   // 50% solid GREEN overlay
    this.State = ThumbnailProgressState.Error;  // Still 50% but now solid RED overlay
    this.State = ThumbnailProgressState.Paused; // Still 50% but now solid YELLOW overlay
    this.Value = 384;   // 75% YELLOW overlay (384 / 512 == 75%)
    this.Value = this.Maximum;  // 100% YELLOW overlay (512 of 512)
    this.State = ThumbnailProgressState.Indeterminate;  // Marquee. (Ignores Value and constantly scrolls with a fade)
    this.State = ThumbnailProgressState.NoProgress;     // Ignores Value, there is no overlay

Tested with Windows 10.0.19044

Easily add taskbar progress to the taskbar icon of an existing form.
No NuGet package, no complex coding... just copy/paste.

The only code you need to add to your form, are calls to the new form properties which update the icon. (Why junk up your form with API calls, procs, and an enum... when it can be avoided?)

Put the following code into a new class module in your project.

using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
    [ToolboxBitmap(typeof(Form))]
    public class ProgressForm : Form
    {
        private ThumbnailProgressState m_State = ThumbnailProgressState.NoProgress;
        private int m_Maximum = 100;
        private int m_Value = 0;

        //
        // Summary:
        //     Gets or sets the state in which progress should be indicated on the task
        //     bar.
        //
        // Returns:
        //     One of the System.Windows.Forms.ThumbnailProgressState values. The default is System.Windows.Forms.ThumbnailProgressState.NoProgress
        //
        // Exceptions:
        //   T:System.ComponentModel.InvalidEnumArgumentException:
        //     The value is not a member of the System.Windows.Forms.ThumbnailProgressState enumeration.
        //
        // History:
        //   An old Microsoft article
        //   July 2022 - Verified with Win10, reworded/reformatted ThumbnailProgressState.
        //   Argument exception of property Value is more granular.
        //   Windows7orGreater - no longer relevant, but unchanged in case there is a need to distinguish versions beyond Windows 10
        //   
        // Usage:
        //   Add class with this code to your project
        //   Add the inheritance "ProgressForm" to the form that will show progress.
        //   Example: public partial class Form1 : ProgressForm
        //   
        [Browsable(true)]
        [DefaultValue(ThumbnailProgressState.NoProgress)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public ThumbnailProgressState State
        {
            get { return m_State; }
            set
            {
                switch (value)
                {
                    case ThumbnailProgressState.NoProgress:
                    case ThumbnailProgressState.Indeterminate:
                    case ThumbnailProgressState.Normal:
                    case ThumbnailProgressState.Error:
                    case ThumbnailProgressState.Paused:
                        m_State = value;
                        OnStateChanged(new EventArgs());
                        break;
                    default:
                        throw new InvalidEnumArgumentException("The value is not a member of the System.Windows.Forms.ThumbnailProgressState enumeration.");
                }
            }
        }

        //
        // Summary:
        //     Gets or sets the current position of the progress bar.
        //
        // Returns:
        //     The position within the range of the progress bar. The default is 0.
        //
        // Exceptions:
        //   T:System.ArgumentException:
        //   • The value specified is greater than the value of the System.Windows.Forms.ProgressForm.Maximum property.
        //   • The value specified is less than 0.
        [Browsable(true)]
        [DefaultValue(0)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public int Value
        {
            get { return m_Value; }
            set
            {
                if (value > m_Maximum)
                {
                    throw new ArgumentException("The value specified is greater than the value of the System.Windows.Forms.ProgressForm.Maximum property.");
                }
                else if(value < 0)
                {
                    throw new ArgumentException("The value specified is less than 0.");
                }
                else
                {
                    m_Value = value;
                    OnValueChanged(new EventArgs());
                }
            }
        }

        //
        // Summary:
        //     Gets or sets the maximum value of the range of the control.
        //
        // Returns:
        //     The maximum value of the range. The default is 100.
        //
        // Exceptions:
        //   T:System.ArgumentException:
        //     The value specified is less than 0.
        [Browsable(true)]
        [DefaultValue(100)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public int Maximum
        {
            get { return m_Maximum; }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("The value specified is less than 0.");
                }
                else
                {
                    m_Maximum = value;
                    if (value < m_Value) m_Value = value;
                    OnMaximumChanged(new EventArgs());
                }
            }
        }

        protected virtual void OnStateChanged(EventArgs e)
        {
            if (Windows7orGreater) SetProgressState();
        }

        protected virtual void OnValueChanged(EventArgs e)
        {
            if (Windows7orGreater) SetProgressValue();
        }

        protected virtual void OnMaximumChanged(EventArgs e)
        {
            if (Windows7orGreater) SetProgressValue();
        }

        protected override void WndProc(ref Message m)
        {
            if (Windows7orGreater)
            {
                // if taskbar button created or recreated, update progress status
                if (m.Msg == WM_TaskbarButtonCreated) SetProgressState();
            }
            base.WndProc(ref m);
        }

        private void SetProgressState()
        {
            // must be Windows7orGreater
            TaskbarList.SetProgressState(Handle, m_State);
            SetProgressValue();
        }
        private void SetProgressValue()
        {

            switch (m_State)
            {
                case ThumbnailProgressState.Normal:
                case ThumbnailProgressState.Error:
                case ThumbnailProgressState.Paused:
                    TaskbarList.SetProgressValue(Handle, (ulong)m_Value, (ulong)m_Maximum);
                    break;
            }
        }

        private static int WM_TaskbarButtonCreated = -1;
        private static int _winVersion = -1;
        internal static bool Windows7orGreater
        {
            get
            {
                if (_winVersion < 0)
                {
                    Version osVersion = Environment.OSVersion.Version;
                    if ((osVersion.Major == 6 && osVersion.Minor > 0) || (osVersion.Major > 6))
                    {
                        // Taskbar progress indicator requires Windows 7 Or Greater
                        _winVersion = 1;

                        // register taskbar creation window message
                        WM_TaskbarButtonCreated = RegisterWindowMessage(@"TaskbarButtonCreated");
                    }
                    else
                    {
                        _winVersion = 0;
                    }
                }
                return (_winVersion > 0);
            }
        }

        private static ITaskbarList3 _taskbarList = null;
        internal static ITaskbarList3 TaskbarList
        {
            get
            {
                if (_taskbarList == null)
                {
                    lock (typeof(ProgressForm))
                    {
                        if (_taskbarList == null)
                        {
                            _taskbarList = (ITaskbarList3)new CTaskbarList();
                            _taskbarList.HrInit();
                        }
                    }
                }
                return _taskbarList;
            }
        }

        [DllImport("user32.dll")]
        internal static extern int RegisterWindowMessage(string message);

        [ComImportAttribute()]
        [GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        internal 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
            void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
            void SetProgressState(IntPtr hwnd, ThumbnailProgressState tbpFlags);
        }

        [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
        [ClassInterfaceAttribute(ClassInterfaceType.None)]
        [ComImportAttribute()]
        internal class CTaskbarList { }
    }

    public enum ThumbnailProgressState
    {
        /// <summary>
        /// No progress is displayed.<br>
        /// yourFormName.Value is ignored.</br> </summary>
        NoProgress = 0,
        /// <summary>
        /// Normal progress is displayed.<br>
        /// The bar is GREEN.</br> </summary>
        Normal = 0x2,
        /// <summary>
        /// The operation is paused.<br>
        /// The bar is YELLOW.</br></summary>
        Paused = 0x8,
        /// <summary>
        /// An error occurred.<br>
        /// The bar is RED.</br> </summary>
        Error = 0x4,
        /// <summary>
        /// The progress is indeterminate.<br>
        /// Marquee style bar (constant scroll).</br> </summary>
        Indeterminate = 0x1
    }
}

Add ProgressForm to the form which needs the ability to show progress:

public partial class Form1 : ProgressForm

Add this sample code to your form and step through it to watch it work:

    if (this.State == ThumbnailProgressState.NoProgress)
    {
        // Show the progress with GREEN
        this.State = ThumbnailProgressState.Normal;
    }
    this.Maximum = 512; // set to any integer above zero (defaults to 100)
    this.Value = 0;     // Visually equivalent to this.State = ThumbnailProgressState.NoProgress;
    this.Value = 256;   // 50% solid GREEN overlay
    this.State = ThumbnailProgressState.Error;  // Still 50% but now solid RED overlay
    this.State = ThumbnailProgressState.Paused; // Still 50% but now solid YELLOW overlay
    this.Value = 384;   // 75% YELLOW overlay (384 / 512 == 75%)
    this.Value = this.Maximum;  // 100% YELLOW overlay (512 of 512)
    this.State = ThumbnailProgressState.Indeterminate;  // Marquee. (Ignores Value and constantly scrolls with a fade)
    this.State = ThumbnailProgressState.NoProgress;     // Ignores Value, there is no overlay
忆伤 2024-08-09 09:31:15

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

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

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

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:

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

where X is the progress you want to display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文