每个windows的文件都有自己的图标。如果要获取这些文件的图标应该怎么做?

解决方案 »

  1.   

    根据扩展名取得相关图标    
      function GetIconFromExt(ext:string;var filename:string;var index:integer):boolean; 
    varreg:tregistry;src:string;beginreg:=tregistry.create;reg.RootKey:=hkey_classes_root;result:=reg.OpenKey('.'+ext,false);if result thenbeginsrc:=reg.ReadString('');reg.CloseKey;if reg.OpenKey(src+'\defaulticon',false) thenbeginsrc:=reg.ReadString('');if pos('%1',src)<>0 then exit;filename:=copy(src,0,pos(',',src)-1);index:=strtoint((trim(copy(src,pos(',',src)+1,length(src)-pos(',',src)))));end;end;reg.Free;end; 
     
       
      

  2.   

    我的信箱是[email protected]
      

  3.   

    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);