首页  编辑  

系统图像列表

Tags: /超级猛料/OS.操作系统/   Date Created:

uses shellapi;

第一步  取得系统的图标列表的句柄,将之赋予一个图像列表控件。

procedure GetSystemImageList(imagelist:TImageList);

var

   SysIL: THandle;

   SFI: TSHFileInfo;

begin

   // 取小图标,如果将SHGFI_SMALLICON替换成

   //SHGFI_LARGEICON则表示取大图标

   SysIL := SHGetFileInfo('', 0, SFI, SizeOf(SFI),

       SHGFI_SYSICONINDEX or SHGFI_SMALLICON);

   if SysIL <> 0 then begin

       //将imagelist的图像列表句柄指向系统图像句柄

       imagelist.Handle := SysIL;

       //防止组件释放时释放图像句柄,很重要

       imagelist.ShareImages := TRUE;

   end;

end;

第二步  取得要处理文件的图标索引

//取一个文件的图标索引

function GetIconIndex(const AFile: string; Attrs: DWORD): integer;

//Attrs可以为表示文件或路径FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY

var

   SFI: TSHFileInfo;      

begin

   SHGetFileInfo(PChar(AFile), Attrs, SFI, SizeOf(TSHFileInfo),

           SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);

   Result := SFI.iIcon;

end;

//获取一个文件的imageindex;

function GetIconIndex(const APath: string; Attrs: DWORD): integer;

var

 SFI: TSHFileInfo;

begin

 if FileExists(APath) or DirectoryExists(APath) then

   // If the file or directory exists, just let Windows figure out it's attrs.

   SHGetFileInfo(PChar(APath), 0, SFI, SizeOf(TSHFileInfo),

                 SHGFI_SYSICONINDEX)

 else

   // File doesn't exist, so Windows doesn't know what to do with it.  We have

   // to tell it by passing the attributes we want, and specifying the

   // SHGFI_USEFILEATTRIBUTES flag so that the function knows to use them.

   SHGetFileInfo(PChar(APath), Attrs, SFI, SizeOf(TSHFileInfo),

                 SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);

 Result := SFI.iIcon;

end;

实例调用:

//如在TreeView中得到c:\mydir的图标,因为是路径所以要加上路径的标志

aNode.ImageIndex := GetIconIndex('c:\mydir\',

    FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY);

//如在TreeView中得到c:\index.html的图标

aNode.ImageIndex := GetIconIndex('c:\index.html',FILE_ATTRIBUTE_NORMAL);