?
Solved

Using Shell-Icons?!

Posted on 2006-05-01
8
Medium Priority
?
463 Views
Last Modified: 2011-10-03
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!
0
Comment
Question by:LenWinSonSoft
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:Dariusz Dziara
ID: 16578689
Use ExtractIcon() or ExtractIconEx()
0
 
LVL 86

Expert Comment

by:jkr
ID: 16578810
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
 

Author Comment

by:LenWinSonSoft
ID: 16579238
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 16579409
>>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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 16579423
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
 
LVL 8

Expert Comment

by:Dariusz Dziara
ID: 16579439
Maybe
ExtractAssociatedIcon()
0
 
LVL 8

Accepted Solution

by:
Dariusz Dziara earned 1000 total points
ID: 16579476
ExtractAssociatedIcon() behaves like Windows - if there's icon in file it uses it, otherwise it gets associated with file extension icon from registry.
0
 

Author Comment

by:LenWinSonSoft
ID: 16580378
Thanks for advance!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question