zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Java之序列化和反序列化
    Java之流的分类
    Java之扫描目录,修改文件内容
    Java之数据流-复制二进制文件
    Java之字符流操作-复制文件
    Java之字节流操作-复制文件
    SQL语句优化原则
    php程序效率优化的一些策略小结
    MYSQL的索引类型:PRIMARY, INDEX,UNIQUE,FULLTEXT,SPAIAL 有什么区别?各适用于什么场合?
    笔记:php有那几种错误提示和查错方法
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/562864.html
Copyright © 2011-2022 走看看