根据文件的扩展名取得关联图标(100分)

  • 主题发起人 主题发起人 xuxiaohan
  • 开始时间 开始时间
X

xuxiaohan

Unregistered / Unconfirmed
GUEST, unregistred user!
问题如题:<br>我是通过通过SHGetFileInfo,建立个临时文件取得的,我认为不够理想。有以下疑问:<br>1. pshPath必须为存在的文件名才能取得文件的关联图标?<br>2. 是否必须建立一个临时文件?<br>3. 有没有更简单的方法?
 
<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>操作过程大体如下:<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>另:我在6月17日的笔记里提到ExtractAssociatedIcon()和ExtractIcon()函数,<br>也可以提取文件的图标,但是速度比这个方法要慢上许多,而且我不会用它们提<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;<br><br>
 
我真的很清楚怎样取得一个文件的关联图标,<br>我的问题是: 不建立临时文件的情况下取得关联于文件扩展名的图标, 例如:.Doc的图标。行不行?
 
&nbsp; &nbsp; &nbsp; ico := TIcon.Create;<br>&nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp;SHGetFileInfo(Pchar('a'+ext),0,SHFileInfo,Sizeof(SHFileInfo),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SHGFI_SMALLICON or SHGFI_ICON or SHGFI_USEFILEATTRIBUTES);<br>&nbsp; &nbsp; &nbsp; &nbsp;ico.Handle :=SHFileInfo.hIcon;<br>&nbsp; &nbsp; &nbsp; &nbsp;ImageList.AddIcon(ico);<br>&nbsp; &nbsp; &nbsp; &nbsp;Result := ImageList.Count-1;<br>&nbsp; &nbsp; &nbsp; &nbsp;ico.ReleaseHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp;ico.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>上面的函数里的文件“a”并不真正生成,应该符合你的要求
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1036258
 
接受答案了.
 
后退
顶部