2

Using C#* Windows Forms on Windows 10, is there a way to set up a progress indicator on the taskbar icon? While searching for it almost all the results were directed to Windows 7. I tried using WindowsAPICodePack, TaskbarItemInfo, TaskbarManager and others. None worked. Here are some snippets of code:

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;
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.SetValue(this.Handle, 50, 100);
TaskbarProgress.SetState(this.Handle, TaskbarProgress.TaskbarStates.Indeterminate);

* My project currently uses .NET Framework 4.7.2

Thanks in advance

Brhaka
  • 1,622
  • 3
  • 11
  • 31
  • 1
    Windows API CodePack should work, assuming you can find the library to use. What did you actually try? Your question is pretty vague. – Peter Duniho Nov 06 '20 at 18:25
  • 1
    @PeterDuniho As written in my question, I tried the above 3 codes. If you have a solution, please let me know! Thanks – Brhaka Nov 10 '20 at 13:55

1 Answers1

4

old answer:

find "presentationframework.dll" in your computer, just like "C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF"

Then, add it to reference in your project, and add "using System.Windows.Shell" at your code.

try your code above again.

——————————————

I must explain further The above libraries and classes are applied to WPF programs

For C# WinForm, you must download Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll, import them into the project, and then use the following code:

using Microsoft.WindowsAPICodePack.Taskbar;

In the Form class: private TaskbarManager TaskbarProcess = TaskbarManager.Instance;

In an event of Form: TaskbarProcess.SetProgressState(TaskbarProgressBarState.Normal); TaskbarProcess.SetProgressValue(current, total);

I tested in Win10 64bit and VS2017 .net4.5, the above method is ok.

James
  • 66
  • 3
  • 1
    Thank you for your answer! I'll definitively run some tests on the next few days. – Brhaka Jan 07 '21 at 14:19
  • 1
    @Brhaka I must explain further, The above libraries and classes are applied to WPF programs For C# WinForm, please see the above answer again. – James Jan 09 '21 at 03:28
  • 1
    Thank you for your explanation. Much appreciated – Brhaka Jan 09 '21 at 08:05
  • 1
    I’ll be able to test the libraries in a few days. When I do, if they’re correct (which I believe they are) I’ll accept your answer. – Brhaka Jan 09 '21 at 08:10