home *** CD-ROM | disk | FTP | other *** search
- unit Resunit;
-
- interface
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, ExtCtrls, ShellAPI;
-
- procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
-
- procedure GetIconForDrive( TheType : Integer; var TheIcon : TIcon );
-
- implementation
-
- {$R FIP.RES} { Import custom resource file }
-
- procedure GetIconForDrive( TheType : Integer; var TheIcon : TIcon );
- var TheExt : String; { File extension holder }
- TheOtherPChar , { Windows ASCIIZ string }
- TheResultPChar , { Windows ASCIIZ string }
- ThePChar : PChar; { Windows ASCIIZ string }
- begin
- { Use the data field value to determine which icon to get from RES file }
- case TheType of
- 1 : begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'FLOPPY35' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- end;
- 2 : begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'FIXEDHD' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- end;
- 3 : begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NETWORKHD' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- end;
- 4 : begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'CDROM' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- end;
- 5 : begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'RAM' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- end;
- end;
- end;
-
- { This procedure gets an icon for a file using FindExecutable }
- { and ExtractIcon. (assumes file/dir is passed) }
- procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
- var TheExt : String; { File extension holder }
- TheOtherPChar , { Windows ASCIIZ string }
- TheResultPChar , { Windows ASCIIZ string }
- ThePChar : PChar; { Windows ASCIIZ string }
- begin
- if TheName = 'NO FILE' then
- begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end;
- { Check for directory and if so get directory icon from RES file }
- if (( FileGetAttr( TheName ) and faDirectory ) = faDirectory ) then
- begin
- { Set up the PChar to communicate with Windows }
- GetMem( TheOtherPChar , 255 );
- { Convert Pascal-style string to ASCIIZ Pchar }
- StrPCopy( TheOtherPChar , 'DIRECTORY' );
- { Use API call to return icon handle of Icon Resource in FILECTRL.RES }
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- { Release memory from PChar }
- FreeMem( TheOtherPChar , 255 );
- { Leave }
- exit;
- end;
- { Assume archive file; get its extension }
- TheExt := Uppercase( ExtractFileExt( TheName ));
- { If not an executable/image file then use FindExecutable to get icon }
- if (( TheExt <> '.EXE' ) and ( TheExt <> '.BAT' ) and
- ( TheExt <> '.PIF' ) and ( TheExt <> '.COM' )) then
- begin
- { Grab three chunks of memory }
- GetMem( TheOtherPChar , 255 );
- GetMem( TheResultPChar , 255 );
- GetMem( ThePChar , 255 );
- { Set up the name and its directory in Windows string formats }
- StrPCopy( ThePChar, TheName );
- StrPCopy( TheOtherPChar , ExtractFilePath( TheName ));
- { Use FindExecutable API call to get path and name of owning file }
- if FindExecutable( ThePChar , TheOtherPChar , TheResultPChar ) > 31 then
- begin
- { If get a result of 32 or more then try to get first icon of owner }
- { Using ExtractIcon API call; 0 indicates first icon. }
- TheIcon.Handle := ExtractIcon( hInstance , TheResultPchar , 0 );
- { If a handle is 0 then no icon in owner, get default icon from RES file }
- if TheIcon.Handle = 0 then
- begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end;
- end
- else
- { if no assigned executable, then get default icon from RES file }
- begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end;
- FreeMem( TheOtherPChar , 255 );
- FreeMem( TheResultPChar , 255 );
- FreeMem( ThePChar , 255 );
- end
- else
- { Assume Windows Executable file, so get icon from it with ExtractIcon API }
- begin
- GetMem( ThePChar , 255 );
- StrPCopy( ThePChar , TheName );
- { If no icons in file then get default icon (note use FFFF for -1) }
- if ExtractIcon( hInstance , ThePchar , 65535 ) = 0 then
- begin
- Freemem( ThePChar , 255 );
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end
- else
- begin
- { Try to get first icon for file }
- TheIcon.Handle := ExtractIcon( hInstance , ThePChar , 0 );
- FreeMem( ThePChar , 255 );
- { If handle is 0 invalid icon format so use default from RES file }
- if TheIcon.Handle = 0 then
- begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end;
- end;
- end;
- end;
-
- end.
-