2017-10-14 103 views
1

在Delphi 10.1.2柏林,我需要提取無論是大圖標(32×32)或從an.EXE文件中的小圖標(16×16),使用特定的IconIndex:獲得小或大圖標在可執行文件

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; 

對排除的圖標大小使用nil會產生編譯器錯誤。

那麼我怎樣才能得到所需的圖標?

+0

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

+0

行之有效。 – user1580348

+1

源代碼(在WinAPI.ShellAPI.pas中)顯示聲明爲'function ExtractIconEx(lpszFile:LPCWSTR; nIconIndex:Integer; var phiconLarge,phiconSmall:HICON; nIcons:UINT):UINT; stdcall;',顯然'nil'不能作爲'var'參數傳遞。 –

回答

1

一種方式是固定的API函數的聲明:

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

然後使用類似:

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;