如题,代码如下:
void CAVVIDlg::set_folder_item_icon(const wchar_t *folder_path)
{
CoInitialize(NULL);
SHFILEINFO file_info;
HIMAGELIST image_list = NULL; image_list = (HIMAGELIST)SHGetFileInfo(folder_path, 0, &file_info, sizeof(file_info), SHGFI_ICON | SHGFI_SMALLICON);
icon_image_list_.Attach(image_list);
        scan_area_tree_.SetImageList(&icon_image_list_, TVSIL_NORMAL);
icon_image_list_.Detach(); /*DestroyIcon(file_info.hIcon);*/
}  folder_path包含文件夹的路径,icon_image_list_是一个CImageList成员变量,scan_area_tree_是一个CTreeCtrl成员变量。
  在调试时,发现执行了SHGetFileInfo函数后,image_list值0x00000001。
  急待各位相助!

解决方案 »

  1.   

    兄台...无语了图标在SHFILEINFO *psfi 里面的,没你用返回值强制转换成图标的.....
      

  2.   


    typedef struct _SHFILEINFO {
        HICON hIcon; //here
        int iIcon;
        DWORD dwAttributes;
        TCHAR szDisplayName[MAX_PATH];
        TCHAR szTypeName[80];
    } SHFILEINFO;
      

  3.   

    SHFILEINFO file_info; 
    图标句柄在这个变量里面
    file_info.hIcon; 
      

  4.   

    要获取系统的图标列表,貌似应使用SHGFI_SYSICONINDEX:
    HIMAGELIST image_list = (HIMAGELIST)SHGetFileInfo ((LPCSTR) "C:\\", 0, &file_info, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
      

  5.   

    1楼的说的对。
    我不是要系统的图标。要指定文件夹的图标。
    发觉我把文件夹的图标换成别的图标后,使用CTreeCtrl的setItemImage函数设置出来的图标还是原来的图标。
    何解?
      

  6.   

    获取图标后,将图标添加到CTreeCtrl的图像列表,然后再SetItemImage:
    int nIcon = icon_image_list_.Add(file_info.hIcon);
    DestroyIcon(file_info.hIcon);
    scan_area_tree_.SetItemImage(hItem, nIcon, nIcon);
      

  7.   

    我没有使用add,因为我要遍历整个系统的文件夹,所以我始终在imagelist里只有一个image,
    使用,icon_image_list_.replace(0, file_info.hIcon)
    然后在使用scan_area_tree_.SetItemImage(hItem, 0, 0);
    但是:
        当我把一个文件夹的图标更换成自定义图标后,再treectrl中显示的仍然是原来的图标。
      

  8.   

    虚拟文件夹如“My Computer”,"回收站"
      

  9.   

    treectrl刷新一下看看,treectrl.Invalidate()
      

  10.   

    看看file_info.hIcon是否为NULL,和Replace的返回值:
    int nIcon = icon_image_list_.replace(0, file_info.hIcon);
    scan_area_tree_.SetItemImage(hItem, nIcon, nIcon); 
      

  11.   

    Replace前先看看图像列表中图标的个数:int nCount=icon_image_list_.GetImageCount()nCount > 0?
      

  12.   


    正解,不过要注意一点,使用后要调用 ::DestroyIcon(),否则有资源泄漏。。
      

  13.   

    说一下什么时候用SHGFI_SYSICONINDEX,什么时候用SHGFI_ICON
      

  14.   

    SHGFI_ICON
    Retrieve the handle to the icon that represents the file and the index of the icon within the system image list. The handle is copied to the hIcon member of the structure specified by psfi, and the index is copied to the iIcon member.
    SHGFI_ICONLOCATION
    Retrieve the name of the file that contains the icon representing the file specified by pszPath, as returned by the IExtractIcon::GetIconLocation method of the file's icon handler. Also retrieve the icon index within that file. The name of the file containing the icon is copied to the szDisplayName member of the structure specified by psfi. The icon's index is copied to that structure's iIcon member.
      

  15.   

    hIcon不为NULL,Replace返回值为0.为0 的原因是,image list 里只有一个image,返回索引为0.
      

  16.   

    我问的是什么时候用SHGFI_SYSICONINDEX,什么时候用SHGFI_ICON。你说的是SHGFI_ICONLOCATION。
    还是用中文说吧,通俗点。那些英语我读了,还是不理解
      

  17.   

    void CLeftView::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    // get LeftTree
    CTreeCtrl&    LeftTree=GetTreeCtrl();
    HTREEITEM     hTreeCurrent=LeftTree.GetSelectedItem (); 
        SHFILEINFO    sfi;
        DWORD ret =   SHGetFileInfo((LPCSTR)_T("E:\\"),
                                               0,
                                               &sfi, 
                                               sizeof(SHFILEINFO), 
                                               SHGFI_ICON  | SHGFI_SMALLICON);
    // Set new icon
    LeftTree.SetItemImage(hTreeCurrent,sfi.iIcon,sfi.iIcon); 
    // afxDump << sfi.iIcon << "\n" ;
    // CTreeView::OnTimer(nIDEvent);
    }
      

  18.   

    貌似treectrl的图像列表控件与icon_image_list_是2个不同的imagelisticon_image_list_.Attach(image_list); 
            scan_area_tree_.SetImageList(&icon_image_list_, TVSIL_NORMAL); 
    icon_image_list_.Detach(); 
      

  19.   

    我已经把代码改成:
    void CAVVIDlg::set_folder_item_icon(const wchar_t *folder_path/*, CImageList &icon_image_list, CTreeCtrl &tree_ctrl*/)
    {
    CoInitialize(NULL);
    SHFILEINFO file_info;
    HIMAGELIST image_list = NULL;        image_list = (HIMAGELIST)SHGetFileInfo(folder_path, 0, &file_info, sizeof(SHFILEINFO),                 
    SHGFI_ICON | SHGFI_SMALLICON);
    if(image_list)
    {
    if(icon_image_list_.GetImageCount() == 0)
    icon_image_list_.Add(file_info.hIcon);
    else
    {
    icon_image_list_.SetImageCount(1);
    icon_image_list_.Replace(0, file_info.hIcon);
    } scan_area_tree_.SetImageList(&icon_image_list_, TVSIL_NORMAL); DestroyIcon(file_info.hIcon);
    }
    }调用set_folder_item_icon:
    set_folder_item_icon( (folder_path + folder_name).c_str()/*, icon_image_list_, scan_area_tree_*/);
    HTREEITEM grandson_item = tree_ctrl.InsertItem(folder_name.c_str(), 0, 0, current_item);
      

  20.   

    读取icon是没有问题的,可以直接使用改变窗口图标就可以获得结果
    建议跟一下,看看是不是程序其他的地方没有处理好
      

  21.   

    MSDN:http://msdn.microsoft.com/en-us/library/aa453700.aspxIf uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, the return value is nonzero if successful, or zero otherwise
      

  22.   

    在对话框中定义一个CImageList的数据成员m_ilIcons;然后在对话框的OnInitDialog中使用m_ilIcons.Create创建图像列表;并使用CTreeCtrl::SetImageList将其设置为树控件的图像列表。使用SHGetFileInfo获取文件夹图标后,先使用m_ilIcons.Add或m_ilIcons.Replace添加或替换图标,然后tree.SetItemImage设置图标不需要每次都使用tree.SetImageList设置树控件的图像列表
      

  23.   

    我的程序可以 , 呵呵. 你试试.
    SHFILEINFO sfiTemp; ZeroMemory(&sfiTemp,sizeof(sfiTemp));  CString strName = ff.GetFileName();
    CString strPath = ff.GetFilePath(); hil = (HIMAGELIST)SHGetFileInfo(strPath, FILE_ATTRIBUTE_NORMAL, &sfiTemp, sizeof(sfiTemp), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_LARGEICON);
    m_iImageList.Add(sfiTemp.hIcon);
      

  24.   

    你直接获得file_info.hIcon以后用seticon设置一下窗口的图标不就ok了?
    应该是可以获取到的
      

  25.   

    SHGFI_ICON 时返回的是:
    DWORD ret ;// 1= OK
      

  26.   


    CFileFind ff;
    CString szDir = strDir;
    HTREEITEM hSubItem;
    HIMAGELIST hil;
    SHFILEINFO sfiTemp; ZeroMemory(&sfiTemp,sizeof(sfiTemp)); 
    BOOL res = ff.FindFile(szDir);
    m_FileTree.SetRedraw (FALSE); if(ff.IsDirectory() && !ff.IsDots())
    {
    CString strPath = ff.GetFilePath();
    CString strTitle = ff.GetFileTitle(); hil = (HIMAGELIST)SHGetFileInfo(strPath, FILE_ATTRIBUTE_DIRECTORY, &sfiTemp, sizeof(sfiTemp), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_LARGEICON); m_iImageList.Add(sfiTemp.hIcon);
    m_FileTree.SetImageList ( &m_iImageList, LVSIL_NORMAL ); int i = m_iImageList.GetImageCount() - 1; hSubItem = m_FileTree.InsertItem( strTitle, i, i, parent ); BrowseDir( strPath, hSubItem );
    }
    else if(!ff.IsDirectory() && !ff.IsDots())
    {
    //CString strTitle = ff.GetFileTitle();
    CString strName = ff.GetFileName();
    CString strPath = ff.GetFilePath(); hil = (HIMAGELIST)SHGetFileInfo(strPath, FILE_ATTRIBUTE_NORMAL, &sfiTemp, sizeof(sfiTemp), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_LARGEICON);
    m_iImageList.Add(sfiTemp.hIcon);
    m_FileTree.SetImageList ( &m_iImageList, LVSIL_NORMAL ); int i = m_iImageList.GetImageCount() - 1; m_FileTree.InsertItem( strName, i, i, parent );
    } m_FileTree.SetRedraw (TRUE);
    m_FileTree.Invalidate ();