Making your app shine on Windows 7: Tabbed Thumb Nails
This post is one in a series of posts covering the Windows API Code Pack. The code pack provides a managed code framework for using the new native Windows 7 features.
In this post we’ll examine how to implement thumbnail previews for a tabbed application UI. We’ll add to each thumbnail preview some buttons to control our application directly from the preview thumbnail. You need to be using .Net 3.5 SP1.
I used a simple C# Winforms project to test this all out. You’ll need to add five references:
WindowsBase
PresentationCore
PresentationFramework
Microsoft.WindowsAPICodePack
Microsoft.WindowsAPICodePack.Shell
The code pack framework provides Integration to the Windows 7 thumbnail preview via the TaskbarManager class. The class contains four methods and five properties. The methods mainly deal with the overlay of icons onto the taskbar icon and the progress bar overlay that I’m sure you are already familiar with in Windows 7.
The properties are the interesting bits for using thumbnail previews. The first property is a a boolean flag – IsPlatformSupported - to indicate if the current platform your app is running on can support thumbnails. Then you have Instance and ApplicationId. Instance represents the Windows Taskbar, and ApplicationId is the unique identifier for your application.
Then you have the two most useful properties; the TabbedThumbnail and ThumbnailToolbars. These get the manager class for each type allowing you to add and update the thumbnail previews associated with your application and the toolbars that may also exist for each of the previews for your application. Yes each preview could have a different toolbar for each tabbed thumbnail.
To add support to your application for thumbnail previews all you have to do is populate and maintain these two collections as necessary for your application. Because the code pack framework is provided as a static library doing this is very simple.
I have a tabcontrol in my Winforms app that allows me to add new tabs containing new instances of the WebBrowser control. In my add new tab button click event I run the following code:
Code Snippet
- TabPage newTab = new TabPage("Tab " + tabControl1.TabPages.Count + 1);
- tabControl1.TabPages.Add(newTab);
- WebBrowser wb = new WebBrowser();
- wb.DocumentTitleChanged += new EventHandler(wb_DocumentTitleChanged);
- wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
- wb.Navigated += new WebBrowserNavigatedEventHandler(wb_Navigated);
- wb.Dock = DockStyle.Fill;
- wb.Navigate(textBox1.Text);
- newTab.Controls.Add(wb);
- // Add thumbnail toolbar buttons
- TaskbarManager.Instance.ThumbnailToolbars.AddButtons(newTab.Handle, thumbButtonBack, thumbButtonForward, thumbButtonRefresh);
- // Add a new preview
- TabbedThumbnail preview = new TabbedThumbnail(this.Handle, newTab.Handle);
- // Event handlers for this preview
- preview.TabbedThumbnailActivated += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailActivated);
- preview.TabbedThumbnailClosed += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailClosed);
- preview.TabbedThumbnailMaximized += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailMaximized);
- preview.TabbedThumbnailMinimized += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailMinimized);
- TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
- // Select the tab in the application UI as well as taskbar tabbed thumbnail list
- tabControl1.SelectedTab = newTab;
- TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(tabControl1.SelectedTab);
As you can see. I add a new tabpage, create a new instance of a WebBrowser control. Tie some events to it – these have meaningful code in them ensuring the correct thumbnail behaviour occurs on the Taskbar – and add the browser control to the tabpage.
Then I add the ThumbnailToolbars buttons. These have already been defined at class level.
Code Snippet
- private ThumbnailToolbarButton thumbButtonBack;
- private ThumbnailToolbarButton thumbButtonForward;
- private ThumbnailToolbarButton thumbButtonRefresh;
Then a new instance of a TabbedThumbnail is created naming the Windows handle and the new tabpage handle. Following this the event handlers for the thumbnail toolbar buttons are attached.
Our TabbedThumbnail definition is then added to the Taskbar instance’s collection of TabbedThumbnails. And finally, we ensure the new tabpage is the current tabpage in focus on the tabcontrol and on the taskbar.
The rest of the code in the application ensures that when tabpages are changed in the tabcontrol the preview thumbnails are synchronised. And likewise, when preview thumbnails are selected or hovered over the correct tabpage is brought to focus.
The end result lookis like this:
There are all sorts of additional goodies in the TabbedThumbnail class properties that allow you to clip the bitmap and effective zoom into it as well as provide tooltips etc.
Adding thumbnail previews to your applications seems a small effort for achieving one of the most visible enhancements in the Windows 7 UI. The code pack API makes it easy to do.
My complete application can be downloaded from here. It is a simplified version of the code pack Thumbnail sample.