RT,想將選中的檔案目錄的層次結構顯示到樹形控制元件中,同時根據檔案的型別顯示不同的圖示,如果是檔案夾顯示檔案夾圖示,如果是其他型別的檔案格式則顯示檔案圖示,程式運行后顯示如下:

原本1.3 1.4應該顯示在一級目錄下的,現在跑到二級目錄下了
我的代碼如下:
//向imagelist中添加兩個圖示
m_imageList.Create(32,32,ILC_COLOR32,2,2);
m_imageList.Add(AfxGetApp()->LoadIconW(IDI_ICON_FOLDER));//檔案夾圖示
m_imageList.Add(AfxGetApp()->LoadIconW(IDI_ICON_FILE));//檔案圖示
m_ctrlFileTree.SetImageList(&m_imageList,TVSIL_NORMAL);
//將檔案層次結構顯示到樹形控制元件
void CDataBaseQueryDlg::LoadTree(CString strCurdir,HTREEITEM hItem)
{
//遍歷檔案
if(strCurdir==L"")
return;
else{
if(strCurdir.Right(1)!=L"//")
strCurdir+=L"//";
strCurdir+=L"*.*";
}
CFileFind finder;
CString strPath;
int i=0;
BOOL bWorking=finder.FindFile(strCurdir);
while (bWorking)
{
bWorking=finder.FindNextFileW();
strPath=finder.GetFilePath();
int count=0;
if(finder.IsDirectory()&&!finder.IsDots())
{
hFolder=m_ctrlFileTree.InsertItem(finder.GetFileName(),0,0,hRoot,TVI_LAST);
count++;
LoadTree(strPath,hFolder);
}
else if (!finder.IsDirectory()&&!finder.IsDots())
{
if(count!=0)
hFile=m_ctrlFileTree.InsertItem(finder.GetFileName(),1,1,hRoot,TVI_LAST);
else
hFile=m_ctrlFileTree.InsertItem(finder.GetFileName(),1,1,hFolder,TVI_LAST);
}
}
}
//打開需要顯示成樹形結構的目錄
void CDataBaseQueryDlg::OnBnClickedBtnFolder()
{
// TODO: 在此添加控制元件通知處理程式代碼
LPMALLOC pMalloc; //定義指向IMalloc的指標
if(::SHGetMalloc(&pMalloc)!=NOERROR)
return;
wchar_t szDisplayName[MAX_PATH];
wchar_t szBuffer[MAX_PATH];
BROWSEINFO browseInfo; //構造對話框結構
browseInfo.hwndOwner=this->m_hWnd;
browseInfo.pidlRoot=NULL; //設定根目錄為桌面
browseInfo.pszDisplayName=szDisplayName;
browseInfo.lpszTitle=L"選擇搜索檔案目錄"; //對話框標題
browseInfo.ulFlags=BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
browseInfo.lpfn=NULL;
browseInfo.lParam=0;
LPITEMIDLIST lpItemIDList=SHBrowseForFolderW(&browseInfo);
if (lpItemIDList!=NULL)
{
if (::SHGetPathFromIDListW(lpItemIDList,szBuffer))//得到目錄的路徑
{
if('\0'==szBuffer[0])
return;
m_strFile=szBuffer;
UpdateData(FALSE);
}
else{
AfxMessageBox(L"不能得到所選的檔案!",MB_ICONSTOP|MB_OK);
return;
}
pMalloc->Free(lpItemIDList);
pMalloc->Release();
}
hRoot=m_ctrlFileTree.InsertItem(m_strFile,0,0);
LoadTree(m_strFile,hFolder);
}
感覺是LoadTree函式中的else if段有問題,請大神們指教,謝謝!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/122913.html
標籤:界面
