Welcome to the new Friends-of-FPC!

Here you can find all kinds of information about the FreePascal Compiler. We have many tutorials and howtos as well as a selection of tools to help you with your programming. We also have some example codes for you. And if you want to contribute some information/ sources/ tools yourself you can do so.
Also we have finally relaunched the FoFPC forum. It's your chance for some Q&A about everything FreePascal.

Friends-of-FPC

Tutorials: Learn how to code with FreePascal.

Source Codes: A collection of examples, miscellaneous source codes and open source stuff.

Tools and Help Files: Intro- duction of some tools that might help you with FPC.

Community

Forum: Ask or answer questions about the FreePascal Compiler, programming or just babble about coding.

Contribute! Contribute your own Tutorial, Source Codes or Tools and send them to us!

Website

About: Information about Friends-of-FPC.org.

Win32/ FPC - Chapter 9 - by Delax

This chapter is about resource files. In Windows, you use resources for a great number of things. Resource files are external files that are linked into your application. They may contain files, strings or definitions. A file could be an icon, a bitmap or whatever file you want. A string would be a number of characters, for example all the messages that your application uses as output. So for a localization you'd only need to change the strings inside the resource file. Definitions can be Windows menus up to complete Windows dialogs. Yes, a resource file can contain complete definitions for window layouts.

Resource files are written much like a script. Actually, a resource file starts out as a "resource script" and is then compiled into a "resource file" that is then linked into the executable by FPC. But we need to compile the resource script first.

Lucky for us, FPC provides us with a resource compiler calles "WindRES". But unfortunately I won't use WindRES in this tutorial. Instead, I use GoRC. You can get GoRC on http://www.GoDevTool.com. It is easier to use and fairly small. The real problem is, that the resource scripts for WindRES and GoRC are a bit different. So it is not possible to use the code from this tutorial with WindRES. Sorry for that.

Let's start with a (very) simple resource file. We will include a Windows icon into our last example. As a result, the icon will be shown in the explorer next to our application exe. The way of "writing a resource script, compiling it an link it to the executable" also works with complex resource files though.

Now create a new file with your favourite editor. Just add one line:

 1 ICON youricon.ico

Now save it as "source06_resource.rc". Let's have a look at that line. The first number is the ID of this resource. With this ID you can use the resources from your application. Next ist the type of the resource. In this case the type is "ICON" as we include an icon as resource. This is important as we could have included the icon as simple file without special type. But then Windows would not show our icon as icon and we would have to access it differently to use it as an icon. And the third parameter is the name of the actual file. Of course you should replace "youricon.ico" with the name of your icon you want to include.

Now we have to compile the resource script. GoRC is a console application, just like FPC. This means you have to enter the command for compilation manually. Of course you can also use your favourite editor and tell him to remote control GoRC. But for now I will explain how to do it "by hand".

Open a new DOS shell. Change into the directory of your resource script. Now enter "GoRC /r source06_resource.rc". Of couse you need a path to GoRC or GoRC should be in the same directory as the rc. If everything went well you should have a file called "source06_resource.res" in your directory now. If not, check if GoRC works and that you have the script as well as the icon file in the same directory.

Now we have a compiled resource file. Note that it has about the size of the icon. But how do we tell FPC that it should link this file into our application? That's easy. Just add the following line to the source (preferably after the units).

 {$r source06_resource.res}

With this, FPC will link the given resource file into your executable. And that's it - your icon will be shown next to your application in the explorer.

Download the source here and the resource script here. You may as well download an example icon for compilation. Compile them both (first the script, then the source code) and see what happens. In the next chapter, we will actually use some resources in the main application.

Delax/ Sundancer Inc.
[delax@sundancerinc.de]

Back to previous page

Useful Links









Link to us