Zayko in the Net

Personal blog of Vitaly Zayko

NAVIGATION - SEARCH

How to add progress support to an App icon on Windows 7 Taskbar? (C#)

If you ever work with Windows 7, you already noticed that some Applications could show progress of a long process directly in their icon placed on Windows 7 Taskbar. Good example of this functionality is Internet Explorer when it download a file from Internet or Windows Explorer when you copy/move a file from one location to another. Before .NET Framework 4 era you have had to use external libraries to do this or implement PInvoke by yourself. Now, in your WPF 4 Applications you can use .NET 4 functionality. Here is step-by-step guideline how to do this.

1. Open your progress dialog XAML file. Create reference to Windows.TaskbarItemInfo as very top item of your dialog as shown below (lines 7-9):

1: <Window x:Class="IconProgressDemo.MainWindow" 
2:         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
3:         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
4:         Title="MainWindow" Height="130" Width="490"> 
5:      
6:     <!-- Reference to TaskbarItemInfo: --> 
7:     <Window.TaskbarItemInfo> 
8:         <TaskbarItemInfo /> 
9:     </Window.TaskbarItemInfo> 
10:      
11:     <Grid> 
12:         <StackPanel> 
13:             <!-- Regular WPF ScrollBar: --> 
14:             <ProgressBar Name="progressBar" HorizontalAlignment="Stretch" Height="25" /> 
15:              
16:             <!-- Start long process by pushing this button: --> 
17:             <Button HorizontalAlignment="Center" Margin="0,20" Width="65" Height="25" Click="Button_Click">Start!</Button> 
18:         </StackPanel> 
19:     </Grid> 
20: </Window> 
21: 
22: 

2. Switch to code behind of this dialog. Before process start, set a process state (line #52 below). I’ll explain possible states later. Let start with Normal.

3. Pass a process value to TaskbarItemInfo at the same time as to regular progress bar (lines 65-70).

4. When process finishes toy may want to reset both progress bar and TaskbarItemInfo (lines 58-63).

5. Run this example and press Start button. You will see that Task bar icon shows green progress over it self.

TaskbarItemInfo in Normal state

Now, as promised, let see what other ProgressState values for.

TaskbarItemInfo.ProgressState could be one of the following values:

Normal. As you saw above, this value shows regular green progress on App’s icon.

None. This is default value. This is why it’s important to change this value to any other before you start the process. Otherwise you won’t see any progress on your App icon.

Error. Show red progress bar and suitable for emerging cases when, for example, a non-critical error occurs and you don’t want to interrupt the process but let customer know about such case.

Indeterminate. Shows green ‘wave’ or ‘pulse’. Good for processes when you don’t know how long they will take and have to wait until they finish.

Paused. Shows yellow progress and good for interrupted processes. But note that switching to this value doesn’t interrupt the process it self, you still have to do this manually.

The pictures below illustrate Error, Indeterminate and Paused values of ProgressState property:

Error Indeterminate Paused

And here is full code behind of this example:

1: using  System;
2: using  System.Collections.Generic;
3: using  System.Linq;
4: using  System.Text;
5: using  System.Windows;
6: using  System.Windows.Controls;
7: using  System.Windows.Data;
8: using  System.Windows.Documents;
9: using  System.Windows.Input;
10: using  System.Windows.Media;
11: using  System.Windows.Media.Imaging;
12: using  System.Windows.Navigation;
13: using  System.Windows.Shapes;
14: using  System.Threading;
15: using  System.ComponentModel;
16: 
17: namespace  IconProgressDemo
18: {
19:     /// <summary> 
20:     /// Interaction logic for MainWindow.xaml 
21:     /// </summary> 
22:     public  partial  class  MainWindow  : Window 
23:     {
24:         public  MainWindow()
25:         {
26:             InitializeComponent();
27:         }
28: 
29:         private  void  Button_Click(object  sender, RoutedEventArgs  e)
30:         {
31:             // Simulate long process by using BackgroundWorker: 
32:             using  (BackgroundWorker  bw = new  BackgroundWorker ())
33:             {
34:                 bw.WorkerReportsProgress = true ;
35: 
36:                 // Pass process data: 
37:                 bw.ProgressChanged += new  ProgressChangedEventHandler (bw_ProgressChanged);
38: 
39:                 // Reset the counter: 
40:                 bw.RunWorkerCompleted += new  RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);
41: 
42:                 bw.DoWork += (p, ev) =>
43:                     {
44:                         for  (int  i = 0; i <= 100; i += 10)
45:                         {
46:                             Thread .Sleep(1000);
47:                             ((BackgroundWorker )p).ReportProgress(i);
48:                         }
49:                     };
50: 
51:                 // Set the progress state: 
52:                 TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState .Normal;
53: 
54:                 bw.RunWorkerAsync();
55:             }
56:         }
57: 
58:         void  bw_RunWorkerCompleted(object  sender, RunWorkerCompletedEventArgs  e)
59:         {
60:             // The process was finished, reset the counter: 
61:             progressBar.Value = 0;
62:             TaskbarItemInfo.ProgressValue = 0;
63:         }
64: 
65:         void  bw_ProgressChanged(object  sender, ProgressChangedEventArgs  e)
66:         {
67:             // Pass process counter to both ProgressBar and TaskbarItemInfo: 
68:             progressBar.Value = e.ProgressPercentage;
69:             TaskbarItemInfo.ProgressValue = (double )e.ProgressPercentage / 100;
70:         }
71:     }
72: }
73: 
74: 

Have fun!

Technorati Tags: ,

God Mode on Windows 7

Recently taken from here.

Do you want to keep all Windows 7 settings in one simple place? Then create an empty folder on your desktop and rename it to GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}. Amazing!

