Mega Code Archive

 
Categories / Delphi / System
 

Function to work with icons in DLL, EXE and ICO files

Title: Function to work with icons in DLL, EXE and ICO files Question: There are a list of solution, but those are what i personally use for that work. Answer: You need to include the ShellAPI unit in your uses clausole! // This returns how many icons are in a file. Function DLLIconsCount( FileName : String ) : Integer; Var IconaGrande : HIcon; IconaPiccola : HIcon; Begin Result := 0; If ( FileExists( FileName ) ) Then Begin Result := ExtractIconEx( PChar( FileName ), -1, IconaGrande, IconaPiccola, 0 ); End; End; // This returns if there're icons in a file Function DLLHasIcons( FileName : String ) : Boolean; Begin Result := ( DLLIconsCount( FileName ) 0 ); End; // This returns a TIcon for a given file and index. Function GetDLLIcon( FileName : String; Index : Integer = 0 ) : TIcon; Begin Result := TIcon.Create; Result.Handle := 0; If ( DLLHasIcons( FileName ) ) Then Begin Try If ( Index If ( Index DLLIconsCount( FileName ) ) Then Index := DLLIconsCount( FileName ); Result.Handle := ExtractIcon( 0, PChar( FileName ), Index ); Finally End; End; End; // This saves an icon from a DLL (EXE or ICO) to a ICO file. Function ExportDLLIcon( OutputFile, InputFile : String; Index : Integer = 0 ) : Boolean; Var Icona : TIcon; Begin Result := False; Icona := GetDLLIcon( InputFile, Index ); If ( Not( Icona.Handle = 0 ) ) Then Try Icona.SaveToFile( OutputFile ); Finally If ( FileExists( OutputFile ) ) Then Result := True; End; Icona.Destroy; End; // This is like ExportDLLIcon, but it saves a bitmap file. Function ExportDLLIconAsBitmap( OutputFile, InputFile : String; Index : Integer = 0 ) : Boolean; Var Icona : TIcon; Immagine : TBitmap; Begin Result := False; Icona := GetDLLIcon( InputFile, Index ); Immagine := TBitmap.Create; If ( Not( Icona.Handle = 0 ) ) Then Try Immagine.Assign( Icona ); Immagine.SaveToFile( OutputFile ); Finally If ( FileExists( OutputFile ) ) Then Result := True; End; Icona.Destroy; Immagine.Destroy; End;