Recent

Author Topic: TIcon to TBitmap  (Read 11518 times)

Monolith

  • Newbie
  • Posts: 5
TIcon to TBitmap
« on: June 26, 2009, 01:09:36 am »
Since TImageList.AddIcon isnt supported yet, I was looking for a way to add an TIcon to an ImageList.

Here is my work around using Windows API:

Code: [Select]
function TMainForm.AddIconToImageList(Ico:HICON; ImageList:TImageList):Integer;
var
  Bmp : Graphics.TBitmap;
  IconInfo : TIconInfo;
begin
  if not GetIconInfo(Ico,IconInfo) then
    Exit(0); // default Icon
   
  Bmp := Graphics.TBitmap.Create;
  Bmp.SetHandles(IconInfo.hbmColor,IconInfo.hbmMask);
  Bmp.Masked := TRUE;
 
  ImageList.Add(Bmp,nil);
 
  DeleteObject(IconInfo.hbmColor);
  DeleteObject(IconInfo.hbmMask);
  DestroyIcon(Ico);
  Bmp.Free;
 
  Result := ImageList.Count - 1;
end;

I would like to be able to complete this task without GetIconInfo, so I tried a couple of different ways, for instance:

Code: [Select]
function TMainForm.AddIconToImageList(Ico:TIcon; ImageList:TImageList):Integer;
var
  Bmp : Graphics.TBitmap;
begin
  Bmp := Graphics.TBitmap.Create;
  Bmp.Assign(Ico);
  Bmp.Height := 16;
  Bmp.Width := 16;
 
  ImageList.Add(Bmp,nil);
 
  Ico.Free;
  Bmp.Free;
 
  Result := ImageList.Count - 1;
end;

But all I am getting is black boxes. Thanks in advance for any information.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2590
Re: TIcon to TBitmap
« Reply #1 on: June 26, 2009, 02:06:38 am »
Don't change the size of the bmp after you assignd the icon, since this will erase the bitmap.

First select the IconImage you want:
  icon.Current := someindex;
or
  icon.Current := GetBestIndexForSize(ASize);

then assign it to a bitmap:
  bmp.Assign(icon);
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Monolith

  • Newbie
  • Posts: 5
Re: TIcon to TBitmap
« Reply #2 on: June 26, 2009, 04:39:28 am »
I still have a problem getting this to work, for some reason TIcon doesn't appear to get set, the size is always 0, and just straight black background.

Code: [Select]
var
  aIcon : TIcon;
begin
  aIcon := TIcon.Create;
  aIcon.Handle := Ico;
  ...

This code should be working right?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2590
Re: TIcon to TBitmap
« Reply #3 on: June 26, 2009, 10:35:13 am »
Should be working... yes.
But I cant tell you if we made some mistake there.

Have you tried to display the Icon using a timage (just for checking if the icon is OK)?
« Last Edit: June 26, 2009, 10:37:37 am by Marc »
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Monolith

  • Newbie
  • Posts: 5
Re: TIcon to TBitmap
« Reply #4 on: September 25, 2009, 09:33:46 am »
I hate to bring up an old post, but I think I should post my best solution for this. Since the TIcon module is pretty much completely broken or not complete, it is best just to use system api and draw the Icon to the bitmap, for instance:

Code: [Select]
procedure ...(AIcon: HICON);
var
  ABitmap: TBitmap;
begin
  ABitmap := TBitmap.Create;
  ABitmap.SetSize(32,32);
  DrawIcon(ABitmap.Canvas.Handle,0,0,AIcon);

  ... Do stuff

end;

I wish I knew more about the Lazarus library so I could help finish the Icon module, but ... BTW: No, it did not take me this long to figure this out.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2590
Re: TIcon to TBitmap
« Reply #5 on: September 30, 2009, 12:19:25 pm »
Do you mind telling us what is broken or not complete ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018