不知你有没有看到RAR里压缩每种文件都有图标,其实这些都是系统图标,并非你所想的是从压缩文件的流中读取的。
类似: 通过Shell外壳 http://www.delphibbs.com/keylife/iblog_show.asp?xid=8018
uses ShellAPI, CommCtrl, ShellCtrls;
procedure SetViewImgListHandle(View: TObject; IcoType: TIcoType);
const
AICOType: array[TIcoType]of DWORD = (SHGFI_LARGEICON, SHGFI_SMALLICON, SHGFI_OPENICON);
var
FileInfo: TSHFileInfo;
ImageList_FileList: THandle;
Flags: UINT;
begin
ImageList_FileList := SHGetFileInfo(PChar('C:/'), 0, FileInfo,
sizeof(TSHFileInfo), SHGFI_SYSICONINDEX or Ord(AICOType[IcoType]));
if ImageList_FileList <> 0 then begin
if View is TComboBoxEx then
begin
PostMessage(TComboBoxEx(View).Handle, CBEM_SETIMAGELIST, 0, ImageList_FileList);
end;
if View is TTreeView then
TreeView_SetImageList(TTreeView(View).Handle, ImageList_FileList, 0);
if View is TListView then begin
case IcoType of
itICON: Flags := LVSIL_NORMAL;
itSmallICON: Flags := LVSIL_SMALL;
else Flags := LVSIL_SMALL;
end;
ListView_SetImageList(TListView(View).Handle, ImageList_FileList, Flags);
end;
end;
end;
function GetFileBySystemDefaultIcon(AFileName: string): Integer;
var
shinfo: TSHFILEINFO;
isCreateFile: Boolean;
Flag: Integer;
I: Integer;
Ext: string;
Count: Integer;
begin
Result := -1;
if Trim(AFileName) = '' then Exit;
try
isCreateFile := false;
if not FileExists(AFileName) then
begin
AFileName := GetWindowsTempFolder + '/' + ExtractFileName(AFileName);
if not FileExists(AFileName) then
//这里是创建一个临时文件而已
isCreateFile := FCFileCreate(AFileName);
end;
Flag := (SHGFI_SMALLICON or SHGFI_ICON);
SHGetFileInfo(Pchar(AFileName), 0, shinfo, Sizeof(shinfo), Flag);
Result := shinfo.iIcon; //只须取得ICO的Index就行了。系统已经把所有图标装载
if isCreateFile then
//删除临时文件
DeleteFile(pchar(AFileName));
except
on E: Exception do
UpdateStatusBar(5, 'GetFileBySystemDefaultIcon: ' + E.Message);
end;
end;
//调用
type
TIcoType = (itICON, itSmallICON, itStateICON);
SetViewImgListHandle(ListView1, itSmallICON);
fileName := ListView1.selected.caption;
ListView1.selected.ImageIndex := GetFileBySystemDefaultIcon(fileName );