Using Shell-Icons?!

Hello,

i wrote a component (in delphi but that is not important), that displays for example all files in a folder. How can i use the explorer-like-icons for file-extensions in my application?

Maybe there is a routine like GetIconFromFile[Extension](char* File):HBITMAP... or something like this???

Thanks for advance!
LenWinSonSoftAsked:
Who is Participating?
 
Dariusz DziaraProgrammerCommented:
ExtractAssociatedIcon() behaves like Windows - if there's icon in file it uses it, otherwise it gets associated with file extension icon from registry.
0
 
Dariusz DziaraProgrammerCommented:
Use ExtractIcon() or ExtractIconEx()
0
 
jkrCommented:
See also http://msdn.microsoft.com/library/techart/msdn_icons.htm ("Icons in Win32"):

// Load the DLL/EXE without executing its code
hLib = LoadLibraryEx( szFileName, NULL, LOAD_LIBRARY_AS_DATAFILE );
// Find the group resource which lists its images
hRsrc = FindResource( hLib, MAKEINTRESOURCE( nId ), RT_GROUP_ICON );
// Load and Lock to get a pointer to a GRPICONDIR
hGlobal = LoadResource( hLib, hRsrc );
lpGrpIconDir = LockResource( hGlobal );
// Using an ID from the group, Find, Load and Lock the RT_ICON
hRsrc = FindResource( hLib, MAKEINTRESOURCE( lpGrpIconDir->idEntries[0].nID ),
                      RT_ICON );
hGlobal = LoadResource( hLib, hRsrc );
lpIconImage = LockResource( hGlobal );
// Here, lpIconImage points to an ICONIMAGE structure

as well as http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/resources/icons/usingicons.asp ("Using Icons"):

HICON hIcon1;       // icon handle
HINSTANCE hExe;     // handle to loaded .EXE file
HRSRC hResource;    // handle for FindResource  
HRSRC hMem;         // handle for LoadResource
BYTE *lpResource;   // pointer to resource data  
int nID;            // ID of resource that best fits current screen
 
HDC hdc;        // handle to display context
 
// Load the file from which to copy the icon.
// Note: LoadLibrary should have a fully explicit path.
//
hExe = LoadLibrary("myapp.exe");
if (hExe == NULL)
{
    //Error loading module -- fail as securely as possible
      return;
}
 
 
// Find the icon directory whose identifier is 440.
 
hResource = FindResource(hExe,
    MAKEINTRESOURCE(440),
    RT_GROUP_ICON);
 
// Load and lock the icon directory.
 
hMem = LoadResource(hExe, hResource);
 
lpResource = LockResource(hMem);
 
// Get the identifier of the icon that is most appropriate
// for the video display.
 
nID = LookupIconIdFromDirectoryEx((PBYTE) lpResource, TRUE,
    CXICON, CYICON, LR_DEFAULTCOLOR);
 
// Find the bits for the nID icon.
 
hResource = FindResource(hExe,
    MAKEINTRESOURCE(nID),
    MAKEINTRESOURCE(RT_ICON));
 
// Load and lock the icon.
 
hMem = LoadResource(hExe, hResource);
 
lpResource = LockResource(hMem);
 
// Create a handle to the icon.
 
hIcon1 = CreateIconFromResourceEx((PBYTE) lpResource,
    SizeofResource(hExe, hResource), TRUE, 0x00030000,
    CXICON, CYICON, LR_DEFAULTCOLOR);
 
// Draw the icon in the client area.
 
DrawIcon(hdc, 10, 20, hIcon1);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
LenWinSonSoftAuthor Commented:
Thanks...

That is for the individual icons of any exe... Am I right?
Thats also what i need...

But more important is that all this default extensions like ".txt", ".dll", ".inf", ".cfg", etc.... are visible with their explorer-like-icons...

Do you have any idea how to realize this?

Thanks for advance...
0
 
jkrCommented:
>>That is for the individual icons of any exe... Am I right?

Yes.

>>But more important is that all this default extensions like ".txt", ".dll", ".inf", ".cfg", etc.... are visible
>>with their explorer-like-icons...

You need to look them up in the registry before loading them with the above code. E.g. '.txt' is listed in

HKEY_CLASSES_ROOT\.txt

as 'txtfile'. Under HKEY_CLASSES_ROOT\txtfile\DefaultIcon, you'll then find where to load the icon from.

OK, that's the long route, BUT:

Alternatively, you can call 'SHGetFileInfo()' with 'SHGFI_ICON' to directly obtain the icon.
0
 
jkrCommented:
BTW, see also http://support.microsoft.com/kb/179167/en-us ("How To Retrieve an Icon for a File Class Without a File"):

   SHFILEINFO sfi;
   HIMAGELIST hil;

   ZeroMemory(&sfi,sizeof(sfi));
   hil = SHGetFileInfo(".zip",
                       FILE_ATTRIBUTE_NORMAL,
                       &sfi,
                       sizeof(sfi),
                       SHGFI_USEFILEATTRIBUTES|SHGFI_ICON);

   // hil now contains the handle to the system image list
   // containing the large icon images.
   // sfi.hIcon contains the large icon for the file.
0
 
Dariusz DziaraProgrammerCommented:
Maybe
ExtractAssociatedIcon()
0
 
LenWinSonSoftAuthor Commented:
Thanks for advance!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.