5,401,186 members and growing! (17,961 online)
Email Password   helpLost your password?
Desktop Development » Toolbars & Docking windows » Toolbars     Intermediate License: The GNU Lesser General Public License

Multiple Image Sizes for ToolStrip Items

By lhayes00

This 'ToolStrip' extension automatically selects an image using the selected image size.
C# (C# 3.0, C#), Windows, .NET (.NET, .NET 3.5), GDI+, Dev

Posted: 16 Jun 2008
Updated: 16 Jun 2008
Views: 4,504
Bookmarked: 23 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 4.21 Rating: 4.42 out of 5
0 votes, 0.0%
1
1 vote, 11.1%
2
1 vote, 11.1%
3
2 votes, 22.2%
4
5 votes, 55.6%
5
IconToolStrip_demo

Introduction

Tool strips are a fantastic asset because they provide a tidy and user-friendly interface for the user. It is possible to add images to many of the different tool strip items. It is even possible to force such items to appear larger by adjusting the property ImageScalingSize, however at the cost of pixelisation.

Usually it is desirable to make our applications as accessible as possible. Larger tool strip items are fantastic for those with poorer sight, or even those who just simply enjoy the quality of larger icons. Unfortunately, the standard ToolStrip class does not provide Icon support. When an icon is specified, it is converted into an Image instance. Thus the benefits of using an icon are lost.

So I decided that it would be a nice idea to add multi-image support to the ToolStrip control. Whilst my primary focus was on adding some support for Icon, I thought it more desirable to generalize this by using the two interfaces IImageProvider and IMultipleImageProvider.

Design

When MultipleImageToolStrip is being updated, it calls upon available IImageProvider's to acquire images of the required size.

An IImageProvider exposes the two methods IsImageSupported and GetImage. When multiple icon providers are available it is possible to access the required icon using a key. In the code supplied with this article the key represents a ToolStripItem. The following illustration provides an overview of the design using a UML class diagram.

uml-overview.png

When no IImageProvider is available, the original ToolStripItem is maintained and stretched where necessary to match the overall tool strip size.

For my extension I decided that four discreet sizes would ease the implemention of custom image providers. The available sizes are: Small (16 x 16), Medium (24 x 24), Large (32 x 32), and ExtraLarge (48 x 48). When the requested image size is not available, a default icon can be automatically substituted if the MultipleImageToolStrip.UseUnknownImageSizeIcon is set to true.

Images are selected as follows:

  • If an image provider is available from within the tool strip:
    • If the required image size is supported, that image is acquired.
    • If the required image size was not supported and UseUnknownImageSizeIcon is set, the default image provider is utilized.
    • When no image can be acquired using those criteria, the original image is simply stretched to match that of the scaled buttons.
  • If the above fails and the item is itself an IImageProvider then it is used to acquire the required image.
  • Finally if the items ToolStripItem.Tag is an IImageProvider then it is used to acquire the required image.

Using the Code

It is always a good idea to backup your project before trying something new!

To use the attached code simply:

  • Add the source files to your project.
  • Recompile your project.
  • Drag MultipleImageToolStrip onto your Form or UserControl from the tool box.
  • Add some buttons.
  • You should now have a ToolStrip which behaves much the same as before.

If you want to convert an existing ToolStrip into a MultipleImageToolStrip:

  • Find the ".designer.cs" file which is associated with your form or control.
  • Open it up and replace instances of ToolStrip with MultipleImageToolStrip.

To make items automatically resizable add a handler for your form's (or control's) Load event. Take a look below for an example of how to do this:

private void Form1_Load(object sender, EventArgs e)
{
    // Begin updating tool strip images. This is important because it prevents
    // the tool strip from refreshing after each image assignment.
    this.iconToolStrip.BeginUpdateImages();

    // Here it is possible to provide an 'IImageProvider' instance.
    this.iconToolStrip.AssignImage(toolStripBack, new IconImageProvider(
        Resources.arrow_left));

    // Or just provide icons themselves.
    this.iconToolStrip.AssignImage(toolStripForward, Resources.arrow_right);
    this.iconToolStrip.AssignImage(toolStripHome, Resources.home);
    this.iconToolStrip.AssignImage(toolStripStop, Resources.stop);

    // Finalize updating.
    this.iconToolStrip.EndUpdateImages();

    // Then to select an initial icon size.
    this.iconToolStrip.ImageSize = ImageSize.Medium;
}

When a tool strip item is permenantly removed, the associated IImageProvider must also be manually removed. This decision was made because often an item is only removed temporarily and then reinserted.

If you always want to remove an associated IImageProvider when a tool item is removed, add a handler for the MultipleImageToolStrip.ItemRemove event:

private void iconToolStrip_ItemRemoved(object sender, ToolStripItemEventArgs e)
{
    // Automatically remove associated image provider.
    this.iconToolStrip.RemoveImage(e.Item);
}

Alternatively the associated provider can be removed along with the item:

public void RemoveToolStripItem(ToolStripItem item)
{
    // Remove item itself.
    this.iconToolStrip.Items.Remove(item);
    
    // Remove any associated provider.
    this.iconToolStrip.RemoveImage(item);
}

Points of Interest

The fantastic icons used in the demo application were kindly provided by glyFX from their free 'Vista Common' icon set. The required icons were combined together using the free IcoFX tool.

History

Original version posted.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License

About the Author

lhayes00


I have been using computers since I was about 8 years old and have always enjoyed learning about new concepts and technologies. I have been programming since about 1995 and have become experienced with many programming languages and technologies including C/C++, C#, DirectX, OpenGL, Java, VB, ASP.NET, SQL, and Conitec Game Studio A5 Pro.

I have always been interested in game and multimedia development and have recently graduated with the first-class degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich.

Since graduating I have been continuing studying software and game development. In particular I am interested in game engine creation and programming language development. In addition to study, I am working on the design and creation of a game engine.

In my spare time I enjoy playing my musical instruments, especially my guitars. Its great fun and something which helps me unwind after a long day. I enjoy many flavours of music, in particular rock and heavy metal bands like Megadeth, Anacrusis, and Pink Floyd.
Location: United Kingdom United Kingdom

Other popular Toolbars & Docking windows articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Jun 2008
Editor: Sean Ewington
Copyright 2008 by lhayes00
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project