Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get icons for common file types

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.

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)

Thanks.

like image 603
lostInTransit Avatar asked Feb 07 '09 17:02

lostInTransit


People also ask

How do I assign an icon to a file type?

Right click extension whose icon you want to change and then select “Edit Selected File Type.” In the “Edit File Type” window, click the “…” button to the right of the Default Icon text field. The “Change Icon” window shows some basic icons, but click the “Browse” button to find your own icon files.

What file type is used for icons?

The ICO file format is an image file format for computer icons in Microsoft Windows. ICO files contain one or more small images at multiple sizes and color depths, such that they may be scaled appropriately.

How do I associate an icon with a file type in Windows 10?

To select a custom icon hit the 'Browse' button to go to the folder having the icon of your choice and hit 'Open' to upload the icon on the Windows icon list; then select the uploaded icon. Also, if you browse for your own icons, you can select any EXE, DLL, or ICO file.


1 Answers

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:

  • 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

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:

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.

like image 132
newgre Avatar answered Sep 21 '22 19:09

newgre