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 possibilities of Windows 7 for the programmer and consider the possibility of adding your own control buttons in 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. All these buttons can create 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, for all the system functions of Windows 7 there is a wrapper on .NET called the .NET Interop Sample Library. We used the services of this library when we ran the status bar progress bar. Now we will also use this library.
Creation of our buttons should occur at the moment of handling the WM_TaskbarButtonCreated event. Therefore, it is necessary to override the WndProc method in the form and handle the moments when this event occurs.
protected override void WndProc(ref Message m)
{
if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
{
// initialize buttons
}
base.WndProc(ref m);
}
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 need to use the CreateThumbButton method and create a 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)
{
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 is one control button. 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 in the WndProc method to explicitly pass the ability to the ThumbButtonManager object to handle events.
As a result, we get 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 app contains buttons for managing progress (as in the past case) and contains 6 buttons.
Good luck in developing your applications for Windows 7!
Demo application:
ThumbButtons.zip