Recent

Author Topic: [SOLVED] Resource files in Lazarus  (Read 2096 times)

PatBayford

  • Full Member
  • ***
  • Posts: 125
[SOLVED] Resource files in Lazarus
« on: May 22, 2018, 12:17:18 am »
How do I extract individual bitmaps from a loaded resource file please? Used to be easy using the Windows API LoadBitmap function, but Lazarus does not support this, presumably as part of the multi-platform drive.
In fact, I would like to get hold of a good reference to Resource file use in Lazarus - anybody come across one?
« Last Edit: May 22, 2018, 02:38:20 am by PatBayford »
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

wp

  • Hero Member
  • *****
  • Posts: 6485
Re: Resource files in Lazarus
« Reply #1 on: May 22, 2018, 12:39:14 am »
How do I extract individual bitmaps from a loaded resource file please?
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   pic: TPicture;
  4. begin
  5.   pic := TPicture.Create;
  6.   pic.LoadFromResourceName(HINSTANCE, 'BTN_YES');   // <---- read the resource of the program
  7.   Image1.Picture.Assign(pic);
  8.   pic.Free;
  9. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

PatBayford

  • Full Member
  • ***
  • Posts: 125
Re: Resource files in Lazarus
« Reply #2 on: May 22, 2018, 02:37:57 am »
Thanks wp - came up with that route myself - works fine. Just wanted to make sure there was not some other, more direct route.
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

wp

  • Hero Member
  • *****
  • Posts: 6485
Re: Resource files in Lazarus
« Reply #3 on: May 22, 2018, 09:20:40 am »
Thanks wp - came up with that route myself - works fine. Just wanted to make sure there was not some other, more direct route.
More direct than a one-liner?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: [SOLVED] Resource files in Lazarus
« Reply #4 on: May 22, 2018, 10:00:20 am »
 8) And it deserves a link to https://www.youtube.com/watch?v=b19PcuJsQbA
also related to equus asinus.

ccrause

  • Full Member
  • ***
  • Posts: 195
Re: Resource files in Lazarus
« Reply #5 on: May 22, 2018, 11:30:04 am »
Just wanted to make sure there was not some other, more direct route.

An alternative route to working with resources:
Code: Pascal  [Select]
  1. var
  2.   Resource: TResourceStream;
  3. begin
  4.   Resource := TResourceStream.CreateFromID(HInstance, ResID, PChar(ResourceName));
  5.   // the resource is now in a stream, so one can assign, save, read etc.
  6.   Resource.SaveToFile('ImportantData.bin');
  7. ...
This gives you direct access (AFAIK, there may be exceptions) to all resources compiled into the application.

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: [SOLVED] Resource files in Lazarus
« Reply #6 on: May 22, 2018, 12:26:55 pm »
There used to be exceptions(like Windows only...) , but there aren't any anymore (afaik). Resources are even handled correctly cross-platform.(afaik)
also related to equus asinus.