ShGetFileInfo-如何取得一种文件的系统显示图标?(50分)

J

jame

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我寻找某个文件图标的程序,但是运行后出现好多问题:<br>1.windows全部图标消失了<br>2.取得的图标不是windows显示的图标<br>谁能给我一个详细的例子说明如何去的一个文件名字已知的图标?<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; fn:string;<br>&nbsp; SFi:TSHFILEINFO;<br>&nbsp; HIMG:hicon;<br>begin<br>&nbsp; fn := Fr.FileName;<br>&nbsp; IL.Clear; &nbsp;<br>&nbsp; IL.handle :=ShGetFileInfo(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pchar('.htm'),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Pchar(fn),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //'C:',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SFi,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SizeOf(SFI),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SHGFI_SYSICONINDEX or SHGFI_LARGEICON );<br>&nbsp; LV.Items.Clear;<br>&nbsp; With lv.items.add do<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; caption := fn;<br>&nbsp; &nbsp; &nbsp; imageindex := 0; <br>&nbsp; end;<br>&nbsp; IL.free;<br>end;
 
请问一次如何取得多个类型文件的图标?
 
<br>uses shellapi;<br><br>第一步 &nbsp;取得系统的图标列表的句柄,将之赋予一个图像列表控件。<br>procedure GetSystemImageList(imagelist:TImageList);<br>var<br>&nbsp; &nbsp; SysIL: THandle;<br>&nbsp; &nbsp; SFI: TSHFileInfo;<br>begin<br>&nbsp; &nbsp; // 取小图标,如果将SHGFI_SMALLICON替换成<br>&nbsp; &nbsp; //SHGFI_LARGEICON则表示取大图标<br>&nbsp; &nbsp; SysIL := SHGetFileInfo('', 0, SFI, SizeOf(SFI),<br>&nbsp; &nbsp; &nbsp; &nbsp; SHGFI_SYSICONINDEX or SHGFI_SMALLICON);<br>&nbsp; &nbsp; if SysIL &lt;&gt; 0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //将imagelist的图像列表句柄指向系统图像句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; imagelist.Handle := SysIL;<br>&nbsp; &nbsp; &nbsp; &nbsp; //防止组件释放时释放图像句柄,很重要<br>&nbsp; &nbsp; &nbsp; &nbsp; imagelist.ShareImages := TRUE;<br>&nbsp; &nbsp; end;<br>end;<br><br>第二步 &nbsp;取得要处理文件的图标索引<br>//取一个文件的图标索引<br>function GetIconIndex(const AFile: string; Attrs: DWORD): integer;<br>//Attrs可以为表示文件或路径FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY<br>var<br>&nbsp; &nbsp; SFI: TSHFileInfo; &nbsp; &nbsp; &nbsp; <br>begin<br>&nbsp; &nbsp; SHGetFileInfo(PChar(AFile), Attrs, SFI, SizeOf(TSHFileInfo),<br>&nbsp; &nbsp; SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);<br>&nbsp; &nbsp; Result := SFI.iIcon;<br>end;<br><br>实例调用:<br>//如在TreeView中得到c:/mydir的图标,因为是路径所以要加上路径的标志<br>aNode.ImageIndex := GetIconIndex('c:/mydir/',<br>&nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY);<br>//如在TreeView中得到c:/index.html的图标<br>aNode.ImageIndex := GetIconIndex('c:/index.html',FILE_ATTRIBUTE_NORMAL);
 
gcq都说完了。
 
出错的关键,就是IL.Handle := ...<br>然后又IL.Free;<br><br>IL.Free把包含有系统图标的系统图像列表释放了。<br><br>应该改写为<br><br>Temp := IL.Handle;<br>try<br>&nbsp; IL.Handle := ...<br>&nbsp; ...<br>finally<br>&nbsp; IL.Handle := Temp;<br>end;<br>...<br>IL.Free
 
接受答案了.
 

Similar threads

顶部