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);
}
通过使用SHGetFileInfo函数获取文件的系统图标,将其添加到ImageList中,再通过树控件的InsertItem函数将文件夹和文件的名称和对应的图标插入到树控件中即可。示例代码如下:
BOOL CMyDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 创建ImageList
m_ImageList.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
SHFILEINFO shfi;
SHGetFileInfo(_T("C:\\"), FILE_ATTRIBUTE_DIRECTORY, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON);
m_ImageList.Add(shfi.hIcon);
// 绑定ImageList到树控件
m_TreeCtrl.SetImageList(&m_ImageList, TVSIL_NORMAL);
// 加载根目录下的所有文件和文件夹
LoadDirectory(_T("C:\\"), TVI_ROOT);
return TRUE;
}
void CMyDlg::LoadDirectory(LPCTSTR lpszPath, HTREEITEM hParent)
{
CFileFind finder;
BOOL bWorking = finder.FindFile(lpszPath + _T("\\*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if (finder.IsDirectory())
{
// 获取文件夹的系统图标
SHFILEINFO shfi;
SHGetFileInfo(finder.GetFilePath(), FILE_ATTRIBUTE_DIRECTORY, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON);
int nImage = m_ImageList.Add(shfi.hIcon);
// 在树控件中插入文件夹
HTREEITEM hItem = m_TreeCtrl.InsertItem(finder.GetFileName(), nImage, nImage, hParent);
LoadDirectory(finder.GetFilePath(), hItem);
}
else
{
// 获取文件的系统图标
SHFILEINFO shfi;
SHGetFileInfo(finder.GetFilePath(), 0, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON);
int nImage = m_ImageList.Add(shfi.hIcon);
// 在树控件中插入文件
m_TreeCtrl.InsertItem(finder.GetFileName(), nImage, nImage, hParent);
}
}
finder.Close();
}
在MFC(Microsoft Foundation Class)应用程序中使用CTreeCtrl控件显示文件系统中各种文件的图标,您需要以下几个步骤:
初始化CImageList对象并将其与树控件关联。
递归遍历文件系统,获取文件和文件夹的图标。
将文件和文件夹添加到树控件中。
以下是一个简单的示例:
首先,创建一个MFC对话框应用程序并添加一个CTreeCtrl控件。
接下来,添加以下头文件:
#include <shlobj.h>
#include <Shlwapi.h>
在对话框类中添加以下成员变量:
CImageList m_ImageList;
CTreeCtrl m_TreeCtrl;
然后,在OnInitDialog()方法中,初始化CImageList对象并将其与树控件关联:
```cpp
m_ImageList.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
m_TreeCtrl.SetImageList(&m_ImageList, TVSIL_NORMAL);
接下来,定义一个递归函数以遍历文件系统,并使用SHGetFileInfo()函数获取文件和文件夹的图标。将它们添加到图像列表中,然后将它们插入到树控件中:
void PopulateTree(HTREEITEM hParent, CString path)
{
CFileFind finder;
BOOL bWorking = finder.FindFile(path + _T("\\*.*"));
SHFILEINFO shFileInfo;
int nIndex;
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if (finder.IsDirectory())
{
CString strDir = finder.GetFilePath();
SHGetFileInfo(strDir, 0, &shFileInfo, sizeof(shFileInfo), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
nIndex = m_ImageList.Add(shFileInfo.hIcon);
HTREEITEM hItem = m_TreeCtrl.InsertItem(finder.GetFileName(), nIndex, nIndex, hParent);
PopulateTree(hItem, strDir);
}
else
{
CString strFile = finder.GetFilePath();
SHGetFileInfo(strFile, 0, &shFileInfo, sizeof(shFileInfo), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
nIndex = m_ImageList.Add(shFileInfo.hIcon);
m_TreeCtrl.InsertItem(finder.GetFileName(), nIndex, nIndex, hParent);
}
}
finder.Close();
}
最后,调用PopulateTree()函数并传递根目录作为参数:
PopulateTree(TVI_ROOT, _T("C:\\")); // 更改为您想要显示的目录
```
现在,运行应用程序,您应该看到文件系统中各种文件的图标显示在树控件中。