Home

GDI+ Icons

 

Introduction

Like a bitmap, an icon is used to display graphics on window objects. While a bitmap can have any dimension the window needs, the size of an icon is limited. This is because icons assume different roles on an application.

Icons are used to represent folders in Windows Explorer and My Computer:

 

 

Creating Icons

To create an icon, once again, you can use any application that has the capability. Normally, you can use Microsoft Visual Studio 2005 to create or design an icon. To do this

  • In the Solution Explorer, you can right- click Resource Files -> Add -> Resource...
  • In the Resource View, you can right-click the name of the project -> Add -> Resource...

This would open the Add Resource dialog box. In the Add Resource dialog box, you can select Icon:

And click New.

When you start designing an icon, you would be presented with a drawing area whose dimensions are 32 x 32 pixels. This is the size of the icon that displays as Large Icon. Here is an example from the New File dialog box of Microsoft Visual Studio 2005 in the Templates list:

In some cases, you may allow the user to display smaller icons, which are 16x16 pixels:

To make this possible, you can associate a second icon to the 32x32 one. The application you use to design your icon should make it simple for you to add this second icon. To do this in Microsoft Visual Studio 2005, while the icon is displaying:

  • On the main menu, you can click Image -> New Image Type...
  • You can right-click an empty area in the icon window and click New Image Type...

When the New Icon Image Type dialog box comes up, you can click 16x16, 16 colors and click OK.

Whether you create only one or both versions of the icon, both are stored in a single file whose extension is .ico

Practical Learning Practical Learning: Creating Icons

  1. In the Solution Explorer, right-click Resource Files -> Add -> Resource...
  2. In the Add Resource dialog box, double-click Icon
  3. On the Image Editor toolbar, click the Fill Tool , and right-click the green area to make it white
  4. On the Image Editor toolbar, click the Line button 
  5. In the Colors Palette, click the blue color
  6. In the empty drawing area, count 15 small boxes from the top left to the right. In the 16th box, click and drag right and down for an angle of 45˚ for 7 boxes. Release the mouse
  7. Click the next small box on the right side of the top blue box then drag left and down at 45˚ for 7 boxes:
     
  8. Using the tools of Image Editor toolbar, complete the design as follows:
     
  9. In the Colors Palette, click the picture that has the small monitor
  10. In the Image Editor toolbar, click the Fill tool and click a white area in the drawing area
     
  11. To design the 16x16 pixel version of the icon, right-click a white area in the drawing section and click New Image Type
  12. In the New Icon Image Type dialog box, click 16x16, 16 Colors if necessary and click OK
     
  13. Design the icon as follows:
     
  14. In the Resource View tab, click the IDI_ICON1 node
  15. In the Properties window, change the FileName to Diamong.icon
  16. Change the ID to IDI_DIAMOND

Using an Icon

To support icons, the GDI+ library provides the Icon class, which is defined in the System::Drawing namespace. To use an icon in your application, you can first declare an Icon handle using one of the class' constructors. If the icon is stored in a file, the simplest constructor to use it has the following syntax:

public:
    Icon(String ^ filename);

With this constructor, the name of, or the path to, the icon file is passed as argument. After creating the icon, if you want to use only one size version, you can use one the following constructors to declare the variable:

public:
    Icon(Icon ^ original, Size size);
    Icon(Icon ^ original, int width, int height);

After initializing an Icon variable, if you want to get its dimensions, you can access its Width and its Height properties, or its Size property.

As mentioned already, there are various ways an icon can be used. For example, you can display it in a control by drawing it. To do this, you can call the Graphics::DrawIcon() method which is overloaded with two versions whose syntaxes are:

public:
    void DrawIcon(Icon ^ icon, Rectangle targetRect);
    void DrawIcon(Icon ^ icon, int x, int y);

The first version allows you to specify the location and dimensions of the icon. The second version allows you to specify only the location of the icon.

Practical Learning Practical Learning: Using an Icon

  1. Display the form and double-click the middle of its body
  2. To display the icon in the title bar, implement the event as follows:
     
    System::Void Form1_Load(System::Object ^  sender, System::EventArgs ^  e)
    {
        Drawing::Icon ^ icoMain = gcnew Drawing::Icon(L"Diamond.ico");
        Icon = icoMain;
    }
  3. Execute the application
     
  4. Close the form and return to your programming environment
 

Home Copyright © 2007-2012 FunctionX