I am just going through some of my old code looking for useful snippets. I found one snippet that was pretty cool (at least it was at the time!) where I used a grid control to display items for the user to configure. The grid had folders and so I wanted to use the system folder icon that are used in Windows Explorer. After a little searching on the Internet and MSDN, I came up with the following:

#include 
// ...
SHFILEINFO shfi;
HIMAGELIST hImageList = NULL;
HICON hIconClosed = NULL;
HICON hIconOpen = NULL;
UINT flags;

// Closed folder
ZeroMemory(&shfi, sizeof(shfi));
flags = SHGFI_ICON | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
hImageList = (HIMAGELIST) SHGetFileInfo("", 0, &shfi, sizeof(shfi), flags);
hIconClosed = ImageList_GetIcon(hImageList, shfi.iIcon, ILD_TRANSPARENT);

// Open folder
flags = SHGFI_ICON | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
hImageList = (HIMAGELIST) SHGetFileInfo("", 0, &shfi, sizeof(shfi), flags);
hIconOpen = ImageList_GetIcon(hImageList, shfi.iIcon, ILD_TRANSPARENT);

Don't forget to call DestroyIcon() when you are finished. When I actually used it in an application (BCB - Borland C++ Builder 5), it looked more like the following:

// (From header file)
#define SAFE_DELETE(x)        \
  if (x) {                    \
    delete (x);               \
    (x) = NULL;               \
  }
// (From form class header)
TImageList *MyGridImages;

// (From form cpp file)
// Add closed and open folder icons to image list.
Graphics::TIcon* pIcon = NULL;
HIMAGELIST hImageList = NULL;
SHFILEINFO shfi;
HICON hIcon = NULL;
UINT flags;

// Closed folder
ZeroMemory(&shfi, sizeof(shfi));
flags = SHGFI_ICON | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
hImageList = (HIMAGELIST) SHGetFileInfo("", 0, &shfi, sizeof(shfi), flags);
hIcon = ImageList_GetIcon(hImageList, shfi.iIcon, ILD_TRANSPARENT);
pIcon = new Graphics::TIcon();
pIcon->Handle = hIcon;
MyGridImages->AddIcon(pIcon);
SAFE_DELETE(pIcon);

// Open folder
flags = SHGFI_ICON | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
hImageList = (HIMAGELIST) SHGetFileInfo("", 0, &shfi, sizeof(shfi), flags);
hIcon = ImageList_GetIcon(hImageList, shfi.iIcon, ILD_TRANSPARENT);
pIcon = new Graphics::TIcon();
pIcon->Handle = hIcon;
MyGridImages->AddIcon(pIcon);
SAFE_DELETE(pIcon);

// Create an array of IPictureDisp handles
MPictures.resize(MyGridImages->Count);
for (size_t i = 0; i < MPictures.size(); ++i)
{
 std::auto_ptr pPicture( new Graphics::TPicture() );
 MyGridImages->GetBitmap(i, pPicture->Bitmap);
 _di_IPictureDisp pVal;
 GetOlePicture(pPicture.get(), pVal);
 MPictures[i] = pVal;
}


Comment Section

Comments are closed.