2017-10-14 110 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;