如果只知道文件扩展名,如何找到与之关联的(大小)图标呢?

xjbx
博客专家认证
2003-07-11 05:59:08
比如,给一个文件:test.txt ,就可以根据“txt”这个扩展名来获取与之关联的记事本的(大小)图标。!

谢谢!
...全文
119 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyl910 2003-07-18
  • 打赏
  • 举报
回复
用SHGetFileInfo,加上SHGFI_USEFILEATTRIBUTES参数即可




SHGetFileInfo


WINSHELLAPI DWORD WINAPI SHGetFileInfo(
LPCTSTR pszPath,
DWORD dwFileAttributes,
SHFILEINFO FAR *psfi,
UINT cbFileInfo,
UINT uFlags
);

Retrieves information about an object in the file system, such as a file, a folder, a directory, or a drive root.

Returns a value whose meaning depends on the uFlags parameter. If uFlags contains the SHGFI_EXETYPE flag, the return value specifies the type of the executable file. It will be one of the following values: Value Executable File Type
0 Nonexecutable file or an error condition
LOWORD = NE or PE and HIWORD = 3.0, 3.5, or 4.0 Windows application
LOWORD = MZ and HIWORD = 0 MS-DOS .exe, .com, or .bat file
LOWORD = PE and HIWORD = 0 Win32 console application

If uFlags contains SHGFI_SYSICONINDEX, the return value is the handle to the system image list that contains the large icon images. If SHGFI_SMALLICON is included with SHGFI_SYSICONINDEX, the return value is the handle to the image list that contains the small icon images.

If uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, the return value is nonzero if successful, or zero otherwise.

pszPath
Address of a buffer that contains the path and file name. Both absolute and relative paths are valid.
If the uFlags parameter includes the SHGFI_PIDL flag, this parameter must be the address of an ITEMIDLIST (PIDL) structure that contains the list of item identifiers that uniquely identifies the file within the shell's namespace. The PIDL must be a fully qualified PIDL. Relative PIDLs are not allowed.

If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, this parameter does not have to be a valid file name. The function will proceed as if the file exists with the specified name and with the file attributes passed in the dwFileAttributes parameter. This allows you to obtain information about a file type by passing just the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in dwFileAttributes.

This string can use either short (the 8.3 form) or long file names.

dwFileAttributes
Combination of one or more file attribute flags (FILE_ATTRIBUTE_ values). If uFlags does not include the SHGFI_USEFILEATTRIBUTES flag, this parameter is ignored.
psfi
Address of a SHFILEINFO structure to receive the file information.
cbFileInfo
Size, in bytes, of the SHFILEINFO structure pointed to by the psfi parameter.
uFlags
Flags that specify the file information to retrieve. This parameter can be a combination of the following values: SHGFI_ATTR_SPECIFIED Modifies SHGFI_ATTRIBUTES. Indicates that the dwAttributes member of the SHFILEINFO structure at psfi contains the specific attributes that are desired. These attributes will be passed to IShellFolder::GetAttributesOf. If this flag is not specified, 0xFFFFFFFF will be passed to GetAttributesOf, requesting all attributes. This flag cannot be specified with the SHGFI_ICON flag.
SHGFI_ATTRIBUTES Retrieve the item attributes. The attributes are copied to the dwAttributes member of the structure specified in the psfi parameter. These are the same attributes that are obtained from IShellFolder::GetAttributesOf.
SHGFI_DISPLAYNAME Retrieve the display name for the file. The name is copied to the szDisplayName member of the structure specified in psfi. The returned display name uses the long file name, if there is one, rather than the 8.3 form of the file name.
SHGFI_EXETYPE Retrieve the type of the executable file if pszPath identifies an executable file. This flag cannot be specified with any other flags.
SHGFI_ICON Retrieve the handle to the icon that represents the file and the index of the icon within the system image list. The handle is copied to the hIcon member of the structure specified by psfi, and the index is copied to the iIcon member. The return value is the handle to the system image list.
SHGFI_ICONLOCATION Retrieve the name of the file that contains the icon representing the file. The name is copied to the szDisplayName member of the structure specified in psfi.
SHGFI_LARGEICON Modify SHGFI_ICON, causing the function to retrieve the file's large icon.
SHGFI_LINKOVERLAY Modify SHGFI_ICON, causing the function to add the link overlay to the file's icon.
SHGFI_OPENICON Modify SHGFI_ICON, causing the function to retrieve the file's open icon. A container object displays an open icon to indicate that the container is open.
SHGFI_PIDL Indicate that pszPath is the address of an ITEMIDLIST structure rather than a path name.
SHGFI_SELECTED Modify SHGFI_ICON, causing the function to blend the file's icon with the system highlight color.
SHGFI_SHELLICONSIZE Modify SHGFI_ICON, causing the function to retrieve a shell-sized icon. If this flag is not specified the function sizes the icon according to the system metric values.
SHGFI_SMALLICON Modify SHGFI_ICON, causing the function to retrieve the file's small icon.
SHGFI_SYSICONINDEX Retrieve the index of the icon within the system image list. The index is copied to the iIcon member of the structure specified by psfi. The return value is the handle to the system image list.
SHGFI_TYPENAME Retrieve the string that describes the file's type. The string is copied to the szTypeName member of the structure specified in psfi.
SHGFI_USEFILEATTRIBUTES Indicates that the function should not attempt to access the file specified by pszPath. Rather, it should act as if the file specified by pszPath exists with the file attributes passed in dwFileAttributes. This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, or SHGFI_PIDL flags.
xjbx 2003-07-18
  • 打赏
  • 举报