Using Windows 7 Jump List in Visual Studio 2010 Beta 2

If you are on Windows 7, you probably have noticed about new feature – Windows 7 Jump List. If you right-click on an App icon (let say – Internet Explorer) you will see additional menu items which you as a programmer can handle in your solutions. If you are on Visual Studio 2008, the only way is to use an additional Windows API Code Pack for Microsoft .NET Framework. But there is a modern way for Visual Studio 2010 (at least for WPF):

  • Open App.xaml file of your App
  • Add reference to JumpList as shown below:

<Application x:Class="WpfApplication1.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="MainWindow.xaml">

    <Application.Resources>    

    </Application.Resources>

    <!-- Add reference to JumpList here: -->

    <JumpList.JumpList>

        <JumpList ShowFrequentCategory="True" ShowRecentCategory="True">

            <JumpTask Title="Notepad"

                      Description="Open Notepad"

                      ApplicationPath="notepad.exe"

                      IconResourcePath="notepad.exe"/>

        </JumpList>

    </JumpList.JumpList>

</Application>

Technorati Tags: ,,

Most useful Windows 7 shortcut keys

I’m a happy MSDN subscriber so have Windows 7 installed for few months by now and got some useful shortcuts to share.

  • Multi monitor system? Use Win+Shift+Left to move to the left monitor or Win+Shift+Right – to the right
  • Win+P – switches between monitor options (multi monitor/projector settings)
  • One monitor? Then Win+Left is snapping current window to the left side and Win+Right – to the right
  • Win+Space – peeks at the Desktop
  • Win+X – calls Mobility Center
  • Win+T – first Taskbar entry
  • Win+G – brings desktop gadgets to the top

Enjoy!

Technorati Tags:

How to boot Windows 7 faster?

Check your system configuration: active services, what items run at startup ect. All of those are in one place: MSCONFIG.

MSCONFIG in Microsoft Windows 7

Technorati Tags:

Where “Send To” in Windows 7?

Long time ago I have found useful to add some custom shortcuts to “Send To” folder in Windows. Most useful for me is shortcut to Notepad: if you have this shortcut in “Send To”, when you need to open any text file (or HTML, or XML – you name it) you just need to select that file in Windows Explorer, right click on it and under “Send To” select the Notepad shortcut. Simply, isn’t it?

It is not always easy to locate this folder. Because Windows 7 is coming and I already switched to Win7 RC, this tip applies to Win7. Here is the path: C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\SendTo

Note: folder “AppData” is system and hidden by default, thus if you hate to type the entire path manually, open C:\Users\<user name>\ location then type “AppData” in address bar and then you can use your mouse to navigate to “SendTo”.

SendTo folder in Windows 7