delphi取得文件图标并在TListView中显示
2013-05-01 15:42
232 查看
{delphi取得文件图标并在TListView中显示 技术要点: 一、使用SHGetFileInfo函数获取指定扩展名的文件图标。需要引用ShellAPI单元。 二、使用TStringList来保存扩展名与其图标的索引号。当添加一个文件名至TListView后, 我们已经取得了其图标,再次添加同样扩展名的文件时,不需再次获取其图标,只要从该TStringList中取得其图标索引号即可} uses ShellAPI; var IconList:TStringList; { 实现获取图标及将图标添加到TImageList中的过程 } procedure ListView_SetItemImageIndex(Item: TListItem); var nIndex:Integer; Icon:TIcon; fileName:string; extName:string; sinfo:SHFILEINFO; begin if TListView(Item.ListView).SmallImages<>nil then begin fileName:=Item.Caption; extName:=ExtractFileExt(fileName); nIndex:=IconList.IndexOf(extName); if nIndex>-1 then begin nIndex:=Integer(IconList.Objects[nIndex]); Item.ImageIndex:=nIndex; end else begin FillChar(sinfo, SizeOf(sinfo),0); SHGetFileInfo(PChar(extName),FILE_ATTRIBUTE_NORMAL,sinfo,SizeOf(sInfo), SHGFI_USEFILEATTRIBUTES or SHGFI_ICON or SHGFI_SMALLICON); if sinfo.hIcon>0 then begin Icon:=TIcon.Create; Icon.Handle:=sinfo.hIcon; nIndex:=TListView(Item.ListView).SmallImages.AddIcon(Icon); Icon.Free; Item.ImageIndex:=nIndex; IconList.AddObject(extName,TObject(nIndex)); end; end; end; end; { 测试过程 } procedure TForm1.Button1Click(Sender: TObject); var Item:TListItem; begin Item:=ListView1.Items.Add; Item.Caption:=‘c:\test.jpg‘; ListView_SetItemImageIndex(Item); end; { 对IconList进行初始化及释放 } initialization IconList:=TStringList.Create; finalization IconList.Free; end.
相关文章推荐
- TlistView显示文件图标
- TListView点击表头进行排序和显示图标的方法
- Mac Dropbox 文件不显示同步图标 解决
- TortoiseSVN文件夹及文件图标不显示解决方法
- Delphi获取 EXE、DLL 或 ICO 文件中的图标
- TortoiseSVN检出后的文件图标没有显示
- TortoiseSVN文件夹及文件状态图标不显示解决方法
- Android Studio 那些事|Activity文件前标识图标显示为 j 而是 c
- Windows7 只显示图像文件的图标
- Delphi 实现任务栏多窗口图标显示(使用WS_EX_APPWINDOW风格)
- SVN管理的文件不显示状态图标
- 如何使SharePoint文档库能为PDF文件显示正确的图标
- ECLIPSE里面SVN图标消失,文件状态不显示问题
- python(py2exe)打包成exe执行文件,vista/win7下icon图标显示问题的解决方案
- delphi xe2 下TListView的ViewStyle设置vsList后列宽显示问题
- win10的SVN文件图标不能显示的问题
- TListView组件显示文件夹中的文件
- Delphi 实现任务栏多窗口图标显示
- c#使用系统ImageList显示文件图标
- 如何解决TortoiseSVN文件夹或文件图标不显示