Another interesting possibility of a new task panel in
Windows 7 is the opportunity to add your own buttons to manage application in a
preview window. Similar functionality can be noticed when you use Windows 7.
For example, analogues buttons are in Windows Media Player. They allow us to
switch tracks and stop playing. You can create more then seven such buttons.
Without a doubt, such functionality can be useful not only
in Media Player, but also in our applications. Let us look at how we can
implement it in our applications. Creation of our buttons should occur at the
moment of WM_TaskbarButtonCreated message processing. That is why we should
redefine the method WndProc on a form and process moments of this event
occurrencef.
Listing 3: Overriding WndProc method
protected override void WndProc(ref Message m)
{
if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
{
// initialize buttons
}
base.WndProc(ref m);
}
ThumbButtonManager object is needed to initialize buttons.
This object manages the behavior and the displaying of these buttons. This
object can be created using an extension method CreateThumbButtonManager. After
that, it is necessary to take advantage of CreateThumbButton method and create
an object of a button. After all buttons will be created, it is necessary to
add them on the task panel thanks to AddThumbButtons method.
Listing 4: Creation of the buttons
protected override void WndProc(ref Message m)
{
if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
{
InitializeThumbButtons();
}
base.WndProc(ref m);
}
protected void InitializeThumbButtons()
{
ThumbButtonManager thumbButtonManager =
WindowsFormsExtensions.CreateThumbButtonManager(this);
var decreaseThumbButton = thumbButtonManager.CreateThumbButton(1,
Icons.Navigation_First_2, "To reduce the progress");
decreaseThumbButton.Clicked += delegate
{
// ..
};
thumbButtonManager.AddThumbButtons(decreaseThumbButton);
}
Now, when you start the application you can see that there
was one button control. But if we try to press it, we will see that the handler
of event will not work. To let it work it is necessary to pass directly to the
opportunity of process events to ThumbButtonManager object in WndProc method.
As a result, we receive the following simple code.
Listing 5: Handling the pressing of buttons
private ThumbButtonManager _thumbButtonManager;
protected override void WndProc(ref Message m)
{
if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
{
InitializeThumbButtons();
}
if (_thumbButtonManager != null)
_thumbButtonManager.DispatchMessage(ref m);
base.WndProc(ref m);
}
protected void InitializeThumbButtons()
{
if (_thumbButtonManager == null)
{
_thumbButtonManager = WindowsFormsExtensions.
CreateThumbButtonManager(this);
}
var decreaseThumbButton = _thumbButtonManager.CreateThumbButton(1,
Icons.Navigation_First_2, "To reduce the progress");
decreaseThumbButton.Clicked += delegate
{
Progress.Text =
(float.Parse(Progress.Text) - 10).ToString();
WindowsFormsExtensions.SetTaskbarProgress(this,
float.Parse(Progress.Text));
};
// other buttons
_thumbButtonManager.AddThumbButtons(decreaseThumbButton,
normalStateThumbButton, indeterminateStateThumbButton,
pauseStateThumbButton, errorStateThumbButton, increaseThumbButton);
}
This application contains 6 buttons to control progress.
Figure 3: Thumbnail buttons