Link to home
Start Free TrialLog in
Avatar of m_adil
m_adil

asked on

File icon

Hi,
How can i get the icon associated with a file and the type of the file like we see in explorer.
ASKER CERTIFIED SOLUTION
Avatar of Motaz
Motaz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Motaz
Motaz

See this example:

-Drop a button and an Image:
procedure TForm1.Button1Click(Sender: TObject);
var
  Index: Word;
begin
 Index:= 0;
 Image1.Picture.Icon.Handle:=
   ExtractAssociatedIcon(HInstance,
     'c:\autoexec.bat', Index);
end;


Make sure that the file is exists, include ShellApi in uses clause

Motaz

Use this code instead,,, you have to use PChar instead of string,

procedure TForm1.Button1Click(Sender: TObject);
var
  Index: Word;
  H: Integer;
  PFileName: PChar;
  FileName: string;
begin
 Index:= 0;
 PFileName:= StrAlloc(100);
 FileName:= 'c:\Autoexec.bat';
 StrCopy(PFileName, PChar(FileName));
 Image1.Picture.Icon.Handle:=
   ExtractAssociatedIcon(HInstance, PFileName, Index);
end;
Avatar of m_adil

ASKER

Thanks!
But how to get 16x16 size icon of the file, and how can I add this icon to List View control. As List view normally requires image list to show icons. And how to get the file type(the one we see in type column of the explorer). Any example. I'll increase the points
I'm not sure, but you can make the TImage component 16X16 and set it's stretch property to true
Avatar of m_adil

ASKER

Still waiting for a complete answer.
Is it too tough? No body else has passed a single comment.
Actually, Motaz has answer your question.
save this unit as file and register it in component pallete

unit FindIcon;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Registry, ShellApi;

type TeiResult = (eiDefaultIcon,eiExeIcon,eiNoExtensionIcon, eiExtensionIcon);
     TeiIcon = (eiGetFolderOpen, eiGetFolderClose, eiGetDefault,eiGetExe, eiGetNoExt);
     TeiSetIcon = set of TeiIcon;

type
  TRFindIcon = class(TComponent)
  private
    { Private declarations }
    FShellfile: string;
    FFileName: string;
    FLargeIcon, FLargeFolderCloseIcon,
    FLargeFolderOpenIcon, FLargeNoExtIcon, FLargeDefaultIcon,
    FLargeExeIcon, FSmallIcon, FSmallNoExtIcon,
    FSmallFolderCloseIcon, FSmallFolderOpenIcon,
    FSmallDefaultIcon, FSmallExeIcon: TICon;
    FLastIconResult: TeiResult;
    FDefaultChanged: boolean;
    procedure SetLargeDefaultIcon (value: TIcon);
    procedure SetLargeExeIcon (value: TIcon);
    procedure SetLargeNoExtIcon (value: TIcon);
    procedure SetSmallDefaultIcon (value: TIcon);
    procedure SetSmallNoExtIcon (value: TIcon);
    procedure SetSmallExeIcon (value: TIcon);
    procedure SetFileName (value: string);
    function GetIcons: TeiResult;

  protected
    { Protected declarations }

  public
    { Public declarations }
    procedure SetLargeFolderOpenIcon (value: TIcon);
    procedure SetLargeFolderCloseIcon (value:TIcon);
    procedure SetSmallFolderOpenIcon (value: TIcon);
    procedure SetSmallFolderCloseIcon (value:TIcon);
    property SmallFolderOpenIcon: TIcon read FSmallFolderOpenIcon write SetSmallFolderOpenIcon;
    property SmallFolderCloseIcon: TIcon read FSmallFolderCloseIcon write SetSmallFolderCloseIcon;
    property LargeFolderOpenIcon: TIcon read FLargeFolderOpenIcon write SetLargeFolderOpenIcon;
    property LargeFolderCloseIcon: TIcon read FLargeFolderCloseIcon write SetLargeFolderCloseIcon;
    property LargeIcon: TIcon read FLargeIcon;
    property SmallIcon: TIcon read FSmallIcon;
    property LastIconResult: TeiResult read FLastIconResult;

    constructor Create (AOwner: TComponent); override;
    destructor Destroy; override;
    procedure GetDefaultIcon; overload;
    procedure GetDefaultIcon (kind:  TeiSetIcon); overload;

  published
    { Published declarations }
    property FileName: string read FFileName write SetFileName;
    property LargeDefaultIcon: TIcon read FLargeDefaultIcon write SetLargeDefaultIcon;
    property LargeExeIcon: TIcon read FLargeExeIcon write SetLargeExeIcon;
    property LargeNoExtIcon: TIcon read FLargeNoExtIcon write SetLargeNoExtIcon;
    property SmallDefaultIcon: TIcon read FSmallDefaultIcon write SetSmallDefaultIcon;
    property SmallExeIcon: TIcon read FSmallExeIcon write SetSmallExeIcon;
    property SmallNoExtIcon: TIcon read FSmallNoExtIcon write SetSmallNoExtIcon;
  end;



procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Ideias', [TRFindIcon]);
end;

constructor TRFindIcon.Create (AOwner: TComponent);

begin
     inherited Create (AOwner);
     FLargeIcon := TIcon.Create;
     FLargeNoExtIcon := TIcon.Create;
     FLargeExeIcon := TIcon.Create;
     FLargeFolderOpenIcon := TIcon.Create;
     FLargeFolderCloseIcon := TIcon.Create;
     FLargeDefaultIcon := TIcon.Create;
     FSmallIcon := TIcon.Create;
     FSmallNoExtIcon := TIcon.Create;
     FSmallExeIcon := TIcon.Create;
     FSmallFolderOpenIcon := TIcon.Create;
     FSmallFolderCloseIcon := TIcon.Create;
     FSmallDefaultIcon := TIcon.Create;

     FLargeIcon.ReleaseHandle;
     FLargeNoExtIcon.ReleaseHandle;
     FLargeExeIcon.ReleaseHandle;
     FLargeFolderOpenIcon.ReleaseHandle;
     FLargeFolderCloseIcon.ReleaseHandle;
     FLargeDefaultIcon.ReleaseHandle;

     FSmallIcon.ReleaseHandle;
     FSmallNoExtIcon.ReleaseHandle;
     FSmallExeIcon.ReleaseHandle;
     FSmallFolderOpenIcon.ReleaseHandle;
     FSmallFolderCloseIcon.ReleaseHandle;
     FSmallDefaultIcon.ReleaseHandle;
     GetDefaultIcon;

end;



procedure TRFindIcon.GetDefaultIcon;

var Reg: TRegistry;
    handlelarge, handlesmall: array [1..5] of HICON;

begin
     Reg:=TRegistry.Create;
     Reg.RootKey := HKEY_LOCAL_MACHINE;
     Reg.OpenKey ('\SOFTWARE\Microsoft', false);
     if Reg.KeyExists ('Windows NT') then
     begin
        Reg.OpenKey ('Windows NT\CurrentVersion', false);
        fshellfile := '\system32\shell32.dll';
     end
     else
     begin
        Reg.OpenKey ('Windows\CurrentVersion',false);
        fshellfile := '\system\shell32.dll';
     end;
        fshellfile := Reg.ReadString('SystemRoot') + fshellfile;
     Reg.Free;
     ExtractIconEx(Pchar(fshellfile), 0, HandleLarge[1],HandleSmall[1],5);

     FLargeDefaultIcon.Handle := HandleLarge[1];
     FLargeNoExtIcon.Handle := CopyIcon (HandleLarge[1]);
     FLargeExeIcon.Handle := HandleLarge[3];
     FLargeFolderCloseIcon.Handle := HandleLarge[4];
     FLargeFolderOpenIcon.Handle := HandleLarge[5];

     DestroyIcon (HandleLarge[2]);
     DestroyIcon (HandleSmall [2]);

     FSmallDefaultIcon.Handle := HandleSmall[1];
     FSmallNoExtIcon.Handle := CopyIcon (HandleSmall[1]);
     FSmallExeIcon.Handle := HandleSmall[3];
     FSmallFolderCloseIcon.Handle := HandleSmall[4];
     FSmallFolderOpenIcon.Handle := HandleSmall[5];

     FDefaultChanged := true;
