Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Tabbed thumbnails in .NET

04.15.2010
| 5675 views |
  • submit to reddit

Tabbed thumbnails were introduced in Windows 7 with the new taskbar. If an application supports tabbed thumbnails, then, if a user points the mouse over the app icon in the taskbar, while the application is running, he is able to view multiple instances of various application controls opened in several tabs above the taskbar.
The most obvious example in this case would be Internet Explorer 8. If the user opens multiple tabs with various content on them, he can switch between them by pointing the mouse cursor over the IE icon in the taskbar.
 

By default, .NET WinForms applications, be those MDI or TDI do not get support for tabbed thumbnails. To achieve this, either the native API is used, or a managed wrapper, like Windows API Code Pack. The reason behind this lies in DWM – Desktop Window Manager, the graphical user interface subsystem in Windows 7. It is only able to directly communicate with top-level windows (main window for an application) and not with child windows or controls.


The API wrapper provides the functionality to get over this limitation. To test it, I created a standard WinForms application with a button and a label on it. I added references to PresentationCore, WindowsBase, Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll (the last two are included in the Windows API Code Pack). If I run the application now, the default thumbnail preview looks like this:
 


What if I wanted to create a thumbnail preview for each control on the form? Basically, to achieve this, all I have to do is create separate instances of TabbedThumbnail, set the specific properties (if I want to customize the default view) and add it to the taskbar instance.
For my specific sample application, this is achieved by doing this:
TabbedThumbnail[] thumbnails;

thumbnails = new TabbedThumbnail[this.Controls.Count];

for (int i = 0; i < this.Controls.Count; i++)
{
thumbnails[i] = new TabbedThumbnail(this.Handle, this.Controls[i]);
thumbnails[i].Title = this.Controls[i].ToString();
}

foreach (TabbedThumbnail thumbnail in thumbnails)
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(thumbnail);


I am iterating through the control array on my form and creating an instance of TabbedThumbnail for each found control. Notice the fact that each tabbed thumbnail also gets the current window handle. The Title property will represent (in this case) the type of the control. When the array is completely populated, I am adding each TabbedThumbnail to the taskbar instance. It looks like this:



But the functionality of tabbed thumbnails isn’t only limited to previews. What if I want to create a custom event that will be triggered once the user clicks on a tab? To do this, I add this code in the control iteration loop:

thumbnails[i].TabbedThumbnailActivated += new EventHandler<TabbedThumbnailEventArgs>(Form1_TabbedThumbnailActivated);

The event handler itself looks like this:

void Form1_TabbedThumbnailActivated(object sender, TabbedThumbnailEventArgs e)
{
this.Text = "Changed";
}


It doesn’t do anything important – just changes the main form’s Text property. However, a developer can customize this event as needed.
The entire set of event handlers for an instance of TabbedThumbnail is shown below: