We program for Windows 7. The progress bar on the taskbar
The official release of Windows 7 is just around the corner, so exploring the new features of the operating system for C # programmers is becoming relevant. New trends and rules for programmers appeared already with the release of Windows Vista with the advent of UAC. But Vista is not widespread among users, so programmers are not very actively learning new things. With the release of Windows 7, the situation may change for the better. I decided to create a separate section on my site dedicated specifically to programming for Window 7.
Habré already had a series of examples devoted to programming for Windows 7. But these examples came out at a time when Win7 was still in beta. Therefore, I bookmarked the articles to return to them later. I did not want to put the beta version on a working machine. But recently, I still installed Windows 7 and am very pleased with it.
Now that I already had Win7, it was possible to start studying the materials that I bookmarked. But it turned out that the examples are a bit outdated. New libraries with other classes and namespaces have appeared. Therefore, I had to learn new items from scratch. In addition, the published examples were designed for fairly experienced programmers, so some small details were omitted as a matter of course.
And we will start 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 the Windows API Code Pack for the Microsoft .NET Framework . This is a powerful package designed to help developers create applications for Windows 7 (and partially Windows Vista) using .NET. The package is a library of source codes used to access some of the new Windows features through managed code.
Unzip the file archive and run the WindowsAPICodePack.sln solution . By default, the Shell project will be used as the StartUp project.. This suits us perfectly, since it is here that the classes we need to find for working with the taskbar are located. Choose Build | Build Shell and get in the folder .. \ WindowsAPICodePack \ Shell \ bin \ Debug the file Microsoft.WindowsAPICodePack.Shell.dll . This is our library, which we will use in our projects.
Now start Visual Studio and create a new Windows7TaskBarProgressBarDemo project. 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 code editing mode and write the line
using Microsoft.WindowsAPICodePack.Taskbar;
On this, the first preparations are finished.
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 a 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 nothing bad will happen in a minute and that milk will not run away. But, reading an interesting article, you do not notice the time. The result is deplorable. We will try to establish control over time.
So, we set the timer interval equal to 1000 (1 second) and write the code:
Now explanations for the code. For coloring the application button on the taskbar as a progress indicator, the SetProgressValue method is responsible . By 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, how it looks in practice. You put 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é. Out of the corner of your eye on the indicator, you can always control how much time is left for reading.
After the minute has ended, an icon appears on the taskbar to signal the end of the process.
As you can see, everything is very simple.
In our example, we used the standard progress indicator of green (Normal mode). There are other options: NoProgress, Indeterminate, Error, Paused. You can see how they look on the already mentioned page Programming Windows 7: Taskbar. Part 1 - Progress Bar / Windows 7 / Habrahabr
The given example is a little far-fetched. In real life, such functionality may be needed for a wide variety of tasks: displaying the copying process, data generation, image generation, etc. Some third-party applications are already using this feature, apart from native applications in Windows 7.
If someone has difficulties with an example, then a little later I will post the source on the site in the Windows 7 section . Good luck learning Window 7!
Habré already had a series of examples devoted to programming for Windows 7. But these examples came out at a time when Win7 was still in beta. Therefore, I bookmarked the articles to return to them later. I did not want to put the beta version on a working machine. But recently, I still installed Windows 7 and am very pleased with it.
Now that I already had Win7, it was possible to start studying the materials that I bookmarked. But it turned out that the examples are a bit outdated. New libraries with other classes and namespaces have appeared. Therefore, I had to learn new items from scratch. In addition, the published examples were designed for fairly experienced programmers, so some small details were omitted as a matter of course.
And we will start 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 the Windows API Code Pack for the Microsoft .NET Framework . This is a powerful package designed to help developers create applications for Windows 7 (and partially Windows Vista) using .NET. The package is a library of source codes used to access some of the new Windows features through managed code.
Unzip the file archive and run the WindowsAPICodePack.sln solution . By default, the Shell project will be used as the StartUp project.. This suits us perfectly, since it is here that the classes we need to find for working with the taskbar are located. Choose Build | Build Shell and get in the folder .. \ WindowsAPICodePack \ Shell \ bin \ Debug the file Microsoft.WindowsAPICodePack.Shell.dll . This is our library, which we will use in our projects.
Now start Visual Studio and create a new Windows7TaskBarProgressBarDemo project. 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 code editing mode and write the line
using Microsoft.WindowsAPICodePack.Taskbar;
On this, the first preparations are finished.
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 a 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 nothing bad will happen in a minute and that milk will not run away. But, reading an interesting article, you do not notice the time. The result is deplorable. We will try to establish control over time.
So, we set the timer interval equal 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 for the code. For coloring the application button on the taskbar as a progress indicator, the SetProgressValue method is responsible . By 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, how it looks in practice. You put 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é. Out of the corner of your eye on the indicator, you can always control how much time is left for reading.
After the minute has ended, an icon appears on the taskbar to signal the end of the process.
As you can see, everything is very simple.
In our example, we used the standard progress indicator of green (Normal mode). There are other options: NoProgress, Indeterminate, Error, Paused. You can see how they look on the already mentioned page Programming Windows 7: Taskbar. Part 1 - Progress Bar / Windows 7 / Habrahabr
The given example is a little far-fetched. In real life, such functionality may be needed for a wide variety of tasks: displaying the copying process, data generation, image generation, etc. Some third-party applications are already using this feature, apart from native applications in Windows 7.
If someone has difficulties with an example, then a little later I will post the source on the site in the Windows 7 section . Good luck learning Window 7!