Board index » delphi » extracting icons

extracting icons

How do I have to use ExtractAssociatedIcon exactly? Is the filename the
name of the file that contains the icon or is it the file of the type I
want the icon for (for example, if  I want the icon for .doc files,
should I pass the name of a .doc file or the name of the file that
contains the icon). What is the iconindex for? if you know the exact
index of an icon, you could as well use ExtractIcon instead. Last
question : how exactly should I typecast the index? I've tried it like
this :
type
  PWord = ^Word;

var
  Index : PWord;
Index:=@(Number);

This doesn't seem to work and I have no idea how I should do it. Can
somebody maybe explain me why a pointer is used in this function instead
of just the number itself? It looks like they make things difficult on
purpose;-)

 

Re:extracting icons


Koen,
 var whichicon:Word;
begin
 Whichicon:=0;

image1.picture.Icon.handle:=extractassociatedicon(hinstance,'c:\windows\note
pad.exe',Whichicon);
end;

Quote
Koen Van Baelen wrote in message <37839C8F.5A7BC...@pandora.be>...
>How do I have to use ExtractAssociatedIcon exactly? Is the filename the
>name of the file that contains the icon or is it the file of the type I
>want the icon for (for example, if  I want the icon for .doc files,
>should I pass the name of a .doc file or the name of the file that
>contains the icon). What is the iconindex for? if you know the exact
>index of an icon, you could as well use ExtractIcon instead. Last
>question : how exactly should I typecast the index? I've tried it like
>this :
>type
>  PWord = ^Word;

>var
>  Index : PWord;
>Index:=@(Number);

>This doesn't seem to work and I have no idea how I should do it. Can
>somebody maybe explain me why a pointer is used in this function instead
>of just the number itself? It looks like they make things difficult on
>purpose;-)

Re:extracting icons


The message <37839C8F.5A7BC...@pandora.be>
  from  Koen Van Baelen <koen.van.bae...@pandora.be> contains these words:

Quote
> How do I have to use ExtractAssociatedIcon exactly? Is the filename the
> name of the file that contains the icon or is it the file of the type I
> want the icon for (for example, if  I want the icon for .doc files,
> should I pass the name of a .doc file or the name of the file that
> contains the icon). What is the iconindex for? if you know the exact
> index of an icon, you could as well use ExtractIcon instead. Last
> question : how exactly should I typecast the index? I've tried it like
> this :
> type
>   PWord = ^Word;
> var
>   Index : PWord;
> Index:=@(Number);
> This doesn't seem to work and I have no idea how I should do it. Can
> somebody maybe explain me why a pointer is used in this function instead
> of just the number itself? It looks like they make things difficult on
> purpose;-)

Hi Koen,

In Delphi 3, the compiler takes care of most things the Win32 API
calls pointers and you can just treat the iconindex is an unsigned
two byte integer which you should set to zero. This number
is updated to indicate which of the the several icons found in an
executable or resource file the icon handle you get back is for.

If you have Delphi2 then I think in that version you would still
declare IconIndex in the same way except that when you need to use it
you would precede Iconindex with an @ symbol to indicate an address.
The only change in the Delphi 3 example below would be this:-

Result := ExtractAssociatedIcon ( Application.Handle,
                                  pathToIcon,@IconIndex );

Here is the example:-

{================================
 Get Icon Appropriate to Filetype
 ================================}
Function FetchIconHandle(const Fullpath:string):HIcon;
var pathtoIcon : array[0..MAX_PATH] of char;
    IconIndex  : Word;
begin
IconIndex:=0;
StrPcopy(pathToIcon,Fullpath);
Result := ExtractAssociatedIcon ( Application.Handle,
                                  pathToIcon,IconIndex );
end;
--------------------------------------------------------------------
FullPath is a long or short filename to any file which has an
icon associated with it (exe or document it doesnt matter).

PathtoIcon is a nul terminated (C style) string which
is just a copy of FullPath. Windows will overwrite the contents
of PathtoIcon with path of the file containing the icon.

The Function returns the handle to an icon. You assign this
to the handle property of a TICon for use in Delphi.

One Last point, you must always check that the icon handle returned in
non-zero before you use it. Some files do not have an associated icon
and this will cause program crashes and other strange phenomena.

--
regards

Andreas

Other Threads