Windows 7 programming: Taskbar. Part 8 – IconicThumbnail.
Recently I have told about how it is possible to influence on preview windows in the task panel of Windows 7. We displayed only that part of a window which is most important for the user in that time. Nevertheless in certain situations be much more useful displaying of other picture (an application state), instead of window parts.
For example, this possibility uses Windows Live Messanger. If we hover the mouse cursor to WLM icon it will display avatars of the current user in an preview window .
Let's understand this possibility of the task panel of Windows 7. For implementation of similar behaviour in .NET Interop Sample Library there is SetIconicThumbnail method. With its help we can create easily and simply own preview for our window. However, before we will start it to use we should enable this behaviour by means of EnableCustomWindowPreview method. Otherwise at executing of SetIconicThumbnail method we will receive a corresponding exception. To make it it is possible directly in the designer of the form.
public Form1()
{
InitializeComponent();
WindowsFormsExtensions.EnableCustomWindowPreview(this);
}
It is necessary to tell, that the sizes of the image which we wish to display in preview are limited. It is logical, because would present that was, if preview could be the size 1024x768. These sizes are limited by value 200x120. The image can be less than this size. In this case the window preview also will be reduced. If the image more than these sizes the exception will be throwed.
In parametres of SetIconicThumbnail method the reference to the current form and the image (Bitmap) which need to be displayed is passed. Our goal - to generate this Bitmap. And in this case at us hands are completely untied - we can generate all that is necessary for us. It can be a window picture, with any additional text atop. It can be any own picture containing the information. It can be the text with a some statistics. All depends on your imagination.
I will generate the image displaying a state of application by the text and graphically, and also a small picture of a window in the demonstration application. For this purpose I will create Bitmap with necessary sizes to me and I will fill in with its background. After that I will draw there the image of the state and a window picture. Then I will write the text of the state on this image and I will transfer result in SetIconicThumbnail method.
private static void SetState(Form form, string stateText, Image stateImage)
{
// blank image
var preview = new Bitmap(200, 120);
var g = Graphics.FromImage(preview);
// fill background
g.DrawImage(Images.background, 0, 0);
// file image of state
if (stateImage != null)
{
g.DrawImage(stateImage.GetThumbnailImage(100, 100, null, IntPtr.Zero), 100, 10);
}
// fill image of form
g.DrawImage(GetFormImage(form, 50, 60), 10, 10);
// draw text
g.DrawString(stateText, new Font("Verdana", 18), Brushes.White, 10, 70);
// setting thumbnail
WindowsFormsExtensions.SetIconicThumbnail(form, preview);
}
We can see that the code has turned out laconic enough. Finally, we can see a window with the following preview.
That is interesting, our application can change itself this state in a background. Thus, if the mouse is on a icon the application and preview at present is displayed, image change in an emerging window occurs smoothly, without any by-effects. So, for example, I can create the timer in which handler through certain time intervals to change a state.
private readonly string[] _stateTexts = new[] { "Deleting..", "Wireless", "Security", "Stop", "Help", "O'kay", "Playing", "Login", "Warning", "Showcase", "Search", "Processing..", "Locked", "Error", "Refreshing.." };
private readonly Bitmap[] _stateImages = new[] { Images._104, Images._110, Images._111, Images._112, Images._113, Images._114, Images._120, Images._125, Images._129, Images._134, Images._17, Images._25, Images._41, Images._50, Images._52 };
private int _currentIndex = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (_currentIndex + 1 >= _stateImages.Length)
{
_currentIndex = 0;
}
else
{
_currentIndex++;
}
SetState(this, _stateTexts[_currentIndex], _stateImages[_currentIndex]);
}
![]() |
![]() |
![]() |
Preview windows of our application will dynamically change, and the user can see this state easily and conveniently
Sample application: