|

Windows Icon HowTo - by Delax
Introduction
Hi and Hello! Did you ever wonder how to include a windows icon into your application? Actually this is quite easy. It is even described in the FPC documentation, but if you have never dealt with resources before you might encounter some problems. Therefor this little helper here.
Left or Right
There are two ways of including an icon into your executable. The first one is to use a tool that includes the icon for you. The other way is to create a resource file and link this resource while compiling your program. I will describe both ways. Either way you need some windows icon file, ending with ".ico". You can either make one yourself (most of the better image processing applications can save as ico), convert a picture into ico or simply leech one from the net.
The "double-click"-way
First you need some kind of tool to include your icon. One excellent tool for that purpose is "Resource Hacker" by Angus Johnson. It's freeware, it's small and reliable. You can download it on http://rpi.net.au/~ajohnson/resourcehacker. Unzip and run it.
The usage of Resource Hacker is fairly easy. Just open your executable and click on "Action" and "Add a new Ressource". Now click on "open file with new resource" and choose your "ico" file. Then enter a name for your icon (can be anything). Click on "add resource" and you are done.
That's about it. Save your file and in your Explorer you see your executable with the new icon. Yaay!
The "programmers"-way
Altough including resources with tools is nice and nifty, there are times when including resources is better by hand. And if you only want to include a simple icon, it's not much more work anyway.
First we need a so called resource file. Open a new document with your favourite editor and enter one line.
50h ICON "youricon.ico"
Now save the file with the name "test.rc". The line simply includes an icon at address 50h (Hex) with the name "youricon.ico". Next you have to compile your resource file. Compile it? Well, you have to create a test.res file with your included "ico" file. To do that we'll use "GoRC". It's a resource compiler and fairly easier than the "windres" tool included with FPC. You can download it on http://www.GoDevTool.com. Copy the exe in the same directory as your .rc file. the .ico has to be there, too. Now run it with "GoRC /r test.rc". And there you have your test.res - the compiled resource file.
Now you have to enter an additional line in your source code. Place it right after your USES are finished.
{$r test.res}
It tells the compiler to include the resource file in your compiled exe. And again, that's about it!
General Stuff
And that's it. Fairly easy, huh? Well, I admit that's not a resource file as "nice programers would do it", but it works nonetheless.
Now some remarks about the icon files themselves. Please don't use 100x100 pixel icons with 256 colors. It's quite useless and they bloat your executable file. 32x32 with 16 colors should be well sufficient. And don't let this stuff get to your head and think about linking all your needed files into your executable. It's not as easy as including a simple icon here ;)
Well, that's it again. See you around.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|