请教一个获取文件图标的问题。(50分)

  • 主题发起人 主题发起人 iamwind
  • 开始时间 开始时间
I

iamwind

Unregistered / Unconfirmed
GUEST, unregistred user!
本人用TTreeView控件来列出要显示的文件。然后使用ExtractAssociateIcon函数(不知写<br>得对不对)取得与该文件相关联的图标。但遇到一个问题:取得的图标都是32X32大小的,<br>当在TTreeView中显示为List时,文件的图标被强行转换成16X16大小的,从而变得非常难看<br>有的甚至无法辨认。<br>请教怎样才能分别有选择地取得的所需大小的与文件相关联的图标?就像Windows的资源管<br>理器那样。还有就是用ExtractAssociateIcon函数取得bmp文件的关联图标时会得到该bmp文<br>件的缩略图,怎样解决?
 
用SHGetFileInfo函数。<br>参考一下这个<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=594919
 
<br>&nbsp; &nbsp;WINSHELLAPI DWORD WINAPI SHGetFileInfo(<br>&nbsp; &nbsp; LPCSTR &nbsp;pszPath,<br>&nbsp; &nbsp; DWORD &nbsp;dwFileAttributes,<br>&nbsp; &nbsp; SHFILEINFO FAR &nbsp;*psfi,<br>&nbsp; &nbsp; UINT &nbsp;cbFileInfo,<br>&nbsp; &nbsp; UINT &nbsp;uFlags<br>&nbsp; &nbsp;);<br>它的作用是:取回文件系统中的一个对象的信息,对象可以是文件、文件夹、<br>目录或驱动器的根目录,包括大图标、小图标,象资源管理器上的那样。<br>操作过程大体如下:<br>var ShFileInfo: TSHFILEINFO;<br>&nbsp; &nbsp; FileList:TListView;<br>begin<br>&nbsp; ...<br>&nbsp; Result := FileList.Items.Add;<br>&nbsp; with Result do<br>&nbsp; begin<br>&nbsp; &nbsp; Caption:=filename;<br>&nbsp; &nbsp; ShGetFileInfo(pchar(vartostr(filename)), 0, SHFileInfo, SizeOf(SHFileInfo),<br>&nbsp; &nbsp; &nbsp; &nbsp; SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME)=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage('error in shgetfileinfo');<br>&nbsp; &nbsp; ImageIndex := SHFileInfo.iIcon;<br>&nbsp; end;<br>&nbsp; ...<br>end;<br><br>***********************************************************************<br><br>ExtractAssociatedIcon()和ExtractIcon()函数,<br>也可以提取文件的图标,但是速度要慢上许多<br><br>uses ShellApi;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Icon : hIcon;<br>&nbsp; IconIndex : word;<br><br>begin<br>&nbsp;Icon := ExtractAssociatedIcon(HInstance,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'C:/SomePath/SomeFile.ext',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconIndex);<br>&nbsp;DrawIcon(Form1.Canvas.Handle, 10, 10, Icon);<br>end;
 
多谢二位,问题已解决。分数不多,请见谅!
 
后退
顶部