mfc如何通过树控件显示各种文件图标,
BOOL CTree_fileDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化代码
///////////////////////////////////
//Left_cwnd = this;
m_imageList.Create(32, 32, ILC_COLOR32|ILC_MASK, 0, 0);
m_imageList.SetBkColor(RGB(255,255,255));
m_TreeControl.SetImageList(&m_imageList, TVSIL_NORMAL);
///////////////////////////////////
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
/////////////////////////////////
CString CTree_fileDlg::SelectDirectory(HWND hwnd)
{
CString strPath;
TCHAR szBuf[1024] = { 0 };
memset(szBuf, 0, sizeof(szBuf));
BROWSEINFO bInfo;
bInfo.hwndOwner = hwnd;
bInfo.pidlRoot = NULL;
bInfo.pszDisplayName = szBuf;
bInfo.lpszTitle = _T("选择位置");
bInfo.ulFlags= BIF_EDITBOX;
bInfo.lpfn = NULL;
bInfo.iImage = 0;
LPITEMIDLIST lp = SHBrowseForFolder(&bInfo);
if (lp == NULL)
return NULL;
if (lp&&SHGetPathFromIDList(lp, szBuf))
{
strPath = szBuf;
}
else
{
strPath = _T("");
}
m_Path = strPath;
SetDlgItemText(IDC_EDIT_CHOOSE, m_Path);
return strPath;
}
void CTree_fileDlg::CreateTreemanager(HTREEITEM Root,CString path)
{
CFileFind finder;
HTREEITEM hFatherItem, hSonItem;
BOOL exit = finder.FindFile(path + _T("\\*.*"));
while (exit)
{
exit = finder.FindNextFile();
if (finder.IsDirectory() && !finder.IsDots())
{
CString mDir = finder.GetFileTitle();
SHGetFileInfo(finder.GetFilePath(),0,&shfileInfo,sizeof(shfileInfo),SHGFI_ICON);
//SHGetFileInfo(finder.GetFilePath(), 0, &shfileInfo, sizeof(shfileInfo), SHGFI_ICON| SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES );
index = m_imageList.Add(shfileInfo.hIcon);
hFatherItem = m_TreeControl.InsertItem(mDir, index, index, Root, TVI_LAST);
mDir = path + _T("\\") + mDir;
CreateTreemanager(hFatherItem, mDir);
}
else if (!finder.IsDirectory() && !finder.IsDots())
{
CString mDir = finder.GetFileTitle();
//SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES | (bSelected ? SHGFI_OPENICON : 0
//SHGetFileInfo(mDir, 0, &shfileInfo, sizeof(shfileInfo), SHGFI_USEFILEATTRIBUTES|SHGFI_SYSICONINDEX|SHGFI_ICON);
SHGetFileInfo(mDir, 0, &shfileInfo, sizeof(shfileInfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
index = m_imageList.Add(shfileInfo.hIcon);
hSonItem = m_TreeControl.InsertItem(mDir, index, index, Root, TVI_LAST);
}
}
finder.Close();
}
void CTree_fileDlg::OnBnClickedButtonOnloard()
{
// TODO: 在此添加控件通知处理程序代码
HTREEITEM hRoot;
m_TreeControl.DeleteAllItems();
m_Path.Empty();
CString path = SelectDirectory(m_hWnd);
if (path == "NULL")
return;
m_Path = path;
UpdateData(FALSE);
SHGetFileInfo(m_Path, 0, &shfileInfo, sizeof(shfileInfo), SHGFI_SYSICONINDEX | SHGFI_ICON);
index = m_imageList.Add(shfileInfo.hIcon);
hRoot = m_TreeControl.InsertItem(path, index, index, TVI_LAST);
CreateTreemanager(hRoot, m_Path);
UpdateData(FALSE);
}