如何获取某一类型的文件在系统的图标?(100分)

  • 主题发起人 主题发起人 qwert71
  • 开始时间 开始时间
Q

qwert71

Unregistered / Unconfirmed
GUEST, unregistred user!
比如在foxmail中,添加附件后,能将该附件类型对应的图标
显示出来。和该文件在资源管理器中显示的图标一致。
 
The ExtractAssociatedIcon function returns the handle of an indexed icon found in a file or an icon found in an associated executable file.
HICON ExtractAssociatedIcon(
HINSTANCE hInst, // application instance handle
LPTSTR lpIconPath, // path and filename of file for which icon is wanted
LPWORD lpiIcon // pointer to icon index
);

Parameters
hInst
Specifies the instance of the application calling the function.
lpIconPath
Points to a string that specifies the full path and filename of the file for which an icon is desired. The function extracts the icon handle from that file, or from an executable file associated with that file.
If the icon handle is obtained from an executable file, the function stores the full path and filename of that executable in the string pointed to by lpIconPath.
lpiIcon
Points to a WORD that specifies the index of the icon whose handle is to be obtained.
If the icon handle is obtained from an executable file, the function stores the icon's identifier in the
WORD pointed to by lpiIcon.

Return Values
If the function succeeds, the return value is an icon handle. If the icon is extracted from an associated executable file, the function stores the full path and filename of the executable file in the string pointed to by lpIconPath, and stores the icon's identifier in the WORD pointed to by lpiIcon.
If the function fails, the return value is NULL.
Remarks
The ExtractAssociatedIcon function first looks for the indexed icon in the file specified by lpIconPath. If the function cannot obtain the icon handle from that file, and the file has an associated executable file, it looks in that executable file for an icon. Associations with executable files are based on filename extensions, are stored in the per-user part of the registry, and can be defined using File Manager's Associate command.
 
hubdog说的是已知某个程序后取其中指定的Index的Icon
你的问题很简单:
以.386为例
1、已知扩展名为.386
2、在注册表中查找HKey_Classes_Root的/.386,
找到后取其值,为"vxdFile",
这个值就是对扩展名.386的解释,
也就是资源管理器中看到的“类型”部分
3、在注册表中查找HKey_Classes_Root的/vxdFile/DefaultIcon,
找到后取其值,为"C:/WINDOWS/SYSTEM/shell32.dll,-154",
这个结果就表示.386文件显示的图标在C:/WINDOWS/SYSTEM/shell32.dll中
index为-154
4、得到以上结果后,就可以用hubdog说的方法去取实际的图标内容,以便显示
5、除了显示图标外,
如果要打开该文件,
可以将附件按相同的文件名保存到temp目录中,
然后用ShellExecute函数打开,记得uses ShellAPI
 
这样吧:
function GetFileIcon(const Filename:String;
SmallIcon:Boolean):HICON;
var info:TSHFILEINFO;
Flag: Integer;
begin
if SmallIcon then
Flag:=(SHGFI_SMALLICON or SHGFI_ICON)
else
Flag:=(SHGFI_LARGEICON or SHGFI_ICON);
SHGetFileInfo(Pchar(Filename),0,info,Sizeof(info),Flag);
Result:=info.hIcon;
end;

有了HICON就可以用DrawIcon随便画了。
 
好象不行,在把HICON强制转化为TIcon的时候会出错。
 
怎么"强制转化"的?
应该是icon.handle:=HIcon
 
多人接受答案了。
 
后退
顶部