Windows 7 programming: Taskbar. Part 2 - ThumbButtons.

Recently I was telling about how we can display progress inside task panel in Windows7. This time we will continue to talk about Windows 7 possibilities for developers. Let’s look how we can use buttons in window’s preview in task panel.

Such functionality you can see in Windows Media Player already. These buttons allow to change tracks and pause of a playing. Maximum count of these buttons is seven.

Windows 7 - Media player

Such functionality can be useful for other applications undoubtedly. Let’s look how we can use it in our applications.

As I already spoke, for these system functions of Windows 7 there is .NET wrapper which is called .NET Interop Sample Library. We were using this library when were creating application for progress bar state changing. We will are using this library again.

The creation of buttons should be executed when WM_TaskbarButtonCreated message is firing. Therefore it is necessary to override WndProc method in a form.

protected override void WndProc(ref Message m)
{
    if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
    {
        // initialize buttons
    }
    
    base.WndProc(ref m);
}

A ThumbButtonManager object is necessary to initialize buttons. This object is managing behavior and visibility of these buttons. We can create the ThumbButtonManager object via extension method CreateThumbButtonManager. It is necessary to use CreateThumbButton method to create buttons after that. At last, we should add these buttons to the task panel via AddThumbButtons method.

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 application starts we can see that thumb button is shown. However, if we try click her we can see that event was not fired. It is necessary to dispatch a message in WndProc method that event handler can fire.

As a result we will receive the following simple code.

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 thumb buttons to control progress bar.

Windows 7 - Thumb buttons

Good luck to you in development of your Windows 7 applications!

Sample application:

No Comments