获取常见文件类型的图标 [英] Get icons for common file types

查看:148
本文介绍了获取常见文件类型的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的dll中得到常见的文件类型的图标。我使用vc ++。我只有文件的文件扩展名和MIME类型,我想要获取文件的图标。

I want to get the icons of common file types in my dll. I am using vc++. I only have the file extension and mime type of the file based on which I want to get the icon for the file.

有人可以告诉我怎么做? (vc ++中可用的方法需要用户给出需要图标的文件的路径,我没有访问任何这样的文件)

Can someone please tell me how I can do that? (The method available in vc++ needs the user to give the path of the file for which the icon is needed. I do not have access to any such file)

谢谢。

推荐答案

Shell API



调用 SHGetFileInfo() 以及 SHGFI_USEFILEATTRIBUTES 标志 - 此标志允许例程工作,而不需要传递的文件实际上存在,因此如果你有一个文件扩展名只是组成一个文件名

Shell API

You can get them from the shell by calling SHGetFileInfo() along with the SHGFI_USEFILEATTRIBUTES flag - this flag allows the routine to work without requiring the filename passed in to actually exist, so if you have a file extension just make up a filename, append the extension, and pass it in.

通过组合其他标志,您可以检索:

By combining other flags, you'll be able to retrieve:


  • 由系统配置确定的大图标或小图标 SHGFI_ICON | SHGFI_LARGEICON SHGFI_ICON | SHGFI_SMALLICON

  • 由shell配置决定的大图标或小图标 SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SHELLICONSIZE SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SHELLICONSIZE

  • shell的图片列表以及相应的图片列表: SHGFI_SYSICONINDEX

  • 实际模块的路径和文件名 SHGFI_ICONLOCATION

  • A large or small icon as determined by the system configuration: SHGFI_ICON|SHGFI_LARGEICON or SHGFI_ICON|SHGFI_SMALLICON
  • A large or small icon as determined by the shell configuration: SHGFI_ICON|SHGFI_LARGEICON|SHGFI_SHELLICONSIZE or SHGFI_ICON|SHGFI_SMALLICON|SHGFI_SHELLICONSIZE
  • The index of the icon in the shell's image list along with the appropriate image list: SHGFI_SYSICONINDEX
  • The path and filename of the actual module where the icon is stored (along with the icon index in that module): SHGFI_ICONLOCATION
// Load a System Large icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO), 
   SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_LARGEICON);

// Load a System Small icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO), 
   SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON);

// Load a Shell Large icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO), 
   SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SHELLICONSIZE);

// Load a Shell Small icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO), 
   SHGFI_USEFILEATTRIBUTES 
   | SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON);

如果要绘制此类图标,请使用类似如下的

If you want to draw such an icon, use something like this:

// Draw it at its native size
DrawIconEx( hDC, nLeft, nTop, hIcon, 0, 0, 0, NULL, DI_NORMAL );

// Draw it at the System Large size
DrawIconEx( hDC, nLeft, nTop, hIcon, 0, 0, 0, 
            NULL, DI_DEFAULTSIZE | DI_NORMAL );

// Draw it at some other size (40x40 in this example)
DrawIconEx( hDC, nLeft, nTop, hIcon, 40, 40, 0, NULL, DI_NORMAL );

图标句柄以及文件系统路径可以从 SHFILEINFO 结构:

The icon handle as well as the file system path can be obtained from the SHFILEINFO structure:

typedef struct _SHFILEINFOA
{
        HICON       hIcon;                      // out: icon
        int         iIcon;                      // out: icon index
        DWORD       dwAttributes;               // out: SFGAO_ flags
        CHAR        szDisplayName[MAX_PATH];    // out: display name (or path)
        CHAR        szTypeName[80];             // out: type name
} SHFILEINFOA;

请记住,您必须通过 hIcon DestroyIcon()

Keep in mind that you must free the obtained icon by passing hIcon to DestroyIcon() after you're done with it.

这篇关于获取常见文件类型的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