Recent

Author Topic: Drawing to a Panel  (Read 7877 times)

Cran0g

  • New Member
  • *
  • Posts: 28
Drawing to a Panel
« on: June 25, 2011, 02:13:03 pm »
Hi Guys,

I raised a question recently about using resources but although I found a Wiki article about this it was so short and not really to the point I needed it was not overly helpful.  It's becoming a deal killer for me, this difficulty with finding clear information so could some kind person give me a starter for ten?

I used rc.exe to create a resource file called 'resources.res' containing a single gif file with the ID MYDATA.  I seem to have been able to embed it in the program using
Code: [Select]
{$R resources.res} - certainly, no objections are made by lararus.

All I want is to get the image from the resource file and draw it on the panel at a given location. 

Thanks in anticipation.

Cran0g

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12715
  • FPC developer.
Re: Drawing to a Panel
« Reply #1 on: June 25, 2011, 04:06:10 pm »
Have a look at the functions of the units in package fcl-res

Cran0g

  • New Member
  • *
  • Posts: 28
Re: Drawing to a Panel
« Reply #2 on: June 25, 2011, 04:19:54 pm »
I was really hoping for a short (ish) piece of example code.

 :-\

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Drawing to a Panel
« Reply #3 on: June 25, 2011, 04:38:47 pm »
Create the resource config file (the rc file) as described in the Wiki.
Add the include line to your unit for the rc file: {$R <yourfile>.rc}
(not the compiled *.res file).
To use the resource, follow the Wiki (http://wiki.lazarus.freepascal.org/Lazarus_Resources) at the bottom where it says: 'Now let's extract the stored resource into the mydata.dat file'.

(A better topic title would have been 'load image from resource' or something equivalent; drawing to a panel is not related to resources.)
« Last Edit: June 25, 2011, 04:41:47 pm by eny »
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

Cran0g

  • New Member
  • *
  • Posts: 28
Re: Drawing to a Panel
« Reply #4 on: June 25, 2011, 07:08:35 pm »
Hi Eny,

That would be fine if all I wanted to do was to compile a resource and then save it back to file again.  The point of my enquiry is to get that resource and draw it on a panel.  I have found great difficulty trying to get anything to appear on the panel.  BTW, the Resources article in the Wiki to which you refer says
Quote
To simplify the compiling process better to use only the compiled resources in the .res files.
.  That's why I used it instead of the .rc.

So I pretty much out all of that myself by browsing but it's the getting the image from the resource and making it appear on the panel that's the issue.  Thus, my title.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Drawing to a Panel
« Reply #5 on: June 25, 2011, 09:32:29 pm »
This should work:

Code: [Select]
{$R resname.res}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.LoadFromResourceName(hInstance, 'RESNAME');
end;                   

but does not.

Lazarus 0.9.30 r29749 FPC 2.4.2 i386-win32-win32/win64

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Drawing to a Panel
« Reply #6 on: June 25, 2011, 11:43:32 pm »
Alright, the Wiki article is still too complex; no problem (it needs an update anyway).
  • Create a JPEG image and save it in your source folder (e.g. 'picture.jpg')
  • Create a resourcefile by the name 'myresource.rc' and add the following text:
MYPICTURE DATA "picture.jpg"
  • Add the following line to your implementation section: {$R myresource.rc}
  • Drop a TImage on your panel and give it the name 'Image1' (or leave it as it is because it's the default)
  • Load the image with the code below
Code: Pascal  [Select][+][-]
  1. var s: TResourceStream;
  2. begin
  3.   s := TResourceStream.Create(HInstance, 'MYPICTURE', 'DATA');
  4.   image1.Picture.LoadFromStream(s);
  5.   s.Free
  6. end;    

The point of my enquiry is to get that resource and draw it on a panel.  I have found great difficulty trying to get anything to appear on the panel.
Loading an image from a resource and displaying it on a panel are two different things.
Accept this fact and build and test your code accordingly.
It's easier to answer questions if they are broken down to the simplest steps possible (i.e. code fragments) and accompanied by as much information as possible.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Drawing to a Panel
« Reply #7 on: June 25, 2011, 11:58:01 pm »
Now it works fine for me.

Cran0g

  • New Member
  • *
  • Posts: 28
Re: Drawing to a Panel
« Reply #8 on: June 26, 2011, 12:40:07 am »
Brilliant, Eny.  Many thanks for your help.

 

TinyPortal © 2005-2018