Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get icon from the exe file

Status
Not open for further replies.

Spent

Programmer
Mar 20, 2003
100
BG
I don't know how to get the icon of an existing exe.How can I get it and place it on a SpeedButton as a Glyph. Thanks.
 
Here's an example I quickly knocked up. It does the following:
- asks you to specify a file
- extracts the icon for that file using the API call ExtractAssociatedIcon
- converts the icon to a bitmap
- assigns the bitmap to the Glyph property of a speed button:
Code:
var
  IconIndex: word;
  Buffer: array[0..2048] of char;
  IconHandle: HIcon;
  Bitmap : TBitmap;
  OpenDialog: TOpenDialog;
begin
  OpenDialog := TOpenDialog.Create(nil);
  try
    if OpenDialog.Execute then
    begin
      StrCopy(@Buffer, PChar(OpenDialog.FileName));
      IconIndex := 0;
      IconHandle := ExtractAssociatedIcon(HInstance, Buffer, IconIndex);
      if IconHandle <> 0 then
        Icon.Handle := IconHandle;
      Bitmap := TBitmap.Create;
      try
        Bitmap.Width := Icon.Width;
        Bitmap.Height := Icon.Height;
        Bitmap.Canvas.Draw(0, 0, Icon);
        SpeedButton1.Glyph.Assign(Bitmap);
      finally
        Bitmap.Free;
      end;
    end;
  finally
    OpenDialog.Free;
  end;
end;

The sizing needs a little tweak.

A simpler version which just grabs the application's icon is as follows:
Code:
var
  Bitmap : TBitmap;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.Width := Application.Icon.Width;
    Bitmap.Height := Application.Icon.Height;
    Bitmap.Canvas.Draw(0, 0, Application.Icon);
    SpeedButton1.Glyph.Assign(Bitmap);
  finally
    Bitmap.Free;
  end;
end;

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top