Programming Windows 7: Taskbar. Part 2 - ThumbButtons

    Recently, I talked about how in Windows 7 you can display the progress of an operation right in the Windows taskbar. This time we will continue to talk about the features of Windows 7 for the programmer and consider adding our own control buttons to the preview window.



    You could already notice similar functionality when using Windows 7. For example, similar buttons exist for Windows Media Player. They allow you to switch tracks, as well as stop playback. In total, these buttons can be created no more than seven.

    Undoubtedly, such functionality can be useful not only for Media Player, but also for our applications. Let's see how you can implement this in our application.

    As I said, there is a .NET wrapper for all Windows 7 system functions called the .NET Interop Sample Library. We used the services of this library when we controlled the status of the progress bar. Now we will also use this library.

    The creation of our buttons should occur at the time the WM_TaskbarButtonCreated event is processed. Therefore, in the form, you must override the WndProc method and handle the moments when this event occurs. To initialize the buttons, you need a ThumbButtonManager object. This object controls the behavior and display of these buttons. This object can be created using the CreateThumbButtonManager extension method. After that, you must use the CreateThumbButton method and create the button object. After all the buttons are created, you need to add them to the taskbar using the AddThumbButtons method.

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




    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 one control button has appeared. However, if we try to click on it, we will see that the event handler does not work. In order for the handler to start working, it is necessary to explicitly pass the ability of the ThumbButtonManager to handle events in the WndProc method.

    As a result, we get the following simple code. This application contains buttons for managing progress (as in the previous case) and contains 6 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);
    }






    Good luck in developing your applications for Windows 7! Sample

    Application:
    ThumbButtons.zip

    Also popular now: