如何取得某类文件的图标(100分)

  • 主题发起人 主题发起人 delphilxh
  • 开始时间 开始时间
D

delphilxh

Unregistered / Unconfirmed
GUEST, unregistred user!
我是想利用它的扩展名,取得此一类型的图标,<br>而且是图标在系统 IMAGELIST 中的INDEX<br>
 
uses ShlObj;<br>function GetFileIcon(const AFileName:String; const ASmallIcon:Boolean): HICON;<br>var<br>&nbsp; Flag: Integer;<br>&nbsp; info: TShFileInfo;<br>begin<br>&nbsp; if ASmallIcon then<br>&nbsp; &nbsp; Flag:=(SHGFI_SMALLICON or SHGFI_ICON)<br>&nbsp; else<br>&nbsp; &nbsp; Flag:=(SHGFI_LARGEICON or SHGFI_ICON);<br>&nbsp; SHGetFileInfo(Pchar(AFileName),0,info,Sizeof(info),Flag);<br>&nbsp; Result:=info.hIcon;<br>end;
 
extracticon函数.
 
//设置TreeView或ListView的Images,可以是Small或Large<br>procedure TForm1.SetImageLists;<br>begin<br>&nbsp; //向ListView传递系统的ImageList<br>&nbsp; with ListView1 do<br>&nbsp; begin<br><br>&nbsp; if not Assigned(SmallImages) then<br>&nbsp; begin<br>&nbsp; &nbsp; SmallImages := TImageList.Create(Self);<br>&nbsp; &nbsp; SmallImages.ShareImages := True;<br>&nbsp; &nbsp; SmallImages.Handle := SHGetFileInfo('c:/',<br>&nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; FileInfo,<br>&nbsp; &nbsp; &nbsp; SizeOf(FileInfo),<br>&nbsp; &nbsp; &nbsp; SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES);<br>&nbsp; end;<br>&nbsp; if not Assigned(LargeImages) then<br>&nbsp; begin<br>&nbsp; &nbsp; LargeImages := TImageList.Create(Self);<br>&nbsp; &nbsp; LargeImages.ShareImages := True;<br>&nbsp; &nbsp; LargeImages.Handle := SHGetFileInfo('c:/',<br>&nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; FileInfo,<br>&nbsp; &nbsp; &nbsp; SizeOf(FileInfo),<br>&nbsp; &nbsp; &nbsp; SHGFI_LARGEICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES);<br>&nbsp; end;<br><br>&nbsp; end;<br>end;<br><br>//Tree或ListView的GetImageIndex时调用此代码,把文件名传进去,返回的就是ImageIndex了<br>//Large表示返回的是否大图标的Index,跟上面的设置应保持一致<br>function TForm1.GetFileIconIndex(FileName: string; Large: Boolean): Integer;<br>{ 获取图标的序号函数 }<br>var<br>&nbsp; Ext: string;<br>&nbsp; Flags: Integer;<br>begin<br>&nbsp; Ext := FileName;<br>&nbsp; Flags := SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES;<br>&nbsp; if Large then<br>&nbsp; &nbsp; Flags := Flags or SHGFI_LARGEICON<br>&nbsp; else<br>&nbsp; &nbsp; Flags := Flags or SHGFI_SMALLICON;<br>&nbsp; SHGetFileInfo(PChar(Ext), 0, FileInfo, SizeOf(FileInfo), Flags);<br>&nbsp; Result := FileInfo.iIcon;<br>end;<br><br>
 
后退
顶部