获取常见文件类型的图标

时间:2021-06-23 05:55:03

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.

我想在我的dll中获取常见文件类型的图标。我正在使用vc ++。我只有文件的文件扩展名和mime类型,我希望根据该文件获取文件的图标。

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)

有人可以告诉我我该怎么做吗? (vc ++中可用的方法需要用户提供需要图标的文件的路径。我无权访问任何此类文件)

Thanks.

2 个解决方案

#1


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.

你可以通过调用SHGetFileInfo()和SHGFI_USEFILEATTRIBUTES标志从shell获取它们 - 这个标志允许例程工作而不需要传入的文件名实际存在,所以如果你有一个文件扩展名只需要组成一个文件名,请附加扩展,并传入。

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

通过组合其他标志,您将能够检索:

  • A large or small icon as determined by the system configuration: SHGFI_ICON|SHGFI_LARGEICON or SHGFI_ICON|SHGFI_SMALLICON
  • 由系统配置确定的大图标或小图标:SHGFI_ICON | SHGFI_LARGEICON或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
  • 由shell配置确定的大图标或小图标:SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SHELLICONSIZE或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
  • shell图像列表中图标的索引以及相应的图像列表: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
  • 存储图标的实际模块的路径和文件名(以及该模块中的图标索引):SHGFI_ICONLOCATION

Examples

// 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 );

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

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

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;

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

请记住,在完成后,必须通过将hIcon传递给DestroyIcon()来释放获取的图标。

#2


Identify the icon information from the registry, the associate file type and program that handles the file and extract the icon from the file. http://www.codeproject.com/KB/shell/iconextract.aspx

识别注册表中的图标信息,关联文件类型和处理文件的程序,并从文件中提取图标。 http://www.codeproject.com/KB/shell/iconextract.aspx

#1


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.

你可以通过调用SHGetFileInfo()和SHGFI_USEFILEATTRIBUTES标志从shell获取它们 - 这个标志允许例程工作而不需要传入的文件名实际存在,所以如果你有一个文件扩展名只需要组成一个文件名,请附加扩展,并传入。

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

通过组合其他标志,您将能够检索:

  • A large or small icon as determined by the system configuration: SHGFI_ICON|SHGFI_LARGEICON or SHGFI_ICON|SHGFI_SMALLICON
  • 由系统配置确定的大图标或小图标:SHGFI_ICON | SHGFI_LARGEICON或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
  • 由shell配置确定的大图标或小图标:SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SHELLICONSIZE或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
  • shell图像列表中图标的索引以及相应的图像列表: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
  • 存储图标的实际模块的路径和文件名(以及该模块中的图标索引):SHGFI_ICONLOCATION

Examples

// 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 );

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

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

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;

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

请记住,在完成后,必须通过将hIcon传递给DestroyIcon()来释放获取的图标。

#2


Identify the icon information from the registry, the associate file type and program that handles the file and extract the icon from the file. http://www.codeproject.com/KB/shell/iconextract.aspx

识别注册表中的图标信息,关联文件类型和处理文件的程序,并从文件中提取图标。 http://www.codeproject.com/KB/shell/iconextract.aspx