* * *

Author Topic: Lazarus Resource file problems  (Read 304 times)

dietmar

  • Jr. Member
  • **
  • Posts: 76
Lazarus Resource file problems
« on: September 11, 2017, 10:20:38 pm »
Hi,

I have a splash form unit where I load an image from a resource file. I added
"initialization

{$I app.lrs}"

and this works perfectly.

Now I have a second form where I want to user other pictures also stored in the resource file named above.
If I omit the "$I...", no image is loaded - ok.
If I add the resource file like in the splash form, I get the following errors:

"Compile Project, Target: app.exe: Exit code 1, Errors: 10
app.lrs(1,1) Error: Identifier not found "LazarusResources"
app.lrs(65,3) Error: Type conflict between set elements"

What is that and how can I fix it?

Regards,
Dietmar
Lazarus 1.6.4 with FPC 3.0.2 on Windows10 64Bit

RAW

  • Sr. Member
  • ****
  • Posts: 488
Re: Lazarus Resource file problems
« Reply #1 on: September 12, 2017, 12:31:48 am »
I'm not a big fan of res, lrs and rc files... why don't you use PROJECT -> PROJECT OPTIONS -> RESOURCES ???
I checked version 1.8.0RC4 and this is working very fine...

// USES LCLType (RT_RCDATA)
Code: Pascal  [Select]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.   Var
  3.    RS : TResourceStream;
  4.    PNG: TPicture;
  5.  Begin
  6.   PNG:= TPicture.Create;
  7.    Try
  8.     RS:= TResourceStream.Create(HInstance, 'A', RT_RCDATA);
  9.      Try
  10.       PNG.LoadFromStream(RS);
  11.       Image1.Picture:= PNG;
  12.      Finally
  13.       RS.Free;
  14.      End;
  15.    Finally
  16.     PNG.Free;
  17.    End;
  18.  End;

Code: Pascal  [Select]
  1. // or like this... is easier... :-)
  2.  
  3. Procedure TForm2.FormClick(Sender: TObject);
  4.   Var
  5.    RS: TResourceStream;
  6.  Begin
  7.   RS:= TResourceStream.Create(HInstance, 'B', RT_RCDATA);
  8.    Try
  9.     Image1.Picture.LoadFromStream(RS);
  10.    Finally
  11.     RS.Free;
  12.    End;
  13.  End;
« Last Edit: September 12, 2017, 12:43:40 am by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

Ericktux

  • Full Member
  • ***
  • Posts: 139
Re: Lazarus Resource file problems
« Reply #2 on: September 12, 2017, 07:07:43 am »
you can try the following friend

create two-image resource file:
C:\lazarus\tools>lazres.exe imgs.lrs splash_image.png tux.png

now add to the project:

Code: Pascal  [Select]
  1. uses
  2.  . . . .   LResources;
  3.  
  4.  
  5. . . . .
  6. procedure TForm1.FormCreate(Sender: TObject);
  7. begin
  8.   // your codes...
  9. end;
  10.  
  11. Initialization
  12. {$I imgs.lrs}
  13. end.

I attached an example and the complete code.
i hope it serves you friend  :) :)

complete code: (FORM1)

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes,
  9.   SysUtils,
  10.   FileUtil,
  11.   Forms,
  12.   Controls,
  13.   Graphics,
  14.   LResources,
  15.   Unit2,
  16.   Dialogs,
  17.   ExtCtrls,
  18.   Buttons;
  19.  
  20. type
  21.  
  22.   { TForm1 }
  23.  
  24.   TForm1 = class(TForm)
  25.     BitBtn1: TBitBtn;
  26.     Image1: TImage;
  27.     procedure BitBtn1Click(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.   private
  30.  
  31.   public
  32.  
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.FormCreate(Sender: TObject);
  45. begin
  46.   Image1.Picture.LoadFromLazarusResource('splash_image');
  47. end;
  48.  
  49. procedure TForm1.BitBtn1Click(Sender: TObject);
  50. begin
  51.   Form1.hide;
  52.   form2.show;
  53. end;
  54.  
  55. Initialization
  56. {$I imgs.lrs}
  57. end.  
  58.  

complete code: (FORM2)

Code: Pascal  [Select]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm2 }
  13.  
  14.   TForm2 = class(TForm)
  15.     Image1: TImage;
  16.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form2: TForm2;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm2 }
  32.  
  33. procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  34. begin
  35.   Application.Terminate;
  36. end;
  37.  
  38. procedure TForm2.FormCreate(Sender: TObject);
  39. begin
  40.   Image1.Picture.LoadFromLazarusResource('tux');
  41. end;
  42.  
  43. end.


regards.  :)



dietmar

  • Jr. Member
  • **
  • Posts: 76
Re: Lazarus Resource file problems
« Reply #3 on: September 12, 2017, 05:30:04 pm »
Hi,

moving them to the project resources works like a charm! Thank you all.

Dietmar
Lazarus 1.6.4 with FPC 3.0.2 on Windows10 64Bit

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus