2017-10-14 5 views
1

In Delphi 10.1.2 Berlin, muss ich entweder das große Symbol (32x32) oder das kleine Symbol (16x16) aus an.EXE Datei, unter Verwendung eines spezifischen IconIndex extrahieren:Get Klein oder Groß-Symbol Von Ausführbare Datei

function GetIconFromExecutableFile(const AFileName: string; const Large: Boolean; const AIconIndex: Integer): TIcon; 
var 
    Icon: HICON; 
    ExtractedIconCount: UINT; 
    ThisIconIdx: Integer; 
    F: string; 
begin 
    Result := nil; 
    try 
    ThisIconIdx := AIconIndex; 

    F := AFileName; 
    if Large then 
    begin 
     // error on nil: [dcc32 Error]: E2033 Types of actual and formal var parameters must be identical 
     ExtractedIconCount := ExtractIconEx(PChar(F), ThisIconIdx, Icon, nil, 1); 
    end 
    else 
    begin 
     // error on nil: [dcc32 Error]: E2033 Types of actual and formal var parameters must be identical 
     ExtractedIconCount := ExtractIconEx(PChar(F), ThisIconIdx, nil, Icon, 1); 
    end; 

    Win32Check(ExtractedIconCount = 1); 

    Result := TIcon.Create; 
    Result.Handle := Icon; 
    except 
    Result.Free; 
    raise; 
    end; 
end; 

Die Verwendung von nil für die ausgeschlossene Symbolgröße erzeugt einen Compilerfehler.

Also wie bekomme ich das gewünschte Symbol?

+0

https://stackoverflow.com/a/5955676/6426692 – Sami

+0

, das gut funktioniert . – user1580348

+1

Die Quelle (in WinAPI.ShellAPI.pas) zeigt die Deklaration als 'Funktion ExtractIconEx (lpszFile: LPCWSTR; nIconIndex: Ganzzahl; var phiconLarge, phiconSmall: HICON; nIcons: UINT): UINT; stdcall; ', und eindeutig' nil' kann nicht als 'var' Parameter übergeben werden. –

Antwort

1

Eine Möglichkeit ist die Deklaration der API-Funktion zu beheben:

type 
    PHICON = ^HICON; 
function ExtractIconEx(lpszFile: LPCWSTR; nIconIndex: int; phiconLarge, phiconSmall: PHICON; nIcons: UINT): UINT; stdcall; external shell32 name 'ExtractIconExW' delayed; 

dann verwenden wie:

procedure TForm1.FormCreate(Sender: TObject); 
var 
    Icon : HICON; 
begin 
    if ExtractIconEx(Pchar(ParamStr(0)), 0, @Icon, nil, 1) = 1 then begin 
    self.Icon.Handle := Icon; 
    DestroyIcon(Icon); 
    end; 
Verwandte Themen