但是同样的方法却能正确枚举出“桌面”下的对象。
注意:不能正确枚举和不能枚举是不同的,不能正确枚举意思是:能枚举出来东西,但是获得的图标和DisplayName都不对。
程序如下,请高手帮忙瞧一瞧。
// m_pDirTree 为类成员变量
m_pDirTree=(CTreeCtrl *)GetDlgItem(IDC_TREE_PIDL);
::SetWindowLong(m_pDirTree->GetSafeHwnd(),GWL_STYLE,::GetWindowLong(m_pDirTree->GetSafeHwnd(),GWL_STYLE)|LVS_SHAREIMAGELISTS);
// 获得系统ImageList
SHFILEINFO sfi;
// m_hSysImageList 为类成员变量
m_hSysImageList=(HIMAGELIST)::SHGetFileInfo("C:\\",NULL,&sfi,sizeof(SHFILEINFO),SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
CImageList *pSysImageList;
m_pDirTree->SetImageList(pSysImageList->FromHandle(m_hSysImageList), LVSIL_NORMAL);
LPMALLOC pMalloc;
::SHGetMalloc(&pMalloc);
LPITEMIDLIST pidl;
::SHGetSpecialFolderLocation(GetSafeHwnd(),CSIDL_DRIVES ,&pidl);
IShellFolder *pDesktop,*pMyComputer;
::SHGetDesktopFolder(&pDesktop);
pDesktop->BindToObject(pidl,NULL,IID_IShellFolder,(void **)&pMyComputer);
IEnumIDList *pEnum;
pMyComputer->EnumObjects(GetSafeHwnd(),SHCONTF_FOLDERS, &pEnum);
LPITEMIDLIST pidl_1;
while(S_FALSE!=pEnum->Next(1,&pidl_1,NULL))
{
SHFILEINFO sfi;
::SHGetFileInfo((LPCTSTR)pidl_1,0,&sfi,sizeof(SHFILEINFO),SHGFI_ICON | SHGFI_SMALLICON | SHGFI_PIDL | SHGFI_DISPLAYNAME);
TVINSERTSTRUCT tis;
tis.hInsertAfter=NULL;
tis.hParent=NULL;
tis.item.iImage=sfi.iIcon;
tis.item.pszText=sfi.szDisplayName;
tis.item.mask=TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
m_pDirTree->InsertItem(&tis);
//CString str;
//str.Format("%d",pidl_1);
//MessageBox(str);
pMalloc->Free(pidl_1);
}
pMalloc->Free(pidl);
pMalloc->Release();
pDesktop->Release();
pMyComputer->Release();
pEnum->Release();