Delphi programming-how do I create an icon from a bitmap?

Source: Internet
Author: User

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
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.