Delphi编程 -- How do I create an icon from a bitmap?

Question and Answer Database

FAQ2748D.txt   How do I create an icon from a bitmap?
Category   :Windows API
Platform    :All
Product    :All 32 bit 

Question:
How do I create an icon from an bitmap?


Answer:
You must create two bitmaps, a mask bitmap (called the "AND"
bitmap) and a image bitmap (called the XOR bitmap). You can pass the
handles to the "AND" and "XOR"  bitmaps to the Windows API function
CreateIconIndirect() and use the returned icon handle in your
application.

Example:

procedure TForm1.Button1Click(Sender: TObject);
var
  IconSizeX : integer;
  IconSizeY : integer;
  AndMask : TBitmap;
  XOrMask : TBitmap;
  IconInfo : TIconInfo;
  Icon : TIcon;
begin
 {Get the icon size}
  IconSizeX := GetSystemMetrics(SM_CXICON);
  IconSizeY := GetSystemMetrics(SM_CYICON);

 {Create the "And" mask}
  AndMask := TBitmap.Create;
  AndMask.Monochrome := true;
  AndMask.Width := IconSizeX;
  AndMask.Height := IconSizeY;

 {Draw on the "And" mask}
  AndMask.Canvas.Brush.Color := clWhite;
  AndMask.Canvas.FillRect(Rect(0, 0, IconSizeX, IconSizeY));
  AndMask.Canvas.Brush.Color := clBlack;
  AndMask.Canvas.Ellipse(4, 4, IconSizeX - 4, IconSizeY - 4);

 {Draw as a test}
  Form1.Canvas.Draw(IconSizeX * 2, IconSizeY, AndMask);

 {Create the "XOr" mask}
  XOrMask := TBitmap.Create;
  XOrMask.Width := IconSizeX;
  XOrMask.Height := IconSizeY;

 {Draw on the "XOr" mask}
  XOrMask.Canvas.Brush.Color := ClBlack;
  XOrMask.Canvas.FillRect(Rect(0, 0, IconSizeX, IconSizeY));
  XOrMask.Canvas.Pen.Color := clRed;
  XOrMask.Canvas.Brush.Color := clRed;
  XOrMask.Canvas.Ellipse(4, 4, IconSizeX - 4, IconSizeY - 4);

 {Draw as a test}
  Form1.Canvas.Draw(IconSizeX * 4, IconSizeY, XOrMask);

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

 {Destroy the temporary bitmaps}
  AndMask.Free;
  XOrMask.Free;

 {Draw as a test}
  Form1.Canvas.Draw(IconSizeX * 6, IconSizeY, Icon);

 {Assign the application icon}
  Application.Icon := Icon;

 {Force a repaint}
  InvalidateRect(Application.Handle, nil, true);

 {Free the icon}
  Icon.Free;
end;

7/16/98 4:31:28 PM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值