Board index » delphi » Load Icon From Resource Name

Load Icon From Resource Name

I have a Form that I'd like to have different icons for based on a parameter
when the form opens.

In the past, I have used a TSpeedButton and it's Glyph property in
conjunction with the function LoadFromResourceName to change the picture at
runtime from a resourcename.  So, I'm a little familiar with this concept.

I would like to do the same with the Form's Icon property.  I have a feeling
it's something like:
Form1.Icon.LoadFromStream()

But, I'm not sure what type of parameter to pass into the LoadFromStream
function (i.e. how do I get a resource name into a stream var).

Thanks for any info,
Brian

 

Re:Load Icon From Resource Name


Brian Simmons <bsimm...@tampabay.rr.com> schrieb in im Newsbeitrag:
92vk22$5d...@news.gte.com...

Quote
> I have a Form that I'd like to have different icons for based on a
parameter
> when the form opens.

> In the past, I have used a TSpeedButton and it's Glyph property in
> conjunction with the function LoadFromResourceName to change the picture
at
> runtime from a resourcename.  So, I'm a little familiar with this concept.

> I would like to do the same with the Form's Icon property.  I have a
feeling
> it's something like:
> Form1.Icon.LoadFromStream()

> But, I'm not sure what type of parameter to pass into the LoadFromStream
> function (i.e. how do I get a resource name into a stream var).

> Thanks for any info,
> Brian

Hi,
The parameter of LoadFromStream() must be a TStream object.
For loading a resource, create a TResourceStream object with

var Stream: TResourceStream;

Stream := TResourceStream.Create(HInstance, 'ResourceName', 'RT_ICON');

Then call

Form1.Icon.LoadFromStream(Stream);

Finally, make sure to call

Stream.Free;

HTH
-- Florian Haag
http://private.addcom.de/haag/fh/

Re:Load Icon From Resource Name


In article <92vk22$5d...@news.gte.com>, "Brian Simmons"

Quote
<bsimm...@tampabay.rr.com> writes:
>I would like to do the same with the Form's Icon property.  I have a feeling
>it's something like:
>Form1.Icon.LoadFromStream()

>But, I'm not sure what type of parameter to pass into the LoadFromStream
>function (i.e. how do I get a resource name into a stream var).

Something like (untested) . . .

var
  RS : TResourceStream;
begin
  RS := TResourceStream.Create(Self.Handle, 'MyIcon1', ICON);
  Form1.Icon.LoadFromStream(RS);
  RS.Free;

Assuming, of course, that you have included MyIcon1 as a resource in your
executable.

You could also place the icons in a TImageList and use GetIcon to extract it
when you need it.

Alan Lloyd
alangll...@aol.com

Other Threads