如题,代码如下:
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。
急待各位相助!
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。
急待各位相助!
解决方案 »
- 【求助】指针类型转换问题
- c++ 矩形与多边形的相交求结果,如何实现? int CombineRgn( CRgn* pRgn1, CRgn* pRgn2, int nCombineMode ); 如何解析得到的结果
- 使用OpenProcess打开天网防火墙进程的问题
- 在预编译的定义中,添加一个宏,代码不能识别
- 怎么将CSting型的数据转换成CTime型
- 新建MFC程序的时候,忘记在高级功能"Windows socket"前面打勾,该怎么补救?
- 有没有vc写的录音功能的源程序啊,我给100分,帮小弟一把:)
- 文字识别方面的,那位大哥大姐可以帮一下忙?
- SetTimer(0,1,NULL);的解释求解答在线等。。
- 各位大师,帮个小忙!!!
- 日期时间控件的用法
- 求解CSocket阻塞和CAsyncSocket非阻塞传输文件速度问题
typedef struct _SHFILEINFO {
HICON hIcon; //here
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO;
图标句柄在这个变量里面
file_info.hIcon;
HIMAGELIST image_list = (HIMAGELIST)SHGetFileInfo ((LPCSTR) "C:\\", 0, &file_info, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
我不是要系统的图标。要指定文件夹的图标。
发觉我把文件夹的图标换成别的图标后,使用CTreeCtrl的setItemImage函数设置出来的图标还是原来的图标。
何解?
int nIcon = icon_image_list_.Add(file_info.hIcon);
DestroyIcon(file_info.hIcon);
scan_area_tree_.SetItemImage(hItem, nIcon, nIcon);
使用,icon_image_list_.replace(0, file_info.hIcon)
然后在使用scan_area_tree_.SetItemImage(hItem, 0, 0);
但是:
当我把一个文件夹的图标更换成自定义图标后,再treectrl中显示的仍然是原来的图标。
int nIcon = icon_image_list_.replace(0, file_info.hIcon);
scan_area_tree_.SetItemImage(hItem, nIcon, nIcon);
正解,不过要注意一点,使用后要调用 ::DestroyIcon(),否则有资源泄漏。。
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.
还是用中文说吧,通俗点。那些英语我读了,还是不理解
{
// 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);
}
scan_area_tree_.SetImageList(&icon_image_list_, TVSIL_NORMAL);
icon_image_list_.Detach();
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);
建议跟一下,看看是不是程序其他的地方没有处理好
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);
应该是可以获取到的
DWORD ret ;// 1= OK
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 ();