Recent

Author Topic: [solved]Loading Icon at run time  (Read 1382 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
[solved]Loading Icon at run time
« on: December 29, 2019, 03:33:02 am »
I want to load a (SystemTray) icon at runtime, someone has made me a nice set of icons for my app and diferent platforms/themes really require a different icon.

If I convert my PNG files to ICO, it all works but ICO files are big and as I will be shipping several I would rather ship PNG, they are a tenth of the size. I tried -

Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadAltIcon();
  2. var
  3.     AImage : TImage;
  4. begin
  5.     AImage := TImage.Create(self);
  6.     AImage.Picture.LoadFromFile('tomboy24Colour.png');
  7.     TrayIcon1.Icon.Assign(AImage.Picture.Bitmap);
  8. end;  
   

And, while it worked, it changed the colour of my icon, clearly I am not using the graphics as I should. How should I load a PNG into a Icon ?

Davo
« Last Edit: December 29, 2019, 05:01:00 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Loading Icon at run time
« Reply #1 on: December 29, 2019, 03:59:47 am »
Yes, I use PNG.

Code: Pascal  [Select][+][-]
  1. TrayIcon1.Icon.LoadFromFile();
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Loading Icon at run time
« Reply #2 on: December 29, 2019, 04:35:21 am »
Code: Pascal  [Select][+][-]
  1. TrayIcon1.Icon.LoadFromFile();
Yes, thanks GAN, but I had tried that but get an exception :
EInvalidGraphic exception, "Stream is not a an Icon Type"

I have tried a couple of different PNG files too.....

The docs say an incorrect file format will raise an exception but does not tell us just what is an acceptable format.

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Loading Icon at run time
« Reply #3 on: December 29, 2019, 04:49:15 am »
Here is a lot of code with tray icons even with  animation
https://github.com/Arandusoft/fpcpaymowidget/blob/master/umain.pas

If I remember correctly we're using TImageList...

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Loading Icon at run time
« Reply #4 on: December 29, 2019, 05:00:34 am »
While thanks are due to Gan and Iainz, I have just realized I am being quite silly.

The Lazarus icn file is big (137K) but my 24x24 system tray icon, converted to an ICO file, is tiny, some 2.5K so its not worth worrying about. I'll just ship a few ico files and stop fussing. 

Hmm, but some neat tricks in Iainz's link !   Worth some close study .....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: [solved]Loading Icon at run time
« Reply #5 on: December 29, 2019, 05:56:24 am »
Sorry @dbannon you are right, I can use a PNG only from the OI but at runtime get an exception:

Code: Pascal  [Select][+][-]
  1. procedure TCustomIcon.ReadStream(AStream: TMemoryStream; ASize: Longint);
  2. ...
  3.   if (Header.idType <> 1) and (Header.idType <> 2)
  4.   then raise EInvalidGraphic.Create('Stream is not an Icon type');    

Bug?
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [solved]Loading Icon at run time
« Reply #6 on: December 29, 2019, 07:46:50 am »
I am not sure I'd label it a bug, its just that it does not do it.  In the ide there is extra code behind the 'Load' button. I guess if I am the first person to expect the same code at run time, its not really needed ....

Thanks,

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018