擷取程式大表徵圖方案

來源:互聯網
上載者:User

擷取檔案的表徵圖

在win7的檔案狀態列中能顯示最大256x256的程式應用表徵圖。

在XP下測試無法正常擷取256的巨型表徵圖

Shell提供了一個函數 SHGetFileInfo 可以擷取檔案資訊,在使用此函數有需要處理Icon控制代碼的釋放(DestroyIcon),否則每次會有3個GDI控制代碼泄漏問題。

使用此函數後會一次性產生47個GDI控制代碼,只要Icon控制代碼釋放,就不會再增長。現在還未找到處理多處理啊的調用所產生的這些GDI控制代碼。

 

注意:

  GHGetFileInfo 和 DestoryIcon 成對調用。

 

測試環境

   Win7 and XE2

 

 

View Code

unit Unit4;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type  TForm4 = class(TForm)    btn1: TButton;    procedure btn1Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  Form4: TForm4;implementationuses  ShellApi, Commctrl, ShlObj;{$R *.dfm}const  SHIL_LARGE     = $00;  //The image size is normally 32x32 pixels. However, if the Use large icons option is selected from the Effects section of the Appearance tab in Display Properties, the image is 48x48 pixels.  SHIL_SMALL     = $01;  //These images are the Shell standard small icon size of 16x16, but the size can be customized by the user.  SHIL_EXTRALARGE= $02;  //These images are the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user.  SHIL_SYSSMALL  = $03;  //These images are the size specified by GetSystemMetrics called with SM_CXSMICON and GetSystemMetrics called with SM_CYSMICON.  SHIL_JUMBO     = $04;  //Windows Vista and later. The image is normally 256x256 pixels.  IID_IImageList: TGUID= '{46EB5926-582E-4017-9FDF-E8998DAA0950}';function GetImageListSH(SHIL_FLAG:Cardinal): HIMAGELIST;type  _SHGetImageList = function (iImageList: integer; const riid: TGUID; var ppv: Pointer): hResult; stdcall;var  Handle        : THandle;  SHGetImageList: _SHGetImageList;begin  Result:= 0;  Handle:= LoadLibrary('Shell32.dll');  if Handle<> S_OK then  try    SHGetImageList:= GetProcAddress(Handle, PChar(727));    if Assigned(SHGetImageList) and (Win32Platform = VER_PLATFORM_WIN32_NT) then      SHGetImageList(SHIL_FLAG, IID_IImageList, Pointer(Result));  finally    FreeLibrary(Handle);  end;end;procedure GetIconFromFile(aFile:String; var aIcon : TIcon;SHIL_FLAG:Cardinal);var  aImgList    : HIMAGELIST;  SFI         : TSHFileInfo;Begin    //Get the index of the imagelist    SHGetFileInfo(PChar(aFile), FILE_ATTRIBUTE_NORMAL, SFI,                 SizeOf( TSHFileInfo ), SHGFI_ICON or SHGFI_LARGEICON or SHGFI_SHELLICONSIZE or                 SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_DISPLAYNAME );    if not Assigned(aIcon) then    aIcon:= TIcon.Create;    //get the imagelist    aImgList:= GetImageListSH(SHIL_FLAG);    //extract the icon handle    aIcon.Handle:= ImageList_GetIcon(aImgList, Pred(ImageList_GetImageCount(aImgList)), ILD_NORMAL);    DestroyIcon(SFI.hIcon);    ImageList_Destroy(aImgList);end;procedure TForm4.btn1Click(Sender: TObject);var  icon:TIcon;begin  icon := TIcon.Create;  try    GetIconFromFile(ParamStr(0), icon, SHIL_JUMBO);    canvas.Draw(10, 100, icon);    Canvas.TextOut(10, 80, inttostr(icon.Handle));    GetIconFromFile(ParamStr(0), icon, SHIL_SYSSMALL);    canvas.Draw(10+ 280 , 100, icon);    Canvas.TextOut(10 + 280, 80, inttostr(icon.Handle));    GetIconFromFile(ParamStr(0), icon, SHIL_EXTRALARGE);    canvas.Draw(10+ 280 + 150 , 100, icon);    Canvas.TextOut(10+ 280 + 150, 80, inttostr(icon.Handle));    GetIconFromFile(ParamStr(0), icon, SHIL_LARGE);    canvas.Draw(10+ 280 + 150 +50 , 100, icon);    Canvas.TextOut(10+ 280 + 150 +50, 80, inttostr(icon.Handle));    GetIconFromFile(ParamStr(0), icon, SHIL_SMALL);    canvas.Draw(10+ 280 + 150 +50 +40, 100, icon);    Canvas.TextOut(10+ 280 + 150 +50 +40, 80, inttostr(icon.Handle));  finally    icon.Free;  end;end;end.

 

詳細內容可以看

http://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using

有比較詳細的使用說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.