Board index » delphi » How to obtain the icon associated with a file type

How to obtain the icon associated with a file type

Hi,

Starting from a file extension (e.g. TXT) I would like to end up with
the associated icon as shown in the Windows File Explorer.

Any suggestions are welcome.

Nelis

 

Re:How to obtain the icon associated with a file type


Quote
Nelis Bijl <nb...@redwood.nl> wrote:
> Starting from a file extension (e.g. TXT) I would like to end up with
> the associated icon as shown in the Windows File Explorer.

Below you find a function which return a handle of a icon from a file type
(e.g. TXT) by looking through the registry, you can then insert this by

form1.icon.handle:=GetIconFromType('txt',ShellLarge);

uses  windows,ShellApi;

type
  TIconKind=(ShellSmall, ShellLarge);

function GetIconFromType(const typename:string; kind:TIconKind):HIcon;
var
  s,t:string;
  h:THandle;
  x,y,i:integer;
begin
  result:=0;
  s:=typename+'\DefaultIcon'+#0;
  if RegOpenKeyEx(HKEY_CLASSES_ROOT,@s[1],0,key_read,h)=error_success then
begin
    s:=#0;
    x:=255;
    setlength(t,x);
    if RegQueryValueEx(h,@s[1],NIL,@y,@t[1],@x)=error_success then begin
      try
        s:=copy(t,1,x);
        t:=copy(s,1,pos(',',s)-1)+#0;
        s:=copy(s,pos(',',s)+1,length(s));
        i:=0;
        try
          i:=strtoint(s);
        except
          end;
        case kind of
          ShellSmall: y:=ExtractIconEx(@t[1],i,h,x,1);
          ShellLarge: y:=ExtractIconEx(@t[1],i,x,h,1);
          end;
        result:=x;
      except
        end;
      end;
    end;
  end;

--
Andreas H"orstemeier                       | "We are not interested in
email: a...@scp.de                         | little green men, but in little
       a...@hoerstemeier.de                | green women."
www:   http://www.westend.de/~hoerstemeier | (Edwin "Buzz" Aldrin)
fido:  2:2452/108     ICQ: 1661968         |

Re:How to obtain the icon associated with a file type


Andreas Hoerstemeier schrieb in Nachricht
<6equ1t$...@dilbert.scp.de>...

Quote
>Nelis Bijl <nb...@redwood.nl> wrote:
>> Starting from a file extension (e.g. TXT) I would like to end up
with
>> the associated icon as shown in the Windows File Explorer.
>Below you find a function which return a handle of a icon from a file
type
>(e.g. TXT) by looking through the registry, you can then insert this

by

[Code using RegQueryValue(HKEY_CLASSES_ROOT for a certain type)
directly and ExtractIcon]

Hi Andreas

No doubt your code works well, but it is not really the portable way
of doing things. There is a shell function called SHGetFileInfo (in
ShellAPI), which gives you all this information too.

If Windows ever changes the way, this info is stored in the Registry,
or even between different types of Win32 (NT4 or 95 or even 98), your
way will not work anymore. Anyway, afaik this is not (well) documented
either. SHGetFileInfo is documented in win32.hlp.

Regards
--
Rudy Velthuis
-------------------------------------------------
. Gladbeck, Germany
. E-Mail: rvelthuis at cww dot de
-------------------------------------------------

Other Threads