Link to home
Start Free TrialLog in
Avatar of LenWinSonSoft
LenWinSonSoft

asked on

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!
Avatar of Dariusz Dziara
Dariusz Dziara
Flag of Poland image

Use ExtractIcon() or ExtractIconEx()
Avatar of jkr
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);
Avatar of LenWinSonSoft
LenWinSonSoft

ASKER

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...
>>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.
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe
ExtractAssociatedIcon()
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for advance!