int inline CShellBrowser::SetItemInformation(LPITEMIDLIST pidlDirectory, LPITEMIDLIST pidlRelative,TCHAR *szFileName) { LPITEMIDLIST pidlItem = NULL; HANDLE hFirstFile; TCHAR szPath[MAX_PATH]; int uItemId; m_nAwaitingAdd++; if((m_nTotalItems + m_nAwaitingAdd) > (m_iCurrentAllocation - 1)) { int PrevSize = m_iCurrentAllocation; if(m_iCurrentAllocation > MEM_ALLOCATION_LEVEL_MEDIUM) m_iCurrentAllocation += MEM_ALLOCATION_LEVEL_MEDIUM; else if(m_iCurrentAllocation > MEM_ALLOCATION_LEVEL_LOW) m_iCurrentAllocation += MEM_ALLOCATION_LEVEL_LOW; else m_iCurrentAllocation += DEFAULT_MEM_ALLOC; m_pwfdFiles = (WIN32_FIND_DATA *)realloc(m_pwfdFiles, m_iCurrentAllocation * (sizeof(WIN32_FIND_DATA))); m_pExtraItemInfo = (CItemObject *)realloc(m_pExtraItemInfo, m_iCurrentAllocation * sizeof(CItemObject)); m_pItemMap = (int *)realloc(m_pItemMap,m_iCurrentAllocation * sizeof(int)); InitializeItemMap(PrevSize,m_iCurrentAllocation); if(m_pwfdFiles == NULL || m_pExtraItemInfo == NULL) return E_OUTOFMEMORY; } uItemId = GenerateUniqueItemId(); m_pExtraItemInfo[uItemId].pridl = ILClone(pidlRelative); m_pExtraItemInfo[uItemId].bIconRetrieved = FALSE; m_pExtraItemInfo[uItemId].bThumbnailRetreived = FALSE; m_pExtraItemInfo[uItemId].bFolderSizeRetrieved = FALSE; StringCchCopy(m_pExtraItemInfo[uItemId].szDisplayName,MAX_PATH,szFileName); pidlItem = ILCombine(pidlDirectory,pidlRelative); SHGetPathFromIDList(pidlItem,szPath); CoTaskMemFree(pidlItem); /* DO NOT call FindFirstFile() on root drives (especially floppy drives). Doing so may cause a delay of up to a few seconds. */ if(!PathIsRoot(szPath)) { m_pExtraItemInfo[uItemId].bDrive = FALSE; hFirstFile = FindFirstFile(szPath,&m_pwfdFiles[uItemId]); } else { m_pExtraItemInfo[uItemId].bDrive = TRUE; StringCchCopy(m_pExtraItemInfo[uItemId].szDrive, SIZEOF_ARRAY(m_pExtraItemInfo[uItemId].szDrive), szPath); hFirstFile = INVALID_HANDLE_VALUE; } /* Need to use this, since may be in a virtual folder (such as the recycle bin), but items still exist. */ if(hFirstFile != INVALID_HANDLE_VALUE) { m_pExtraItemInfo[uItemId].bReal = TRUE; FindClose(hFirstFile); } else { StringCchCopy(m_pwfdFiles[uItemId].cFileName, sizeof(m_pwfdFiles[uItemId].cFileName)/sizeof(m_pwfdFiles[uItemId].cFileName[0]),szFileName); m_pwfdFiles[uItemId].nFileSizeLow = 0; m_pwfdFiles[uItemId].nFileSizeHigh = 0; m_pwfdFiles[uItemId].dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; m_pExtraItemInfo[uItemId].bReal = FALSE; } return uItemId; }
void CMyTreeView::AddItemInternal(HTREEITEM hParent,const TCHAR *szFullFileName) { IShellFolder *pShellFolder = NULL; LPITEMIDLIST pidlComplete = NULL; LPITEMIDLIST pidlRelative = NULL; HTREEITEM hItem; TVITEMEX tvItem; TVINSERTSTRUCT tvis; SHFILEINFO shfi; SFGAOF Attributes; TCHAR szDisplayName[MAX_PATH]; HRESULT hr; BOOL res; int iItemId; int nChildren = 0; hr = GetIdlFromParsingName(szFullFileName,&pidlComplete); if(!SUCCEEDED(hr)) return; tvItem.mask = TVIF_CHILDREN | TVIF_STATE; tvItem.hItem = hParent; res = TreeView_GetItem(m_hTreeView,&tvItem); if(res) { /* If the parent node is currently collapsed, simply indicate that it has children (i.e. a plus sign will be shown next to the parent node). */ if((tvItem.cChildren == 0) || ((tvItem.state & TVIS_EXPANDED) != TVIS_EXPANDED)) { tvItem.mask = TVIF_CHILDREN; tvItem.hItem = hParent; tvItem.cChildren = 1; TreeView_SetItem(m_hTreeView,&tvItem); } else { SHGetFileInfo(szFullFileName,NULL,&shfi, sizeof(shfi),SHGFI_SYSICONINDEX); hr = SHBindToParent(pidlComplete, IID_PPV_ARGS(&pShellFolder), (LPCITEMIDLIST *)&pidlRelative); if(SUCCEEDED(hr)) { Attributes = SFGAO_HASSUBFOLDER; /* Only retrieve the attributes for this item. */ hr = pShellFolder->GetAttributesOf(1, (LPCITEMIDLIST *)&pidlRelative,&Attributes); if(SUCCEEDED(hr)) { if((Attributes & SFGAO_HASSUBFOLDER) != SFGAO_HASSUBFOLDER) nChildren = 0; else nChildren = 1; iItemId = GenerateUniqueItemId(); m_pItemInfo[iItemId].pidl = ILClone(pidlComplete); m_pItemInfo[iItemId].pridl = ILClone(pidlRelative); GetDisplayName(szFullFileName,szDisplayName,SIZEOF_ARRAY(szDisplayName),SHGDN_NORMAL); tvItem.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM|TVIF_CHILDREN; tvItem.pszText = szDisplayName; tvItem.iImage = shfi.iIcon; tvItem.iSelectedImage = shfi.iIcon; tvItem.lParam = (LPARAM)iItemId; tvItem.cChildren = nChildren; if(hParent != NULL) { tvis.hParent = hParent; tvis.hInsertAfter = DetermineItemSortedPosition(hParent,szFullFileName); tvis.itemex = tvItem; hItem = TreeView_InsertItem(m_hTreeView,&tvis); } } pShellFolder->Release(); } } } CoTaskMemFree(pidlComplete); }