end;

procedure TRFindIcon.GetDefaultIcon (kind: TeiSetIcon);

var  HandleLarge, HandleSmall: array [1..5] of HICON;
     f: integer;

begin

     ExtractIconEx(Pchar(fshellfile), 0, HandleLarge[1],HandleSmall[1],5);

     if eiGetDefault in kind then
     begin
          FLargeDefaultIcon.ReleaseHandle;
          FSmallDefaultIcon.ReleaseHandle;
          FLargeDefaultIcon.Handle := CopyIcon(HandleLarge[1]);
          FSmallDefaultIcon.Handle := CopyIcon(HandleSmall[1]);
     end;

     if eiGetNoExt in kind then
     begin
          FLargeNoExtIcon.ReleaseHandle;
          FSmallNoExtIcon.ReleaseHandle;
          FLargeNoExtIcon.Handle := CopyIcon (HandleLarge[1]);
          FSmallNoExtIcon.Handle := CopyIcon (HandleSmall[1]);

     end;

     if eiGetExe in kind then
     begin
          FLargeExeIcon.ReleaseHandle;
          FSmallExeIcon.ReleaseHandle;
          FLargeExeIcon.Handle := CopyIcon(HandleLarge[3]);
          FSmallExeIcon.Handle := CopyIcon (HandleSmall[3]);
     end;

     if eiGetFolderOpen in kind then
     begin
          FLargeFolderOpenIcon.ReleaseHandle;
          FSmallFolderOpenIcon.ReleaseHandle;
          FLargeFolderOpenIcon.Handle := CopyIcon (HandleLarge[5]);
          FSmallFolderOpenIcon.Handle := CopyIcon (HandleSmall[5]);
     end;

     if eiGetFolderClose in kind then
     begin
          FLargeFolderCloseIcon.ReleaseHandle;
          FSmallFolderCloseIcon.ReleaseHandle;
          FLargeFolderCloseIcon.Handle := HandleLarge[4];
          FSmallFolderCloseIcon.Handle := HandleSmall[4];
     end;
     for f := 1 to 5 do
     begin
         DestroyIcon (HandleLarge[f]);
         DestroyIcon (HandleSmall [f]);
     end;
     FDefaultChanged := true;
end;



procedure TRFindIcon.SetFileName(value: string);
begin
     if (LowerCase (ExtractFileExt(value)) = LowerCase (ExtractFileExt(FFileName)))
        and (not FDefaultChanged) then exit;
     FFileName := value;
     FLastIconResult := GetIcons;
end;

procedure TRFindIcon.SetLargeNoExtIcon (value: TIcon);
begin
     FLargeNoExtIcon.Assign(value);
     FDefaultChanged := true;
end;

procedure TRFindIcon.SetLargeDefaultIcon (value: TIcon);
begin
     FLargeDefaultIcon.Assign(value);
     FDefaultChanged := true;
end;

procedure TRFindIcon.SetLargeExeIcon (value: TIcon);
begin
     FLargeExeIcon.Assign(value);
     FDefaultChanged := true;
end;

procedure TRFindIcon.SetLargeFolderOpenIcon (value: TIcon);
begin
     FLargeFolderOpenIcon.Assign(value);
     FDefaultChanged := true;
end;


procedure TRFindIcon.SetLargeFolderCloseIcon (value: TIcon);
begin
     FLargeFolderCloseIcon.Assign(value);
     FDefaultChanged := true;
end;


