Bookstore

Recent

Author Topic: SOLVED: Assign Icon in runtime  (Read 188 times)

SaraT

  • Jr. Member
  • **
  • Posts: 80
  • A little student
SOLVED: Assign Icon in runtime
« on: February 19, 2020, 05:38:05 pm »
Hi guys :)
I want to assign an icon file (myIcon.ico) but I have't been able to achieve it... Please help me.

How can I assign this icon loading the icon from Resources OR using Picture.Data = {1754506F727...}?

All this in runtime... Many thanks in advance.
See image below.

Code: Pascal  [Select]
  1. with TImage.Create(Self) do
  2. begin
  3.    Left := 16;
  4.    Height := 48;
  5.    Top := 16;
  6.    Width := 48;
  7.    Picture.Graphic := ---------???;
  8. end;
« Last Edit: February 19, 2020, 07:43:15 pm by SaraT »

winni

  • Hero Member
  • *****
  • Posts: 1114
Re: Assign Icon in runtime
« Reply #1 on: February 19, 2020, 06:40:33 pm »
Hi!

Question:   Picture.Graphic := ---------???;

Answer:

 
Code: Pascal  [Select]
  1. Picture.Icon  := TIcon.create;  // don't know if necessary
  2.  Picture.Icon.LoadFromLazarusResource('myicon');
  3.  

Untested.

Winni

wp

  • Hero Member
  • *****
  • Posts: 6830
Re: Assign Icon in runtime
« Reply #2 on: February 19, 2020, 06:48:21 pm »
I recently did this to show in the About form the largest icon contained in the application's ico file:

Code: Pascal  [Select]
  1.   with AppImage do  // AppImage is a TImage
  2.   begin
  3.     Picture.Assign(Application.Icon);
  4.     Picture.Icon.Current := Picture.Icon.GetBestIndexForSize(Size(Width, Height));
  5.   end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

SaraT

  • Jr. Member
  • **
  • Posts: 80
  • A little student
Re: Assign Icon in runtime
« Reply #3 on: February 19, 2020, 07:42:51 pm »
Many thanks guy.
After trying many times, I got it :D
It works like a charm

Here you go:
Code: Pascal  [Select]
  1. var
  2.  IconFile: TIcon;
  3.  RS: TResourceStream;
  4. begin
  5.     RS := TResourceStream.Create(HInstance, 'ICON', RT_RCDATA);
  6.     try
  7.       IconFile := TIcon.Create;
  8.       IconFile.LoadFromStream(RS);
  9.     finally
  10.       RS.Free;
  11.     end;
  12. end;
  13. with TImage.Create(Self) do
  14. begin
  15.    Left := 16;
  16.    Height := 48;
  17.    Top := 16;
  18.    Width := 48;
  19.    Picture.Icon := IconFile;
  20. end;
« Last Edit: February 19, 2020, 07:44:45 pm by SaraT »

SaraT

  • Jr. Member
  • **
  • Posts: 80
  • A little student
Re: Assign Icon in runtime
« Reply #4 on: February 19, 2020, 07:43:57 pm »
I recently did this to show in the About form the largest icon contained in the application's ico file:

Code: Pascal  [Select]
  1.   with AppImage do  // AppImage is a TImage
  2.   begin
  3.     Picture.Assign(Application.Icon);
  4.     Picture.Icon.Current := Picture.Icon.GetBestIndexForSize(Size(Width, Height));
  5.   end;
Interesting... thanks guys  8)