Mirror

Extract the associated icon of an application (Views: 11)


Problem/Question/Abstract:

In my program, I am displaying a set of path and file names on the screen as the user choses them using a TList box. Alongside this list box I would like to display the icon associated with each one (if available). I understand that some icons are embedded in the executable and others are associated by Windows. Can you point me in the right direction for determining what the icon is, programmatically extracting it and placing it into an image array.

Answer:

procedure TForm1.Button1Click(Sender: TObject);
var
  HIcon: THandle;
  iIcon: Word;
  FilePath: array[0..MAX_PATH] of Char;
begin
  if OpenDialog1.Execute then
  begin
    Label1.Caption := OpenDialog1.FileName;
    Label1.Repaint;
    StrPCopy(FilePath, OpenDialog1.FileName);
    HIcon := ExtractAssociatedIcon(Application.Handle, @FilePath[0], iIcon);
    if HIcon <> 0 then
      Image1.Picture.Icon.Handle := HIcon;
  end;
end;

<< Back to main page