function Tmainfrm.GetFileIconByExE(const ExEName:string;SmallIcon:boolean):HICON;
var
sinfo: SHFILEINFO;
sFlag: INTEGER;
begin
// if copy(ExEname,1,1)<>'.' then ExEName:='.'+exEName;
//必须有".",比如 .doc, .xls, .PDF
ZeroMemory(@sinfo, sizeof(sinfo));
if smallicon then
sflag:=( SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON or SHGFI_ICON)
else
sflag:=( SHGFI_USEFILEATTRIBUTES or SHGFI_LARGEICON or SHGFI_ICON);
SHGetFileInfo(pchar(ExEName), FILE_ATTRIBUTE_NORMAL,sinfo,sizeof(sinfo), sflag);
Result := sinfo.hIcon; //返回ICON的指针
end;
调用:
procedure Tmainfrm.dbListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
temp_il_i:Integer;
begin
if Item.SubItems.Count >=4 then
begin
img1.Picture.Icon.Handle := GetFileIconByExE(Item.SubItems.Strings[3],false);
//temp_il :=TImageList.Create(il3);
for temp_il_i := 1 to il3.Count - 1 do
begin
il3.Delete(temp_il_i);
end;
il3.AddIcon(img1.Picture.Icon);
Item.ImageIndex :=il3.Count-1;
end
else
begin
Item.ImageIndex :=0;
end;
end;