Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Windows 7 & WPF 4: using task bar icons like progress bars

Did you ever try to download a file from the Internet with Windows 7? If yes, maybe you noticed that the Internet Explorer icon in the task bar shows the download progress, like a classic ProgressBar. With WPF 4 and VB (o C#) 2010 it's possible programming this interesting functionality in order to make our applications able of showing the progress state of a given task. The code example shown in this blog post simply wastes some time in an asynchronous way so that it can send progress state to a ProgressBar control and to the icon in the task bar (the reason about this is that the focus is understanding how the progress bar works, not providing complex code). For the sake of completeness, I took some tips from this blog post by Pete Brown, that I revisited in a VB fashion.

On the XAML side, first we need the usual reference to the task bar, then we can add a ProgressBar inside the Window:





        <ProgressBar Name="ProgressBar1" Height="40" Margin="5"/>


Next, let's write some VB code which makes use of a BackgroundWorker control to make async operations, whose progress will be reported on both the task bar and on the ProgressBar. The following code demonstrates how wasting your time and how you can send the progress to the task bar at the same time:

Class MainWindow

    Private Sub MainWindow_Loaded(ByVal sender As Object,

                                  ByVal e As RoutedEventArgs) Handles Me.Loaded


        Dim worker As New System.ComponentModel.BackgroundWorker


        AddHandler worker.DoWork, AddressOf WasteSomeTime

        AddHandler worker.ProgressChanged, AddressOf worker_ProgressChanged

        AddHandler worker.RunWorkerCompleted, AddressOf worker_RunWorkerCompleted


        worker.WorkerReportsProgress = True


        'Set the progress state as "normal"

        TaskbarItemInfo.ProgressState = Shell.TaskbarItemProgressState.Normal


        'Start the work


    End Sub


    'Waste some time, simulating some work

    Private Sub WasteSomeTime(ByVal sender As Object,

    ByVal e As System.ComponentModel.DoWorkEventArgs)

        For i As Integer = 0 To 99 Step 10



            'Raises the ProgressChanged event passing the value

            CType(sender, System.ComponentModel.BackgroundWorker).ReportProgress(i)

        Next i

    End Sub


    'Finally set values for controls

    Private Sub worker_RunWorkerCompleted(ByVal sender As Object,

                                          ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)

        ProgressBar1.Value = 100

        TaskbarItemInfo.ProgressValue = 1.0

    End Sub


    'Increment the control's progress value

    Private Sub worker_ProgressChanged(ByVal sender As Object,

                                       ByVal e As System.ComponentModel.ProgressChangedEventArgs)

        ProgressBar1.Value = e.ProgressPercentage

        TaskbarItemInfo.ProgressValue = CDbl(e.ProgressPercentage) / 100

    End Sub

End Class

The following are the most two interesting lines of code from all the one shown above. The first line:

        'Set the progress state as "normal"

        TaskbarItemInfo.ProgressState = Shell.TaskbarItemProgressState.Normal

Here the code tells the icon on the task bar that the progress bar will have a normal aspect, which is green. Other choices are available from the Shell.TaskBarItemProgressState enumeration, which allow to customize the progress bar aspect. For instance, Error will make the progress state's color as red, Paused as yellow, while Indeterminate will represent the progress state as a loop; this is typical when you want to represent an idle state or a freezed task.

This is instead the second interesting line:

TaskbarItemInfo.ProgressValue = 1.0

Here we just assign to the ProgressValue property the value that represents the progress state. This is a moment of what we get on the task bar:

By the way, such feature consumes some more system resources so you should use it carefully. The source code accompanying this blog post is available from the MSDN Code Gallery here.

Finally, I'd like to mention that there are lots of other ways (and resources) for programming Windows 7 with managed code, not necessarily related to WPF 4. If you are interested in this kind of development, have a look at the Windows API Code Pack which provides managed access to lots of features of the new operating system and that can be also used with Visual Studio 2008.


Print | posted on venerdì 29 gennaio 2010 18:42 | Filed Under [ Visual Studio 2010 Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 1 and 4 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET