Sometimes when a technology has been out awhile, some features are forgotten about. With the release of WPF 4 came libraries that allow you, as the developer, to do some pretty cool stuff with the Windows 7 Task Bar, such as create the following features:
This will be the first part in a four part series covering how to implement each of those features. This post will cover the first bullet in the list, Thumbnail buttons.
Thumbnail buttons are buttons that can be added over your application’s icon that allow the user to execute custom actions. If you are using the Windows Scenic theme, the buttons will show just below the applications screenshot when you hover over the application icon in the task bar.
If you are using a basic theme it will look like this:
Since Windows Media Player is such a good example, we will create our own media player and implement our own thumbnail buttons. Lets start off by defining our interface. All we really need is a menu so we can open our media file to play, we need a MediaElement to show the media file, and we need some buttons to Play, Pause, and Stop the media file.
Will will notice that we have an event handler for our Open menu item and we have assigned a few commands to our buttons. Lets go ahead at look at the code for opening a media file.
This code is pretty straight forward, we simple create a new instance of an OpenFileDialog, set a filter so we restrict the type of file the user can open, and when the user selects the file, we go ahead and set the source of our media player and immediately start playing it. The next thing we need to do is address those commands we are referencing on our buttons. So let’s go ahead and create some CommandBindings for the MediaCommands and implement them accordingly.
Now for this example, all the commands will use the same CanExecute method. The only thing I care about is if the Source of the MediaElement is null.
Great. Now we have the basic application finished. Here is what my version looks like after loading a media file to play. (shameless plug)
Now it is time to move on to creating our thumbnail buttons. We want to add Play, Pause, and Stop buttons. The key to this is the Window’s TaskbarItemInfo object. In order to add thumbnail buttons we need to define a TaskbarItemInfo object, and then populate the ThumbButtonInfos property with a ThumbButtonInfoCollection with some ThumbButtonInfo objects. Sounds confusing doesn’t it? Well, it’s not bad. Let’s look at some code.
As you can see, I set a Description property on the TaskbarItemInfo. The Description property will show a tooltip when the mouse passes over the thumbnail. Inside the ThumbButtonInfoCollection I created three ThumbButtonInfo objects, one for each button we are creating. The Description property on the ThumbButtonInfo shows a tooltip when the mouse hovers over the button. The DismissWhenClicked property tells the button is if should close the thumbnail once click, or if will keep the thumbnail open even after being clicked. We set an image source, and a Command for each button. The Command is the same command as the corresponding CommandBindings we created earlier.
[Important] Now pay special attention to the CommandTarget property.
The CommandTarget property binds the thumbnail button to the related button in our WPF window. If you do not set the CommandTarget property, the command will NOT work. Now that we have all the plumbing done, go ahead and run it. When the application is running, move your mouse over the application’s icon and watch the magic happen. You will immediately see our thumbnail buttons under our application screenshot, which are fully functional.
Go download the source and start brain storming on how you can use this feature to improve the user experience of your application.