procedure TRFindIcon.SetSmallNoExtIcon (value: TIcon);
begin
     FSmallNoExtIcon.Assign(value);
     FDefaultChanged := true;
end;


procedure TRFindIcon.SetSmallDefaultIcon (value: TIcon);
begin
     FSmallDefaultIcon.Assign(value);
     FDefaultChanged := true;
end;

procedure TRFindIcon.SetSmallExeIcon (value: TIcon);
begin
     FSmallExeIcon.Assign(value);
     FDefaultChanged := true;
end;

procedure TRFindIcon.SetSmallFolderOpenIcon (value: TIcon);
begin
     FSmallFolderOpenIcon.Assign(value);
     FDefaultChanged := true;
end;


procedure TRFindIcon.SetSmallFolderCloseIcon (value: TIcon);
begin
     FSmallFolderCloseIcon.Assign(value);
     FDefaultChanged := true;
end;


destructor TRFindIcon.Destroy;
begin
     FLargeIcon.Free;
     FLargeExeIcon.Free;
     FLargeFolderOpenIcon.Free;
     FLargeFolderCloseIcon.Free;
     FLargeDefaultIcon.Free;
     FSmallIcon.Free;
     FSmallExeIcon.Free;
     FSmallFolderOpenIcon.Free;
     FSmallFolderCloseIcon.Free;
     FSmallDefaultIcon.Free;
     inherited destroy;
end;



function TRFindIcon.GetIcons: TeiResult;

var handlelarge,
    handlesmall: HICON;
    temp, fileExtension, IconFileName: string;
    iconindex, commapos: integer;
    Reg: TRegistry;

begin
  FDefaultChanged := false;
  Reg := TRegistry.Create;
  FileExtension := ExtractFileExt(FFileName);
  if FileExtension = '' then
  begin
     FLargeIcon.Assign (FLargeNoExtIcon);
     FSmallIcon.Assign (FSmallNoExtIcon);
     result := eiNoExtensionIcon;
     exit;
  end;
  if Lowercase(FileExtension) = '.exe'then
  begin
      FLargeIcon.Assign(FLargeExeIcon);
      FSmallIcon.Assign (FSmallExeIcon);
      result := eiExeIcon;
      exit;
  end;

  try
    with Reg do
     begin
      RootKey := HKEY_CLASSES_ROOT;
      OpenKey(FileExtension, false);
      temp := ReadString('');
      OpenKey('\' + temp, false);
      temp := ReadString('');
      OpenKey('DefaultIcon', false);
      IconFileName := ReadString('');
      commapos := Pos (',',IconFileName);
      IconIndex := strtointDef(Copy (IconFileName,commapos+1,Length (IconFileName)-commapos),-1);
      if iconindex = -1 then
      begin
           FLargeIcon.Assign (FLargeDefaultIcon);
           FSmallIcon.Assign (FSmallDefaultIcon);
           result := eiNoExtensionIcon;
           exit;
      end;
      IconFileName := Copy(IconFileName,1,commapos-1);
      if ExtractIconEx(Pchar(IconFileName), IconIndex, HandleLarge,HandleSmall,1) = NULL then
      begin
           FLargeIcon.Assign (FLargeDefaultIcon);
           FSmallIcon.Assign (FSmallDefaultIcon);
           result := eiNoExtensionIcon;
           exit;
      end;
      FLargeIcon.Handle := HandleLarge;
      FSmallIcon.Handle := HandleSmall;
     end;
     Result := eiExtensionIcon;
  except
     FLargeIcon.Assign (FLargeDefaultIcon);
     FSmallIcon.Assign (FSmallDefaultIcon);
     Result := eiDefaultIcon;
  end;
  Reg.Free;
end;


end.
Avatar of m_adil

ASKER

Hmmm let me try this
Avatar of m_adil

ASKER

Can't compile. I think the error is because of overload function. I'm using Delphi-3. I'm trying to arrange Delphi-5
Bye the way how can I use this control to add icons in the list view control and get the associated file type?