15,979
社区成员
发帖
与我相关
我的任务
分享
////
void CMyExploreView::PopulateList(LPTVITEMDATA lptvid)
{//from lptvid->lpsfparent->current_folder
LPSHELLFOLDER lpsf2=NULL;
HRESULT hr;
// Initialize the list view to be empty.
CListCtrl &RightList=GetListCtrl();
RightList.DeleteAllItems();
// ask for current folder
if (lptvid)
{
hr=lptvid->lpsfParent->BindToObject(lptvid->lpi,
0, IID_IShellFolder,(LPVOID *)&lpsf2);
if (SUCCEEDED(hr))
{// current dir oK
FillListView(lpsf2,lptvid->lpifq);//
lpsf2->Release();
// then sorting
RightList.SortItems(ListViewCompareProc,0);// name
}
}
}
void CMyExploreView::FillListView(LPSHELLFOLDER lpsf,LPITEMIDLIST lpifq)
{
LVITEM lvi; // ListView Item.
LPENUMIDLIST lpe=NULL;
LPITEMIDLIST lpi=NULL,lpifqThisItem=NULL;
LPLVITEMDATA lplvid=NULL;
LPMALLOC lpMalloc=NULL;
ULONG ulFetched;
UINT uCount=0;
HRESULT hr;
char szBuff[256];
HWND hwnd=::GetParent(m_hWnd);
// char tmp[256];
//
CListCtrl &RightList=GetListCtrl();
// Allocate a shell memory object.
hr=::SHGetMalloc(&lpMalloc);
if (FAILED(hr)) return;
if (SUCCEEDED(hr))
{// Get the IEnumIDList object for the given folder.
hr=lpsf->EnumObjects(hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &lpe);
if (SUCCEEDED(hr))
{// Enumerate throught the list of folder and non-folder objects.
while (S_OK==lpe->Next(1, &lpi, &ulFetched))
{
ULONG ulAttrs = SFGAO_FOLDER;
// Determine what type of object we have.
lpsf->GetAttributesOf(1, (const struct _ITEMIDLIST **)&lpi, &ulAttrs);
lvi.iItem=uCount;
lvi.iSubItem=0;
lvi.mask= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
//OK, let's get some memory for our ITEMDATA struct
lplvid = (LPLVITEMDATA)lpMalloc->Alloc(sizeof(LVITEMDATA));
if (!lplvid) goto Done; // Error - could not allocate memory.
//Now get the friendly name that we'll put in the treeview.
if (!GetName(lpsf, lpi, SHGDN_NORMAL, szBuff))
goto Done; // Error - could not get friendly name.
lvi.pszText = szBuff;
lvi.cchTextMax = MAX_PATH;
//Now, make a copy of the ITEMIDLIST
//They are used in compareproc
lplvid->lpi=CopyITEMID(lpMalloc, lpi);
//SHGetFileInfo() needs a full pidl.
lpifqThisItem=ConcatPidls(lpifq,lpi);
GetNormalIcons(lpifqThisItem,&lvi);// ICON
//
lplvid->lpsfParent=lpsf;//Store the parent folders SF
lpsf->AddRef();
lplvid->lpifq=ConcatPidls(lpifq,lpi);
lvi.lParam = (LPARAM)lplvid;
// Add the item to the list
RightList.InsertItem(&lvi);
// add sub items. size//?? GetDetailsOf();
WIN32_FIND_DATA fd;//relative lpi
hr=SHGetDataFromIDList(lpsf,lpi,SHGDFIL_FINDDATA,&fd,sizeof(fd));
// if (FAILED(hr)) return;
__int64 AllSize=fd.nFileSizeHigh;
AllSize<<=32;
AllSize+=fd.nFileSizeLow;
char tmp[MAX_PATH];
BOOL bl=0;
if (!(ulAttrs & SFGAO_FOLDER))
{// dir no size be shown
FileSizeGB((double)AllSize,tmp);
bl=RightList.SetItemText(uCount,1,tmp);
}
// type
SHFILEINFO sfi;
::SHGetFileInfo((LPCTSTR)lpifqThisItem,0,&sfi,sizeof(SHFILEINFO),SHGFI_PIDL|SHGFI_TYPENAME);
BOOL ret=RightList.SetItemText(uCount,2,sfi.szTypeName);
// time
SYSTEMTIME st;
FileTimeToSystemTime(&fd.ftLastWriteTime,&st);
sprintf(tmp,"%2.2hd/%2.2hd/%4.4hd %2.2hd:%2.2hd",
st.wMonth,st.wDay,st.wYear,st.wHour,st.wMinute);
bl=RightList.SetItemText(uCount,3,tmp);
//Free task allocator
lpMalloc->Free(lpifqThisItem);
lpifqThisItem=0;
// Free this items task allocator.
lpMalloc->Free(lpi); //Free the pidl that the shell gave us.
lpi=0;
uCount++;
}
}// end if success
}
else
return;
Done:
if (lpe) lpe->Release();
//The following 2 if statements will only be TRUE if we got here on an
//error condition from the "goto" statement. Otherwise, we free this memory
//at the end of the while loop above.
if (lpi && lpMalloc) lpMalloc->Free(lpi);
if (lpMalloc) lpMalloc->Release();
if (lpifqThisItem && lpMalloc) lpMalloc->Free(lpifqThisItem);
}
void CMyExploreView::GetNormalIcons(LPITEMIDLIST lpifq,LPLVITEM lplvitem)
{
//Note that we don't check the return value here because if GetIcon()
//fails, then we're in big trouble...
lplvitem->iImage = GetItemIcon(lpifq, SHGFI_PIDL |
SHGFI_SYSICONINDEX |
SHGFI_SMALLICON);
return;
}