Personalizar Tasbar Windows 7(II). ProgressBar

Continuando con la temática de mi post anterior, hoy vamos a ver otra de las funcionalidades que se pueden aplicar a la barra de tareas de Windows 7. En esta ocasión veremos como mostrar una barra de progreso sobre el icono de nuestra aplicación mínimizada. De esta manera como su propio nombre indica veremos el progreso de la misma: activa, pausada, si se produce error, si esta pensando ….
 
Partiendo del proyecto anterior lo que hacemos es añadir un nuevo control WPF, cuyo código de definición es el siguiente:
 
  1. <UserControl x:Class="TaskBarIcons.ProgressBar"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  4.     <Grid Width="300">
  5.         <Grid.RowDefinitions>
  6.             <RowDefinition Height="Auto" />
  7.             <RowDefinition Height="Auto" />
  8.             <RowDefinition Height="Auto" />
  9.             <RowDefinition Height="Auto" />
  10.             <RowDefinition Height="Auto" />
  11.             <RowDefinition Height="*" />
  12.         </Grid.RowDefinitions>
  13.         <Rectangle Style="{DynamicResource BorderStyle}" Grid.RowSpan="4" />
  14.         <TextBlock Text="Taskbar Progress" Margin="{DynamicResource BoxedContentMargin}" Style="{DynamicResource SectionTitle}" />
  15.         <DockPanel Grid.Row="1" Margin="{DynamicResource BoxedContentMargin}">
  16.             <TextBlock Text="Status" DockPanel.Dock="Left"></TextBlock>
  17.             <ComboBox x:Name="ProgressStateSelection" Margin="5,0,0,0" HorizontalAlignment="Stretch"
  18.                 SelectionChanged="ProgressStateSelection_SelectionChanged"/>
  19.         </DockPanel>
  20.         <Slider x:Name="progressSlider" MaxWidth="300" Height="40" Minimum="0" Maximum="100" Grid.Row="2"
  21.             ValueChanged="progressSlider_ValueChanged" Margin="{DynamicResource BoxedContentMargin}"/>
  22.         <CheckBox x:Name="ShowProgressBar" Click="ShowProgressBar_Click" Grid.Row="3"
  23.               Margin="{DynamicResource BoxedContentMargin}">
  24.             <TextBlock Text="Show slider's value in progress bar" />
  25.         </CheckBox>
  26.     </Grid>
  27. </UserControl>
  28.  
  29. </form>

Y cuya lógica de funcionamiento viene definida a través del siguiente código:

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using Microsoft.WindowsAPICodePack.Taskbar;
  5.  
  6. namespace TaskBarIcons
  7. {
  8.     /// <summary>
  9.     /// Interaction logic for ProgressBar.xaml
  10.     /// </summary>
  11.     public partial class ProgressBar : UserControl
  12.     {
  13.         public ProgressBar()
  14.         {
  15.             InitializeComponent();
  16.             this.Loaded += ProgressBar_Loaded;
  17.             this.Unloaded += ProgressBar_Unloaded;
  18.         }
  19.         private void ProgressBar_Unloaded(object sender, RoutedEventArgs e)
  20.         {
  21.             TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
  22.         }
  23.  
  24.         private void ProgressBar_Loaded(object sender, RoutedEventArgs e)
  25.         {
  26.             if (this.ProgressStateSelection.ItemsSource == null)
  27.             {
  28.                 this.ProgressStateSelection.ItemsSource = Enum.GetValues(typeof(TaskbarProgressBarState));
  29.                 ProgressStateSelection.SelectedItem = TaskbarProgressBarState.NoProgress;
  30.             }
  31.         }
  32.  
  33.         private void progressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  34.         {
  35.             UpdateProgress();
  36.         }
  37.  
  38.         //This method is part of the lab. If we are asked to show a progress bar,
  39.         //choose the appropriate TaskbarProgressState value and use the UpdateProgress
  40.         //method to perform the update.
  41.         private void ShowProgressBar_Click(object sender, RoutedEventArgs e)
  42.         {
  43.             if (ShowProgressBar.IsChecked.Value)
  44.             {
  45.                 ProgressStateSelection.SelectedItem = TaskbarProgressBarState.Normal;
  46.             }
  47.             else
  48.             {
  49.                 ProgressStateSelection.SelectedItem = TaskbarProgressBarState.NoProgress;
  50.             }
  51.             UpdateProgress();
  52.  
  53.         }
  54.  
  55.         //This method is part of the lab. When the progress bar state changes,
  56.         //synchronize it with the checkbox and use the UpdateProgress method to
  57.         //perform the update.
  58.         private void ProgressStateSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
  59.         {
  60.  
  61.             if ((TaskbarProgressBarState)ProgressStateSelection.SelectedItem == TaskbarProgressBarState.NoProgress)
  62.             {
  63.                 ShowProgressBar.IsChecked = false;
  64.             }
  65.             else
  66.             {
  67.                 ShowProgressBar.IsChecked = true;
  68.             }
  69.             UpdateProgress();
  70.  
  71.  
  72.         }
  73.  
  74.         //This method is part of the lab. When asked to update the progress state and
  75.         //we should indeed display a taskbar progress bar, use the TaskbarManager's
  76.         //SetProgressValue and SetProgressState methods to update the progress bar state.
  77.         private void UpdateProgress()
  78.         {
  79.  
  80.             if (ShowProgressBar.IsChecked.Value)
  81.             {
  82.                 TaskbarManager.Instance.SetProgressValue((int)progressSlider.Value, 100);
  83.                 TaskbarManager.Instance.SetProgressState((TaskbarProgressBarState)ProgressStateSelection.SelectedItem);
  84.             }
  85.             else
  86.             {
  87.                 TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
  88.             }
  89.  
  90.  
  91.         }
  92.     }
  93. }

 
El control que hemos creado es el que se observa en la parte inferior de la imagen:
 
progressbar
 
A través de él podemos seleccionar los distintos estados:
en progreso, que se mostrará como una barra verde gradual
error, se mostrará una barra roja gradual
en pausa, se mostrará una barra amarilla gradual
pensando se mostrará una barra verde intermitente
 
De manera que veremos los estados de nuestra aplicación de la siguiente forma:
 
progbar1            progbar2           progbar3
En Progreso              Error                  Pensando
 
Desde mi punto de vista, este tipo de desarrollos son muy interesantes no sólo visualmente, sino también a nivel de usabilidad. 

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *