如何保存透明图标???(含源码)(50分)

  • 主题发起人 主题发起人 kiss2
  • 开始时间 开始时间
K

kiss2

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure Bmp2Ico(bmp, ico: string); //bmp和ico是文件名
var
IconSizeX : integer;
IconSizeY : integer;
myBmp : TBitmap;
AndMask : TBitmap;
XOrMask : TBitmap;
IconInfo : TIconInfo;
Icon : TIcon;
begin
IconSizeX := GetSystemMetrics(SM_CXICON);
IconSizeY := GetSystemMetrics(SM_CYICON);

myBmp := TBitmap.Create;
myBmp.LoadFromFile(bmp);

AndMask := TBitmap.Create;
AndMask.Width := IconSizeX;
AndMask.Height := IconSizeY;
AndMask.Canvas.Brush.Color := clBlack;
AndMask.Canvas.Rectangle(0, 0, IconSizeX, IconSizeY);

XOrMask := TBitmap.Create;
XOrMask.Width := IconSizeX;
XOrMask.Height := IconSizeY;
StretchBlt(XorMask.Canvas.Handle, 0, 0, IconSizeX, IconSizeY,
myBmp.Canvas.Handle, 0, 0, myBmp.Width, myBmp.Height, SRCCOPY);

Icon := TIcon.Create;
IconInfo.fIcon := true;
IconInfo.xHotspot := 0;
IconInfo.yHotspot := 0;
IconInfo.hbmMask := AndMask.Handle;
IconInfo.hbmColor := XOrMask.Handle;
Icon.Handle := CreateIconIndirect(IconInfo);

AndMask.Free;
XOrMask.Free;
myBmp.Free;

Icon.SaveToFile(ico);
Icon.Free;
end;

如果我想让白色为透明色该怎么做呢??
 
procedure TForm1.Bmp2Ico(bmp, ico: string; TransparentColor: TColor);
var
Icon: TIcon;
bm: TBitmap;
ImageList: TImageList;
begin
Icon := TIcon.Create;
bm := TBitmap.Create;
ImageList := TImageList.CreateSize(32, 32);
bm.LoadFromFile(bmp);
Imagelist.AddMasked(bm, TransparentColor);
Imagelist.GetIcon(0, Icon);
Icon.SaveToFile(ico);
Icon.Free;
bm.Free;
ImageList.Free;
end;
 
能不能把我这段代码增加个透明参数???
 
delphi自带的image editor可以画透明图标
 
后退
顶部