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;