2011-05-10 39 views
6

我需要从EXE中提取所有图标并将它们保存为磁盘文件,但我无法使用最简单的解决方案(ExtractIconEx),因为我需要以某种方式提取ICO文件即使代码在运行Windows XP的系统上运行,也可以保留大尺寸图标。我将在稍后使用其他代码提取想要的图标(128x128和其他vista大小的图标),但我需要一种方式来提取所有图标及其所有资源,因为它们出现在EXE中,而不管PC代码是否运行正在运行XP,Vista或Win7。从EXE中提取所有图标而不使用ExtractIconEx

到目前为止,我知道图标在RT_GROUP_ICONS

我知道有些人在Delphi中一定是做过这个,因为像IcoFX和资源管理器这样的工具似乎已经做到了。我曾经记得看到一个完全开源的Delphi工具,可以做到这一点,但那是几年前。

要重申我用ExtractIconEx所遇到的问题 - 它不会访问整个.ico文件,它只会提取支持的图标资源格式,这是平台支持的任何单个分辨率(大小)。因此,在XP上,它将提取32x32或48x48图标,但不是Vista格式的128x128图标。

更新:这是公认的答案,解决了我的面向未来的忧虑的修改版本。如果我们所调用的函数在未来的Windows版本中从User32.dll中消失,我希望它会更优雅地失败,而不是加载我的整个应用程序。

unit ExtractIconUtils; 

interface 

uses Graphics,Forms,Windows; 

//---------------------------------------------------------------------------- 
// ExtractIcons 
// Call "private" MS Api to extract Icon file. This calls a publically 
// documented function marked as deprecated in the MSDN documentation. 
// It was no doubt Not Originally Intended to be documented, or publically 
// accessed, but it provides functionality that its hard to live without. 
// It exists on Windows 2000, XP, Vista, and Windows7, but might not exist 
// in some future Windows version (released after year 2011). 
// 
// uses global hUserDll : THandle; 
//---------------------------------------------------------------------------- 
function ExtractIcons(exeFilename,icoOutFileName:String;icoSize:Integer):Boolean; 



var 
    hUserDll : THandle; 





implementation 



function ExtractIcons(exeFilename,icoOutFileName:String;icoSize:Integer):Boolean; 
const 
{$ifdef UNICODE} 
ExtractProcName='PrivateExtractIconsW'; 
{$else} 
ExtractProcName='PrivateExtractIconsA'; 
{$endif} 
type 
    TExtractFunc = function(lpszFile: PChar; nIconIndex, cxIcon, cyIcon: integer; phicon: PHANDLE; piconid: PDWORD; nicon, flags: DWORD): DWORD; stdcall; 
var 
    hIcon : THandle; 
    nIconId : DWORD; 
    Icon : TIcon; 
    PrivateExtractIcons:TExtractFunc; 
begin 
    result := false; 
    if (hUserDll<4) then begin 
    hUserDll := LoadLibrary('user32.dll'); 
    if (hUserDll<4) then exit; 
    end; 

    { PrivateExtractIcons: 
     MSDN documentation says that this function could go away in a future windows 
     version, so we must try to load it, and if it fails, return false, rather than 
     doing a static DLL import. 
    } 
    PrivateExtractIcons :=  GetProcAddress(hUserDll, ExtractProcName); 

    if not Assigned(PrivateExtractIcons) then exit; 

    //extract a icoSize x icoSize icon where icoSize is one of 256,128,64,48,32,16 
    if PrivateExtractIcons (PWideChar(exeFilename), 
          0, icoSize, icoSize, @hIcon, @nIconId, 1, LR_LOADFROMFILE) <>0 then 
    try 
     Icon:=TIcon.Create; 
     try 
     Icon.Handle:=hIcon; 
     Icon.SaveToFile(icoOutFileName); 
     result := true; 
     finally 
     Icon.Free; 
     end; 
    finally 
     DestroyIcon (hIcon); 
    end; 
end ; 


initialization 
    // none 

finalization 
    if (hUserDll>4) then 
     FreeLibrary(hUserDll); 

end. 
+0

Vista图标是256px PNG图像,而不是128px,FWIW – 2011-05-10 19:28:56

+1

你试过'PrivateExtractIcons'功能吗? http://msdn.microsoft.com/en-us/library/ms648075 – RRUZ 2011-05-10 19:33:55

+0

这是一个功能性的工作理念,但它让我感到恐慌,它将在未来破裂(感谢MSDN!) – 2011-05-10 20:51:05

回答

9

PrivateExtractIcons功能可以帮到你。您可以指定要提取图标的大小:

{$IFDEF UNICODE} 
    function PrivateExtractIcons(lpszFile: PChar; nIconIndex, cxIcon, cyIcon: integer; phicon: PHANDLE; piconid: PDWORD; nicon, flags: DWORD): DWORD; stdcall ; external 'user32.dll' name 'PrivateExtractIconsW'; 
{$ELSE} 
    function PrivateExtractIcons(lpszFile: PChar; nIconIndex, cxIcon, cyIcon: integer; phicon: PHANDLE; piconid: PDWORD; nicon, flags: DWORD): DWORD; stdcall ; external 'user32.dll' name 'PrivateExtractIconsA'; 
{$ENDIF} 

procedure TMainForm.Button1Click(Sender: TObject); 
var 
    hIcon : THandle; 
    nIconId : DWORD; 
    Icon : TIcon; 
begin 
    //Extract a 128x128 icon 
    if PrivateExtractIcons ('C:\Users\Public\Documents\RAD Studio\Projects\2010\Aero Colorizer\AeroColorizer.exe', 0, 128, 128, @hIcon, @nIconId, 1, LR_LOADFROMFILE) <>0 then 
    try 
     Icon:=TIcon.Create; 
     try 
      Icon.Handle:=hIcon; 
      Icon.SaveToFile(ExtractFilePath(ParamStr(0))+'Aicon.ico'); 
     finally 
      Icon.Free; 
     end; 
    finally 
     DestroyIcon (hIcon); 
    end; 
end ; 
+0

由于页面顶部的警告,我会保持谨慎。 “[此功能不适用于一般用途,可能会在后续版本的Windows中更改或无法使用。]” – 2011-05-10 20:09:56

+0

@Bruce,是的,您说得对,但我个人使用这个功能没有问题,从Windows XP到Windows 7。 – RRUZ 2011-05-10 20:18:22

+0

哎。这在MSDN中是一个可怕的警告。 – 2011-05-10 20:29:34

1

这里是一个working example on Delphi Praxis。它为指定的可执行文件读取默认的RT_GROUP_ICON和关联的RT_ICON资源,并将它们保存为完整的多图像.ICO文件。

它似乎被256像素图像(保存格式无效)弄糊涂,至少在XP上,所以它可能需要一点调整。

+0

看起来它可以提供一个原始解决方案,如果XP-256x256毛刺可以修复。 – 2011-05-11 01:58:38

+0

256像素图像是PNG文件而不是位图。它们是特殊的,因为它们在图标结构中具有高度和宽度值= 0! – 2011-05-11 10:12:09

相关问题