tags:

views:

702

answers:

2

Hi all,

I want to add Icon to treeview node, using C++. I want to get the icons from system, I tried

I tried with,

PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;    

::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
    SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);

iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);

where, SetResrcID ,PMString are the InDesing API and iconView is the controlView of the Tree, I am not getting what's going wrong, if anyone has idea please suggest.

Thanks, Praveen Mamdge

+1  A: 

Here is the codes what I'm using in my application, you should change the icon to a bitmap.

PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;    

::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
    SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
ICONINFO stIconInfo;
GetIconInfo(s_sfi.hIcon, &stIconInfo);
HBITMAP hBmp = stIconInfo.hbmColor;
DestroyIcon(s_sfi.hIcon) ;

The best way to do it is using the system icon index with SHGFI_SYSICONINDEX.

Yigang Wu
+1  A: 

Some thing like this, Extract icon from file first.

SHFILEINFO stFileInfo;
SHGetFileInfo( file,
               FILE_ATTRIBUTE_NORMAL,
               &stFileInfo,
               sizeof( stFileInfo ),
               SHGFI_ICON | SHGFI_LARGEICON );

Then add to imagelist and use the index to set icon.

m_nIndex = m_ilLargeIcons.Add( stFileInfo.hIcon );
Sauron