Board index » cppbuilder » icons on the fly?

icons on the fly?

Hello!  I'm trying to create and draw on an icon on the fly.  (IE, use the
TCanvas functions to get an icon.)

I've tried making a TImage, drawing on that, and then accessing the Icon
property, but that doesn't seem to work (Help says the Icon property returns
a blank icon).  Is there any way to draw an icon on the fly?

Thanks!

nathan

 

Re:icons on the fly?


Use the CreateIconFromResourceEx API.

Here's what you do.

1.  Create a TBitmap instance.  Set it to 32x32 pixels.  Draw on it
using it's canvas.

2.  Create another TBitmap.  Assign your first bitmap to the second so
that they are identical.

3.  Use the 'Mask' method of the copy of the bitmap to make it
a monochrome mask.  Set it's PixelFormat to pf1Bit.  Get its bits with
the GetDibSizes and GetDib functions (in graphics.pas)

4.  Set the original bitmap's PixelFormat to pf4Bit - ie. 16-color.

5.  Get the original bitmap's header and bits with GetDIBSizes/GetDIB.

6.  Glue the lot together, into a hunk of memory that contains:

*  The header from the original bitmap (104 bytes)
*  The bits from the original bitmap (512 bytes)
*  The bits from the mono copy of the masked bitmap (128 bytes)

   Total 744 bytes.

7.  Double the 'height' in the header. - eg ((PBITMAPINFOHEADER)
myHunk)->biHeight *= 2;

8.  Call CreateIconFromResourceEx, passing your glued together hunk of
memory as the first parameter.  This returns an HICON that you can give
to a TIcon.

9.  This will only produce 16-color icons.  There seems to be a bug in
graphics.pas that produces an incorrect color table in GetDIB for 256
color (pf8Bit) bitmaps.  Also TIcon only does 16-color icons.

Colin

Re:icons on the fly?


Quote
In article <VA.00000003.00c0503c@godzilla>, Colin Wilson wrote:
> 9.  This will only produce 16-color icons.  There seems to be a bug in
> graphics.pas that produces an incorrect color table in GetDIB for 256
> color (pf8Bit) bitmaps.

Scratch that - but you've got to get your palettes right.

Quote
> Also TIcon only does 16-color icons.

That's true though.  To do 256 color icons (with VCL) you've got to start
again with a TExIcon component derived from TGraphic, using DrawIconEx to
do the work...

Colin

Other Threads