Winforms - C# 任务栏进度指示器 (Windows 10)

Brh*_*aka 3 c# taskbar winforms windows-10 windows-10-desktop

Windows 10上使用 C#* Windows 窗体,有没有办法在任务栏图标上设置进度指示器?在搜索时,几乎所有结果都指向 Windows 7。我尝试使用WindowsAPICodePackTaskbarItemInfoTaskbarManager等。没有一个起作用。以下是一些代码片段:

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;
Run Code Online (Sandbox Code Playgroud)
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);
    }
}
Run Code Online (Sandbox Code Playgroud)
TaskbarProgress.SetValue(this.Handle, 50, 100);
TaskbarProgress.SetState(this.Handle, TaskbarProgress.TaskbarStates.Indeterminate);
Run Code Online (Sandbox Code Playgroud)

* 我的项目当前使用.NET Framework 4.7.2

提前致谢

Jam*_*mes 5

旧答案:

\n

在计算机中找到“presentationframework.dll”,就像“C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\WPF”

\n

然后,将其添加到项目中的引用,并在代码中添加“using System.Windows.Shell”。

\n

再次尝试上面的代码。

\n

\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2 \x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94

\n

我必须进一步解释\n以上库和类应用于WPF程序

\n

对于C# WinForm,您必须下载Microsoft.WindowsAPICodePack.dll和Microsoft.WindowsAPICodePack.Shell.dll,将它们导入到项目中,然后使用以下代码:

\n

使用 Microsoft.WindowsAPICodePack.Taskbar;

\n

在 Form 类中:\nprivate TaskbarManager TaskbarProcess = TaskbarManager.Instance;

\n

在 Form 事件中:\nTaskbarProcess.SetProgressState(TaskbarProgressBarState.Normal);\nTaskbarProcess.SetProgressValue(current,total);

\n

我在Win10 64bit和VS2017 .net4.5下测试过,上面的方法是可以的。

\n