How do I code a progress bar for Windows 7 to also update itself on the taskbar?

前端 未结 4 644
情书的邮戳
情书的邮戳 2020-12-05 00:22

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 Window

相关标签:
4条回答
  • 2020-12-05 01:04

    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.

    0 讨论(0)
  • 2020-12-05 01:05

    For below .NET 4, or WinForms in any .NET version

    Using the Windows API Code Pack from Microsoft (as Keeron mentioned), it's really simple. You just need to use the TaskbarManager. E.g.

    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

    You can use System.Windows.Shell.TaskbarItemInfo. E.g. in the Xaml for your main window, you'll need to add:

    <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.

    0 讨论(0)
  • 2020-12-05 01:19

    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.

    This way you don't have to convert the C++ code from scratch.

    0 讨论(0)
  • 2020-12-05 01:27

    There's a good article in MSDN magazine about the new taskbar APIs. And yes, the feature is awesome :-)

    Essentially, it's all about implementing IFileOperation. There's a good article about using it in managed code here.

    0 讨论(0)
提交回复
热议问题