Delphi - Icons

Icons are an important part of application branding.

However, the Delphi 5 help on TIcon is wrong!! (actually, incomplete) This page clarifies the error and explains how to load and display any available icon size from the program's resources.

While Windows applications use icons for many purposes, this page focuses on the application's icon - MAINICON.

Also see- Creating icons using GIMP and wikipedia page on the ICO file_format.

Why the help is wrong | Why this matters | How Delphi 5 handles this | Application.Icon | What works


Why the help is wrong

This is the Delphi 5 help on TIcon.Width In fact - that is not correct .. as demonstrated in the code below As you can clearly see, it is perfectly acceptable to set the width before a handle is assigned!

This argument applies equally to TIcon.Height which has similar help and code.


Why this matters

Icons are Windows resources that contain several similar images of different sizes and different color depths. By default, the 32x32 icon is returned when you load a TIcon. However, depending on the purpose, you may want a different size.

As a result of reverse engineering the code, a better (and more correct) help entry might read.

I would also add See also links (not included in the Delphi 5 help) to the Windows functions that read icons from the application's resource section .. and an appropriate example or two. I think they expect you to use Code to get a different size is below.


How Delphi 5 handles this

Delphi has an internal (and undocumented) ReadIcon procedure that uses the Windows API to create icons of any size. The following snippets only affect the icon size From windows.pas As a result, it is possible to specify which icon size you prefer and windows will create it - unfortunately, this just creates a blank icon and does not load one from the program resources.


Application.Icon

The application's MAINICON resource contains multiple images used to represent the application in Windows Explorer, on the Windows Task Bar (bottom of the screen), and various other places.

Application.Icon returns the default Windows icon size - 32x32.

To run an experiment, I tried setting the icon size before accessing Application.Icon. However, that failed

because the application loads the icon in its constructor using a function that returns the Windows default size. From the Windows API help


What works

From the Windows API help Using that function, the following example will load any size icon - just replace 64, 64 with the desired size. If the desired size is not available, then Windows will resize one of the available icons to the size you request. When the size parameters are set to - 0,0 - then the first icon in the stack is shown unless the last parameter includes LR_DEFAULTSIZE. Other than this one case, the order of the icons is irrelevant.

Note that TApplication.Handle points to the main form (which does not have an associated resource section) and that MainInstance points to the executable (which does). Instead of MainInstance, HInstance also works.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / Delphi / Icons.html