The official release of Windows 7 is not far off, so the study of new features of the operating system for C # programmers becomes relevant. New trends and rules for programmers have already appeared with the release of Windows Vista with the advent of UAC. But Vista was not widely used by users, so programmers were not very actively studying the new. With the release of Windows 7, the situation may change for the better. I decided to create on my website a
separate section dedicated specifically to programming for Window 7.
On Habré there was already a cycle of examples devoted to programming for Windows 7. But these examples came out at the time when Win7 was still in beta. Therefore, I bookmarked articles to return to them later. I did not want to install the beta version on a working machine. But recently, I still
set myself Windows 7 and am very pleased with it.
Now that I already had Win7, I could start exploring the materials that I bookmarked. But it turned out that the examples are a bit out of date. There are new libraries with other classes and namespaces. Therefore, we had to learn new products from scratch. In addition, the published examples were designed for quite experienced programmers, so some minor details were omitted as a matter of course.
And we begin with an example of creating a progress indicator (ProgressBar) on the taskbar. An old topic on this topic can be found
here .
')
But first you need to prepare. Download
Windows API Code Pack for Microsoft .NET Framework . This is a powerful package designed to help developers create applications for Windows 7 (and partly Windows Vista) using .NET. A package is a library of source codes used to access some of the new Windows features through managed code.
Unpack the archive with the files and run the solution
WindowsAPICodePack.sln . By default, the
Shell project will be used as the StartUp project. We are quite satisfied with this, since it is here that the classes we need to work with the taskbar are located. Choose in the menu
Build | Build Shell and get in the folder
.. \ WindowsAPICodePack \ Shell \ bin \ Debug file
Microsoft.WindowsAPICodePack.Shell.dll . This is our library, which we will use in our projects.
Now we start Visual Studio and create a new project Windows7TaskBarProgressBarDemo. In Solution Explorer, right-click on the
References folder and select
Add Reference . In the dialog box, switch to the
Browse tab and find the
Microsoft.WindowsAPICodePack.Shell.dll library that we created. Switch to the code editing mode and set the line
using Microsoft.WindowsAPICodePack.Taskbar;On this first preparations are completed.
Add a timer to the form and a button that will start the timer. It's time to talk about the purpose of our application. Suppose we put milk on the stove for heating. But we do not want to stand at the stove, but want to read a new article on Habré. You convince yourself that in a minute nothing terrible will happen and the milk will not run away. But, reading an interesting article, you do not notice the time. The result is pitiable. We will try to establish control over time.
So, set the timer interval to 1000 (1 second) and write the code:
TaskbarManager instanceTaskBar = TaskbarManager.Instance;
static int counter = 0;
private void butStartTimer_Click( object sender, EventArgs e)
{
instanceTaskBar.SetOverlayIcon( null , "" );
timerCook.Enabled = true ;
}
private void timerCook_Tick( object sender, EventArgs e)
{
counter += 1;
instanceTaskBar.SetProgressValue(counter, 60);
if (counter >= 60)
{
timerCook.Enabled = false ;
counter = 0;
instanceTaskBar.SetProgressValue(0, 60);
instanceTaskBar.SetOverlayIcon(Windows7TaskBarProgressBarDemo.Properties.Resources.Ready, "" );
}
}
* This source code was highlighted with Source Code Highlighter .
Now explanations to the code. The
SetProgressValue method is responsible for coloring the application button on the taskbar as a progress indicator. Starting the timer, we increase the counter value every second and bring it to the maximum value (in our case up to 60). When the counter reaches its ceiling, you need to somehow visually show the user that the operation is completed. For this purpose, I decided to use the
SetOverlayIcon method, which I will talk about another time.
So, what it looks like in practice. You put the milk on the stove, sit down at the computer and press the button that starts the timer. Now you can switch to the browser and read the article on Habré. You can always control how much time is left to read from the corner of your eye on the indicator.

After the end of a minute, an icon appears on the taskbar to indicate the end of the process.

As you can see, everything is very simple.
In our example, the standard progress indicator is green (Normal mode). There are other options: NoProgress, Indeterminate, Error, Paused. You can see how they look on the already mentioned page. We
program Windows 7: Taskbar. Part 1 - Progress Bar / Windows 7 / HabrahabrThe given example is a little far-fetched. In real life, such functionality may be needed for a variety of tasks: displaying the copy process, generating data, generating images, etc. Some third-party applications already use this feature, not counting native applications in Windows 7.
If someone has difficulties with an example, then a little later I will post the source code on the website in the
Windows 7 section. Good luck in mastering Window 7!