Board index » delphi » Getting the icon associated with a file extension?

Getting the icon associated with a file extension?

I'm trying to get the icons associated with particular file extensions (zip
files, DOC files, etc)... Is there some API call for this?  I'm trying to
implement this with a TTreeView (in case someone has some source code)

J. Masters
tr...@circle.net

 

Re:Getting the icon associated with a file extension?


The following three procedures do what you want. I apologise for not
generalising the code! GetIcon is application specific and only serves to
show how I handle the whole process (retrieving icons from either .ICO
files, executable files or associated files as applicable).

procedure GetIcon(Index : integer; IconData : TButtonData; IconDir :
string);
begin
   if (IconData.IconFile <> '') then      //IconData.IconFile is a filename
supposedly containing an icon
   begin
      if IconData.UseLocIcon then  /this is an application specific flag -
ignore
      try
         IconData.Icon.LoadFromFile(IconDir + 'micon' + IntToStr(Index) +
'.ico');
      except
         on EFOpenError do
         begin
            IconData.Icon.Assign(nil);
            MessageDlg('MorphBar could not find the icon file for ' +
IconData.Title +
                                   '. Check that your MorphBar icons
(MICON*.ICO) are located ' +
                                   'in the directory named in the ' +
                                   'Control Dialog. Use the Properties
Dialog to recreate this ' +
                                   'missing icon or switch to using an icon
embedded in ' +
                                   'a Windows application
file.',mtWarning,[mbOK],0);
         end;
      end
      else

//the following is the key code...

      if not GetAppIcon(IconData.Icon, IconData.IconFile, IconData.IconNum)
then
         if not GetAssIcon(IconData.Icon, IconData.IconFile) then
         begin
            IconData.Icon.Assign(nil);
            MessageDlg('MorphBar could not find or associate an icon for '
+ IconData.Title +
                                   '. Check your icon file pathname in the
' +
                                   'Properties Dialog',mtWarning,[mbOK],0);
         end;
   end
   else
      IconData.Icon.Assign(nil);
end;

function GetAppIcon(var theIcon : TIcon; IconFile : TFileName; var IconNum
: longint) : boolean;
begin
   if not FileExists(IconFile) then
   begin
      GetAppIcon := False;
      exit;
   end;
   if Pos('.ICO',IconFile) = 0 then
   begin
      if IconNum = -1 then
         IconNum :=
ExtractIcon(Application.Handle,PChar(IconFile),IconNum)-1;
      theIcon.Handle :=
ExtractIcon(Application.Handle,PChar(IconFile),IconNum);
      if theIcon.handle = 1 then
      begin
         GetAppIcon := False;
         exit;
      end;
      if theIcon.handle = 0 then
      begin
         theIcon.handle :=
ExtractIcon(Application.Handle,PChar(IconFile),0);
         IconNum := 0;
      end;
   end
   else
   begin
      theIcon.LoadFromFile(IconFile);
   end;
   if theIcon.Empty then
      GetAppIcon := False
   else
   begin
      GetAppIcon := True;
   end;
end;

function GetAssIcon(var theIcon : TIcon; IconFile : TFilename) : boolean;
var
   tWord : word;
begin
   if not FileExists(IconFile) then
   begin
      GetAssIcon := False;
      exit;
   end;
   tWord := 0;
   theIcon.handle :=
ExtractAssociatedIcon(Application.Handle,PChar(IconFile),tWord);
   if theIcon.Empty then
      GetAssIcon := False
   else
      GetAssIcon := True;
end;

--
Catch Roj at these locations:

http://www.netkonect.net/~morph
r...@morph.netkonect.co.uk

Trent <tr...@circle.net> wrote in article
<5hv77a$...@news1-alterdial.uu.net>...

Quote
> I'm trying to get the icons associated with particular file extensions
(zip
> files, DOC files, etc)... Is there some API call for this?  I'm trying to
> implement this with a TTreeView (in case someone has some source code)

> J. Masters
> tr...@circle.net

Re:Getting the icon associated with a file extension?


Quote
>   tr...@circle.net (Trent) writes:
>  I'm trying to get the icons associated with particular file extensions (zip
>  files, DOC files, etc)... Is there some API call for this?  I'm trying to
>  implement this with a TTreeView (in case someone has some source code)

Delphi programming secrets by Tom Swan and Jeff Coggswell has this covered in detail.

johan

Other Threads