Home

Icons

 

Introduction to Icons

 

Description

An icon is a small picture used to represent something such as an object on the computer screen. While a bitmap can have any dimension the window needs, the size of an icon must be limited. This is because icons assume different roles on an application.

 

Icons are used to represent folders in different types of applications such as Windows Explorer:

Creating an Icon

An icon is a picture file that has the extension .ico. There are various types of applications you can use to create an icon but there are standards you must follow. In reality, Microsoft Windows supports icons in different categories but each category must respect a specific size. They are 16x16 pixels, 24x24 pixels, 32x32 pixels, 48x48 pixels, or 72x72 pixels. In each category, there are variances on the number bits that each pixels holds, which specifies the number of colors that an icon can display. The options are 4 bits (16 colors), 8 bits (256 colors) or even 32 bits. The other reality is that the application you use to create your icon will specify what types of icons you can create.

Characteristics on an Icon

 

Using an Icon

In Microsoft Windows, an icon is identified as an HICON handle. To support icons, the VCL provides a class named TIcon. The TIcon class is derived from TGraphic. In reality, neither the HICON handle nor the TIcon class is used to create an icon. They only give you a reference to an icon. As stated already, you must use a graphic application to visually create an icon and save it as a file with .ico extension. After creating the icon, you can use it in your application.

One of the ways you can use an icon is to associate it to your application. To do this, on the main menu, click Project -> Options... In the left list, click Application. On the right side, click the Load Icon button:

Project Options

From the Application Icon dialog box, locate the desired icon and select it, then click Open.

To programmatically get an icon, you can use one of the Load... methods of the TGraphic class. For example, you can call the TGraphic::LoadFromFile() method to get an icon file. Here is an example:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Exercise.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TIcon * appIcon = new TIcon;

	appIcon->LoadFromFile("C:\\Exercise\\Sample.ico");
	Application->Icon = appIcon;
}
//---------------------------------------------------------------------------

To let you load an icon that is stored in an application, the TIcon class provides the LoadFromResourceName() method. Its syntax is:

void __fastcall LoadFromResourceName(unsigned int Instance,
                                     System::UnicodeString ResName);

If you can locate the icon using an identifiyer, call the LoadFromResourceID() of the TIcon class. Its syntax is:

void __fastcall LoadFromResourceID(unsigned int Instance, int ResID);

In the Win32, to access an icon, you can call the LoadIcon() function. Its syntax is:

HICON LoadIcon(
  __in  HINSTANCE hInstance,
  __in  LPCTSTR lpIconName
);

The hInstance argument is the handle to the instance of the application that contains the icon. If you are getting the icon from a file, you can (should) pass this argument as NULL. The lpIconName argument is the file name of the icon.

After using an icon, you can (should) release it. This is done by calling the ReleaseHandle() method. Its syntax is

HICON__ * __fastcall ReleaseHandle(void);

Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TIcon * appIcon = new TIcon;

	appIcon->LoadFromFile("C:\\Exercise\\Sample.ico");
	Application->Icon = appIcon;
	
	appIcon->ReleaseHandle();
}
//---------------------------------------------------------------------------

A Handle to an Icon

To give you access to a Win32's HICON handle, the TIcon class provides the Handle property:

__property HICON__ * Handle = {read=GetHandle,write=SetHandle};

This handle gives you access to the functions provided by the Win32 library to deal with icons. For example, you can call the Win32's LoadIcon() function to get an icon, then assign the returned value to the Handle, and use the icon as you see fit. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	HICON iconExample = LoadIcon(NULL, "C:\\Exercise\\Sample.ico");
	TIcon * appIcon = new TIcon;

	appIcon->Handle = iconExample;

	Application->con = appIcon;

	appIcon->ReleaseHandle();
}
//---------------------------------------------------------------------------

Microsoft Windows Predefined Icons

Microsoft Windows ships with some sample icons you can use in your application. To get one of these icons, you can call the LoadIcon() function, pass the first argument as NULL, and pass the second argument using the provided identifier. The identifiers of these icons are: IDI_APPLICATION, IDI_ASTERISK, IDI_ERROR, IDI_EXCLAMATION, IDI_HAND, IDI_INFORMATION, IDI_QUESTION, IDI_WARNING, IDI_WINLOGO, and IDI_SHIELD. Here is an example of using one of those icons:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	HICON iconExample = LoadIcon(NULL, IDI_APPLICATION);
	TIcon * appIcon = new TIcon;

	appIcon->Handle = iconExample;

	Application->Icon = appIcon;

	appIcon->ReleaseHandle();
}
//---------------------------------------------------------------------------
 
 
     
 

Home Copyright © 2010-2011 FunctionX, Inc.