Board index » delphi » Getting Windows icon for a file _type_, not from actual file

Getting Windows icon for a file _type_, not from actual file

Hi,

Two questions:

Question One-

I've been able to use SHGetFileInfo to retrieve the Windows large icon
(SHGFI_Icon) for windows files like notepad.exe, and read the icon into a
TImage or a TImageList.  I have not yet been able to figure out how to
retrieve a windows icon for a file type (without needing to have a copy of
the file type available on disk), such as the default icon for text files.
The only way I see to do this is:

1) Read from the HKEY_CLASSES_ROOT portion of the registry looking key with
the target file's extension
2) Find the associated DefaultIcon Key
3) Use SHGetFileInfo to retrieve the icon as specified

This seems like a lot of work just to get an icon.  Is this the right way,
of am I creating a lot of work for myself?

Question Two-

How do you get a good looking 16x16 version of an icon retrieved using
SHGFI_Icon.  The resizing process performed by a TImageList looks
_terrible_!  For some reason, SHGFI_SmallIcon doesn't seem to work, either.

TIA.

Best Regards,

David Montgomery

 

Re:Getting Windows icon for a file _type_, not from actual file


On Mon, 21 Jun 1999 12:26:33 -0700, "David Montgomery"

Quote
<montgom...@netfile.nospam.net> wrote:
>This seems like a lot of work just to get an icon.  Is this the right way,
>of am I creating a lot of work for myself?

A simple workaround is to create a temporary file (in the TEMP
directory) of the appropriate type.

Quote
>How do you get a good looking 16x16 version of an icon retrieved using
>SHGFI_Icon.  The resizing process performed by a TImageList looks
>_terrible_!  For some reason, SHGFI_SmallIcon doesn't seem to work, either.

Look at the CopyImage API function.

-Steve

Re:Getting Windows icon for a file _type_, not from actual file


Quote
David Montgomery <montgom...@netfile.nospam.net> wrote in message

news:7km39n$6016@forums.borland.com...

Quote
> This seems like a lot of work just to get an icon.  Is this the right way,
> of am I creating a lot of work for myself?

Here 's an easier way from Brad Stowers web site

//GetIconIndex is from Brad Stowers
function GetIconIndex(const AFile: string; Attrs: DWORD): integer;
var SFI: TSHFileInfo;
begin
  SHGetFileInfo(PChar(AFile), Attrs, SFI, SizeOf(TSHFileInfo),
                SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);
  Result := SFI.iIcon;
end;

Just call this function with a fake filename with the extension of the image
you need. For example:

GetIconIndex('Temp.pas',0)

Quote
> How do you get a good looking 16x16 version of an icon retrieved using
> SHGFI_Icon.  The resizing process performed by a TImageList looks
> _terrible_!  For some reason, SHGFI_SmallIcon doesn't seem to work,

either.

Get a copy of the small system imagelist and instead of getting a handle to
the file's icon using SHGFI_Icon, get the index number of the icon instead.
Here's a function that get's the system image list:

procedure SetupSysImage(ImageList:TImageList;Flags: Word);
var AHandle: DWord;
FileInfo: TSHFileInfo;
Begin
  AHandle:=SHGetFileInfo('',0,FileInfo,sizeof(TSHFileInfo),Flags);
  if AHandle<>0 then
    Begin
    ImageList.Handle:=AHandle;
    ImageList.ShareImages:=True;
    ImageList.DrawingStyle:=dsTransparent;
    End;
End;

Here's an example of using it to get the small imagelist:

SetupSysImage(SmallSysImageList,SHGFI_SMALLICON or SHGFI_SYSICONINDEX)

Then you can use the following to get a file's image index (i.e. which image
in the aforementioned imagelist is the file's icon)

function GetImageIndex(FileName:String):Integer;
var FileInfo: TSHFileInfo;
Begin
  if SHGetFileInfo(Pchar(FileName),0,FileInfo,sizeof(TSHFileInfo),
SHGFI_SMALLICON or SHGFI_SYSICONINDEX)<>0 then
    Result:=FileInfo.IIcon
  else
    Result:=0;
End;

Hope this helps,

Gerald

Other Threads