Required : PNGDelphi Component
function PNGtoIcon(const APNG : TPNGObject; ACursor : Boolean = false; AHotSpotX : Integer = 0; AHotSpotY : Integer = 0) : HICON; var Width, Height : Integer; BitmapHeader : PBitmapV5Header; hNewBitmap, hMonoBitmap : HBITMAP; Bits : Pointer; x, y : Integer; DC : HDC; IconInfo : _ICONINFO; Pixel : ^Integer; ScanLine : PRGBTriple; AlphaScanline : pByteArray; begin Width := APNG.Width; Height := APNG.Height; New(BitmapHeader); BitmapHeader.bV5Size := sizeof(BITMAPV5HEADER); BitmapHeader.bV5Width := Width; BitmapHeader.bV5Height := -Height; BitmapHeader.bV5Planes := 1; BitmapHeader.bV5BitCount := 32; BitmapHeader.bV5Compression := BI_BITFIELDS; BitmapHeader.bV5RedMask := $00FF0000; BitmapHeader.bV5GreenMask := $0000FF00; BitmapHeader.bV5BlueMask := $000000FF; BitmapHeader.bV5AlphaMask := $FF000000; DC := GetDC(0); hNewBitmap := CreateDIBSection( DC, PBitmapInfo(BitmapHeader)^, DIB_RGB_COLORS, Bits, 0, 0); Dispose(BitmapHeader); ReleaseDC(0,dc); hMonoBitmap:=CreateBitmap(Width,Height,1,1,nil); Pixel := Bits; for y := 0 to Height-1 do begin ScanLine := APNG.Scanline[y]; AlphaScanline := APNG.AlphaScanline[y]; for x := 0 to Width - 1 do begin Pixel^ := AlphaScanLine[x]; Pixel^ := Pixel^ shl 8; Inc(Pixel^, Scanline^.rgbtRed); Pixel^ := Pixel^ shl 8; Inc(Pixel^, Scanline^.rgbtGreen); Pixel^ := Pixel^ shl 8; Inc(Pixel^, Scanline^.rgbtBlue); Inc(Pixel); Inc(ScanLine); end; end; IconInfo.fIcon := not ACursor; if ACursor then begin IconInfo.xHotspot := AHotSpotX; IconInfo.yHotspot := AHotSpotY; end; IconInfo.hbmMask := hMonoBitmap; IconInfo.hbmColor := hNewBitmap; Result := CreateIconIndirect(IconInfo); DeleteObject(hNewBitmap); DeleteObject(hMonoBitmap); end;
관련글 : MSDN - How To Create an Alpha Blended Cursor or Icon in Windows XP