Getting a file types icon

Started by
3 comments, last by SikCiv 22 years, 6 months ago
How would one get the icon of a particular file type like in ''My Computer''. I have a list box with a list of files of various extensions, and I need to get the file-type icon and specify the icon when the ListBox item is inserted. For example if I had a BMP file to add to the listbox, the icon would be the icon with the shapes and brush.

  Downloads:  ZeroOne Realm

  Downloads:  ZeroOne Realm

Advertisement
Look in HKEY_CLASSES_ROOT in the registry. The ".bmp" key will have a value (Default) which is the name of another key in HKEY_CLASSES_ROOT. Look in there and you'll find another key named "Default Icon". The (Default) value in there is the name of the icon (the path + the icon's number in that file).

You can use ExtractIcon to get the handle of the icon (HICON) from that file.

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on September 21, 2001 1:26:31 AM
Programmatically, you''ll need to use SHGetFileInfo( most of these ''shell'' type functions begin with SH.. helpful for MSDN searching )...

Here is a code snippet:

SHFILEINFO stInfo;
ZeroMemory( &stInfo, sizeof( SHFILEINFO ) );

//--Lets get the HICON, as well as the location info..
// Note that you are responsible for cleaning up the HICON
// with DestroyIcon( HICON ), after you have used it..
// ie, you''re done drawing...
if( SHGetFileInfo( "c:\\bug.txt", 0, &stInfo,
sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_ICONLOCATION ) )
{
//--Let''s display the icon...
m_staticImage.SetIcon( stInfo.hIcon );
}//endif

m_staticImage is a CStatic object. If you wanted to display in a list ctrl, you''d need to create an image list..

Hope that is helpful..

Zerapolis


"It''s only after you''ve lost everything that you''re free to do anything." Tyler Durden
"It's only after you've lost everything that you're free to do anything." Tyler Durden
I beleive the icon for my computer is in the shell32.dll file
Thanks heaps

  Downloads:  ZeroOne Realm

This topic is closed to new replies.

Advertisement