Difference between revisions of "Lazarus Resources"

From Free Pascal wiki
Jump to navigationJump to search
Line 10: Line 10:
 
:<pre><nowiki>lazres mylazarusresource.lrs image.jpg</nowiki></pre>
 
:<pre><nowiki>lazres mylazarusresource.lrs image.jpg</nowiki></pre>
  
To use a lazarus resurce file in your projects include the file with the $I compiler directive in the '''initialization''' section of your  
+
To use a lazarus resource file in your projects include the file with the $I compiler directive in the '''initialization''' section of your unit.
unit.
 
  
You can access the data in the resources with the LoadFromLazarusResource method of the variables which will hold the file contents afterwards. LoadFromLazarusResource requires one string parameter which indicades which object should be loaded from the resource file.
+
You can access the data in the resources directly or with the LoadFromLazarusResource method of the variables which will hold the file contents afterwards. LoadFromLazarusResource requires one string parameter which indicades which object should be loaded from the resource file.
  
 
Example:
 
Example:
Line 21: Line 20:
 
begin
 
begin
 
   Image := TImage.Create;
 
   Image := TImage.Create;
   Image.Picture..LoadFromLazarusResource('image'); // note that there is no need for the extension
+
   Image.Picture.LoadFromLazarusResource('image'); // note that there is no need for the extension
 
end;
 
end;
  
Line 33: Line 32:
 
== Lazarus Resource Form File ==
 
== Lazarus Resource Form File ==
 
Lazarus generate .LRS file from .LFM Form files.  
 
Lazarus generate .LRS file from .LFM Form files.  
But it does not generate it automatically if .LRS file is missing.
 
  
 
When a LRS form file is missing, FPC reports the following error:
 
When a LRS form file is missing, FPC reports the following error:
Line 41: Line 39:
  
 
The easiest way to recreate a .lrs file to make a trivial change in the form design, revert it and save the form, no need to use lazres. [[User:Vincent|Vincent]] 09:47, 6 January 2009 (CET)
 
The easiest way to recreate a .lrs file to make a trivial change in the form design, revert it and save the form, no need to use lazres. [[User:Vincent|Vincent]] 09:47, 6 January 2009 (CET)
 +
 +
==Storing a file in lrs==
 +
 +
You can retrieve the data of the resource with:
 +
 +
uses ...LResources...;
 +
 +
...
 +
procedure TForm1.FormCreate(Sender: TObject);
 +
var
 +
  r: TLResource;
 +
  Data: String;
 +
begin
 +
  r:=LazarusResources.Find('datafile1');
 +
  if r=nil then raise Exception.Create('resource datafile1 is missing');
 +
  Data:=r.Value;
 +
  ...do something with the data...
 +
end;
 +
  
 
== Authors and Contributors ==
 
== Authors and Contributors ==
 
[[User:JayK|JayK]] 21:35, 4 Nov 2005 (CET)
 
[[User:JayK|JayK]] 21:35, 4 Nov 2005 (CET)

Revision as of 14:06, 24 July 2010

Deutsch (de) English (en) español (es) français (fr) 한국어 (ko) русский (ru)

Every win32 developer should know about resource files. They contain data which should be compiled into the exe file. That data could consist of images, string tables, version info, ... even an XP manifest and forms.

So now you cannot use "normal" resource files (*.res) in Lazarus because they are win32 specific. If you want to use your resources anyway, you will have to recreate them with lazres. Lazres can be found in the "Tools" directory (C:\Lazarus\Tools\ - You have to compile the lazres.lpi first)of your Lazarus installation folder (maybe you will first have to compile it).

Then you can compile Lazarus resource files (*.lrs) via command line. The syntax for lazres is:

lazres <filename of resource file> <files to include (file1 file2 file3 ...)>

Example:

lazres mylazarusresource.lrs image.jpg

To use a lazarus resource file in your projects include the file with the $I compiler directive in the initialization section of your unit.

You can access the data in the resources directly or with the LoadFromLazarusResource method of the variables which will hold the file contents afterwards. LoadFromLazarusResource requires one string parameter which indicades which object should be loaded from the resource file.

Example:

procedure exampleproc;
var
  Image: TImage
begin
  Image := TImage.Create;
  Image.Picture.LoadFromLazarusResource('image'); // note that there is no need for the extension
end;

initialization
  {$I mylazarusresource.lrs}

This code includes the file mylazarusresource.lrs into the project. In the procedure exampleproc an icon object is created and loaded from the object "image" out of the resource. The file which was compiled into the resource was probably named image.jpg.

Every class that is derived from TGraphic contains the LoadFromLazarusResource procedure.

Lazarus Resource Form File

Lazarus generate .LRS file from .LFM Form files.

When a LRS form file is missing, FPC reports the following error: ERROR: unit1.pas(193,4) Fatal: Can't open include file "unit1.lrs"

To solve it you must use lazres: c:\lazarus\tools\lazres.exe unit1.lrs unit1.lfm

The easiest way to recreate a .lrs file to make a trivial change in the form design, revert it and save the form, no need to use lazres. Vincent 09:47, 6 January 2009 (CET)

Storing a file in lrs

You can retrieve the data of the resource with:

uses ...LResources...;

... procedure TForm1.FormCreate(Sender: TObject); var

 r: TLResource;
 Data: String;

begin

 r:=LazarusResources.Find('datafile1');
 if r=nil then raise Exception.Create('resource datafile1 is missing');
 Data:=r.Value;
 ...do something with the data...

end;


Authors and Contributors

JayK 21:35, 4 Nov 2005 (CET)