Monday, March 28, 2011

Get icons for common file types

Hi

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.

From stackoverflow
  • 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

    MSalters : Bad idea - such things may change between Windows versions. Stick to the documented interface.
  • This Microsoft article goes into it in some detail.

  • 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.

    Shog9 : @jn: I liked your examples, so i combined our answers and removed mine.
    jn_ : good move, thx :)

0 comments:

Post a Comment