如何得到文件的图标(100分)

  • 主题发起人 主题发起人 MicroZeng
  • 开始时间 开始时间
M

MicroZeng

Unregistered / Unconfirmed
GUEST, unregistred user!
每一个文件,不管什么类型, 在windows中,都有一个对应的图标。<br>请问我如何得取这个图标,并且显示在TListView上。<br>delphi中自带有那个例子。但是确实看不懂。<br>请大虾指点迷津。
 
function GetFileIcon(const Filename:String; SmallIcon:Boolean):HICON; <br>var info:TSHFILEINFO; <br>Flag: Integer; <br>begin <br>if SmallIcon then Flag:=(SHGFI_SMALLICON or SHGFI_ICON) <br>else Flag:=(SHGFI_LARGEICON or SHGFI_ICON); <br>SHGetFileInfo(Pchar(Filename),0,info,Sizeof(info),Flag); <br>Result:=info.hIcon; <br>end;
 
uses ShellAPi, FileCtrl;<br>...<br>var<br>&nbsp; shf &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TShFileInfo;<br>&nbsp; F_icon &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TIcon;<br>&nbsp; FimagesL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TImageList;<br>...<br>begin<br>&nbsp; ...<br>&nbsp; //得到文件的ICO图标<br>&nbsp; &nbsp; &nbsp; ShGetFileInfo(PChar('文件'),//包括完整的路径<br>&nbsp; &nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; &nbsp; shf,<br>&nbsp; &nbsp; &nbsp; &nbsp; sizeof(shf), shgfi_Icon or<br>&nbsp; &nbsp; &nbsp; &nbsp; shgfi_DisplayName or<br>&nbsp; &nbsp; &nbsp; &nbsp; shgfi_sysiconindex or<br>&nbsp; &nbsp; &nbsp; &nbsp; shgfi_SmallIcon);<br>&nbsp; &nbsp; end;<br>&nbsp; F_icon := TIcon.Create;<br>&nbsp; F_icon.Handle := shf.hIcon;<br>&nbsp; FImagesL.AddIcon(F_icon);//把ICO装入ImageList中<br>&nbsp; ...<br>&nbsp; F_Icon.Free;<br>...<br>end;<br><br>在StringGrid的OndrawCell事件中加入代码:<br><br>procedure TForm1.StringGridDrawCell(Sender: TObject; ACol,<br>ARow: Integer; Rect: TRect; State: TGridDrawState);<br>var<br>&nbsp; P_Str : string;<br>begin<br>......<br><br>With StringGrid do<br><br>begin<br>&nbsp; ImgList.GetIcon(3, Image.Picture.Icon);<br>&nbsp; //放一TImageList和一个TImage控件,把ImageList中的第3个图标赋给Image<br>&nbsp; //你可以根据所需取相应的图标<br>&nbsp; P_Str := Trim(StringGrid.Cells[ACol, ARow]);<br>&nbsp; //先把该栏的内容保存<br>&nbsp; Canvas.FillRect(Rect);//清掉矩形框内的内容,试试看,不要这一句会有什么<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //后果:)<br>&nbsp; Canvas.Draw(Rect.Left + 1, rect.Top + 1, Image.Picture.Icon);<br>&nbsp; //画图标<br>&nbsp; Canvas.TextOut(Rect.Left + Image.ClientWidth + 1, Rect.Top + 3,<br>&nbsp; &nbsp; P_str);//写入保存的内容<br>&nbsp; ......<br>end;<br><br>我想TListView也差不多吧<br>wind2000@21cn.com<br>windstorm2000.yeah.net<br><br> <br><br>
 
多人接受答案了。
 
后退
顶部