Recent

Author Topic: [SOLVED] Change Application icon in taskbar  (Read 7192 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Change Application icon in taskbar
« on: December 16, 2017, 12:01:48 pm »
hi All,
based on application state i need to change the icon presented in taskbar (windows and osx later on).
i came with the below and attached code but i do not see the icon changing.
please advise what i am missing.
thank you

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   appIcon: TBitmap;
  4.  
  5. begin
  6.   try
  7.     appIcon := TBitmap.Create;
  8.  
  9.     ImageList1.GetBitmap(0, appIcon);
  10.     //appIcon.SaveToFile('x.bmp');
  11.     Application.Icon.LoadFromBitmapHandles(appIcon.Handle, 0);
  12.   finally
  13.     FreeAndNil(appIcon);
  14.   end;
  15. end;

Lazarus 1.8 64b on Win 7
« Last Edit: December 17, 2017, 10:30:28 am by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Change Application icon in taskbar
« Reply #1 on: December 16, 2017, 12:23:12 pm »
You also free the icon... appIcon needs to be part of the main form or global.
You need to separate that and wait until application shutdown. You can re-use the same instance multiple times.
Also, at least on windows, the desktop needs a notification to repaint.
« Last Edit: December 16, 2017, 12:25:47 pm by Thaddy »
Specialize a type, not a var.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Change Application icon in taskbar
« Reply #2 on: December 16, 2017, 12:34:50 pm »
i modified code as below. it does not work.
please advise.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   _I := TBitmap.Create;
  4.    ImageList1.GetBitmap(0, _I);
  5. end;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. begin
  9.     Application.Icon.LoadFromBitmapHandles(_I.Handle, 0);
  10.     self.Invalidate;
  11. end;
  12.  
  13. procedure TForm1.FormDestroy(Sender: TObject);
  14. begin
  15.   FreeAndNil(_I);
  16. end;  
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: Change Application icon in taskbar
« Reply #3 on: December 16, 2017, 04:27:49 pm »
Please try attached project.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Change Application icon in taskbar
« Reply #4 on: December 17, 2017, 10:30:15 am »
thank you!
works like a charm.

i have updated http://wiki.lazarus.freepascal.org/Application_Icon with a link to your code in http://wiki.lazarus.freepascal.org/Changing_application_Icon.
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018