procedure GetExeIcon(FileName:string);<br>var<br> icon:Ticon;<br>begin<br> icon := TIcon.create;<br> try<br> icon.handle := extractIcon(hInstance,pchar(FileName),0);<br> finally<br> icon.free;<br> end;<br>end;<br><br>附 ExtarctIcon 的联机帮助<br><br>the ExtractIcon function retrieves the handle of an icon from the specified executable file, dynamic-link library (DLL), or icon file. <br><br>这个函数可以从可执行文件,动态连接库,或 icon 文件中取得 icon 的 handle<br><br>HICON ExtractIcon(<br><br> HINSTANCE hInst, // instance handle 实例句柄,传 hinstance就行<br> LPCTSTR lpszExeFileName, // filename of file with icon 文件的路径<br> UINT nIconIndex // index of icon to extract 图标索引<br> ); <br> <br><br>Parameters<br><br>hInst<br><br>Identifies the instance of the application calling the function. <br><br>lpszExeFileName<br><br>Points to a null-terminated string specifying the name of an executable file, DLL, or icon file. <br><br>nIconIndex<br><br>Specifies the index of the icon to retrieve. If this value is 0, the function returns the handle of the first icon in the specified file. If this value is -1, the function returns the total number of icons in the specified file. <br><br>值为 0 ,则返回第一个 icon的 handle ,为 -1 则返回 icon 的总数<br><br> <br><br>Return Values<br><br>If the function succeeds, the return value is the handle to an icon. If the file specified was not an executable file, DLL, or icon file, the return is 1. If no icons were found in the file, the return value is NULL.<br><br><br>注意:要用 -1 来返回 icon 的个数时,由于 uInt 是无符号类型,所以提示为<br><br>Constant expression violates subrange bounds <br><br>解决办法为用 $FFFFFFFF 来代替 -1<br><br>这可得到的是 32 * 32 大小的图标<br><br>-------------------------------------------<br>这就是HubDog的《Delphi之未经证实的葵花宝典version 1.8》 中的有关提取图标的内容,<br>请自己试一试吧<br><br><br> WINSHELLAPI DWORD WINAPI SHGetFileInfo(<br> LPCSTR pszPath,<br> DWORD dwFileAttributes,<br> SHFILEINFO FAR *psfi,<br> UINT cbFileInfo,<br> UINT uFlags<br> );<br>它的作用是:取回文件系统中的一个对象的信息,对象可以是文件、文件夹、<br>目录或驱动器的根目录。经过三个多小时的调试,我终于完全弄明白怎样取<br>得并显示一个文件的图标了:包括大图标、小图标,象资源管理器上的那样。<br>操作过程大体如下:<br>var ShFileInfo: TSHFILEINFO;<br> FileList:TListView;<br>begin<br> ...<br> Result := FileList.Items.Add;<br> with Result do<br> begin<br> Caption:=filename;<br> ShGetFileInfo(pchar(vartostr(filename)), 0, SHFileInfo, SizeOf(SHFileInfo),<br> SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME)=0 then<br> showmessage('error in shgetfileinfo');<br> ImageIndex := SHFileInfo.iIcon;<br> end;<br> ...<br>end;<br>这是最关键的几个地方,中间省略了许多细节。<br>另:我在6月17日的笔记里提到ExtractAssociatedIcon()和ExtractIcon()函数,<br>也可以提取文件的图标,但是速度比这个方法要慢上许多,而且我不会用它们提<br>取小图标。<br>//////////////////////////////////////////////////////////////<br>uses ShellApi;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> Icon : hIcon;<br> IconIndex : word;<br><br>begin<br> Icon := ExtractAssociatedIcon(HInstance,<br> 'C:/SomePath/SomeFile.ext',<br> IconIndex);<br> DrawIcon(Form1.Canvas.Handle, 10, 10, Icon);<br>end;