回复
这样的话不是每次都要建一个临时的文件么?
还有别的什么好方法么?
小白兔 2003-07-18
  • 打赏
  • 举报
回复
每个扩展名在注册表里的 HKEY_CLASSES_ROOT 中有它的一个分支,该分支下边有个(大多数) DefaultIcon 分支,里边有其图标的文件和序号(即该文件的第几个图标)。没有该分支的图标应该是怎么指定的正在研究中………………
xjbx 2003-07-18
  • 打赏
  • 举报
回复
我解决了!
xjbx 2003-07-18
  • 打赏
  • 举报
回复
给代码提示好么?
我是想根据文件类型取得图标,不是根据具体的文件来获得。
现在,有很多软件如"WinZip" 能在它的文件列表中显示各类文件相关的图标(exe等可执行文件例外)。
注:是压缩包里的文件列表, 不是磁盘上已存在的文件。
SHGetFileInfo、ExtractAssociatedIcon等函数好象不能返回不存在的文件的图标. (如果根据不同的扩展名创建一个假文件, 获取图标后又删除, 未免太不专业了吧)能不能根据扩展名取得相关图标? 或其它方式?
gpo2002 2003-07-11
  • 打赏
  • 举报
回复
思路:
在临时目录里面建一个.txt文件,读取图标(这类代码网上很丰富),删除文件
Rozre 2003-07-11
  • 打赏
  • 举报
回复
up
benxie 2003-07-11
  • 打赏
  • 举报
回复
gzing
penglc 2003-07-11
  • 打赏
  • 举报
回复
思路: 从给出的文件中找到该文件的默认打开程序, 然后从该程序.exe或其主引用库.dll中获取图标。 下面只是从exe和dll获取图标的api.

Public Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long

说明:
判断一个可执行文件或DLL中是否有图标存在,并将其提取出来

返回值:
Long,如成功,返回指向图标的句柄;如文件中不存在图标,则返回零。如果nIconIndex设为-1,就返回文件中的图标总数

参数表:
hInst ---------- Long,当前应用程序的实例句柄。也可用GetWindowWord函数取得拥有一个窗体或控件的实例的句柄

lpszExeFileName - String,在其中提取图标的那个程序的全名

nIconIndex ----- Long,欲获取的图标的索引。如果为-1,表示取得文件中的图标总数
xjbx 2003-07-11
  • 打赏
  • 举报
回复
up

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