这是我在网上找到的代码,但是只能显示三层目录,怎样能获得磁盘下所有目录并用树控件实现? 代码如下:
BOOL CCFileTreeDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here////////////////////////////////////////////////////////////////////////////////////////////
//读取磁盘文件
m_ImageList.Create(16,16,ILC_COLOR16,0,0); m_treeFile.SetImageList(&m_ImageList,TVSIL_NORMAL); m_treeFile.ModifyStyle(0L,TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT); size_t alldriver = ::GetLogicalDriveStrings(0,NULL); _TCHAR *driverstr; driverstr = new _TCHAR[alldriver + sizeof(_T(""))]; if(GetLogicalDriveStrings(alldriver,driverstr) != alldriver-1) return FALSE; _TCHAR *pdriverstr = driverstr; size_t driversize = strlen(pdriverstr); HTREEITEM disktree; while(driversize>0) { SHGetFileInfo(pdriverstr,0,&fileinfo,sizeof(fileinfo),SHGFI_ICON); imindex = m_ImageList.Add(fileinfo.hIcon); disktree = m_treeFile.InsertItem(pdriverstr,imindex,imindex,TVI_ROOT,TVI_LAST); pdriverstr += driversize+1; driversize = strlen(pdriverstr); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}void CCFileTreeDlgDlg::OnDblclkTREEFile(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CFileFind filefd; HTREEITEM parent; HTREEITEM item = m_treeFile.GetSelectedItem(); if(m_treeFile.GetChildItem(item))return; parent = item; CString rootstr = m_treeFile.GetItemText(item); CString temp; CString lstr; if(rootstr.Find("\\") == 2)
{ lstr.Format("%s*.*",rootstr); } else { CString strparent; while(1) { parent = m_treeFile.GetParentItem(parent); strparent = m_treeFile.GetItemText(parent); if(strparent.Find("\\") == 2) goto end;
//CString root = m_treeFile.GetItemText(parent); //lstr.Format("%s%s%s\\*.*",root,temp,rootstr); temp += strparent; temp += "\\"; }
end: CString root = m_treeFile.GetItemText(parent); lstr.Format("%s%s%s\\*.*",root,temp,rootstr); } BOOL bfinded = filefd.FindFile(lstr); while(bfinded) { bfinded = filefd.FindNextFile(); if(filefd.IsDirectory()&&!filefd.IsDots()) { SHGetFileInfo(filefd.GetFilePath(),0,&fileinfo,sizeof(fileinfo),SHGFI_ICON); imindex = m_ImageList.Add(fileinfo.hIcon); m_treeFile.InsertItem(filefd.GetFileName(),imindex,imindex,item); } }
*pResult = 0;
}
BOOL CCFileTreeDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here////////////////////////////////////////////////////////////////////////////////////////////
//读取磁盘文件
m_ImageList.Create(16,16,ILC_COLOR16,0,0); m_treeFile.SetImageList(&m_ImageList,TVSIL_NORMAL); m_treeFile.ModifyStyle(0L,TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT); size_t alldriver = ::GetLogicalDriveStrings(0,NULL); _TCHAR *driverstr; driverstr = new _TCHAR[alldriver + sizeof(_T(""))]; if(GetLogicalDriveStrings(alldriver,driverstr) != alldriver-1) return FALSE; _TCHAR *pdriverstr = driverstr; size_t driversize = strlen(pdriverstr); HTREEITEM disktree; while(driversize>0) { SHGetFileInfo(pdriverstr,0,&fileinfo,sizeof(fileinfo),SHGFI_ICON); imindex = m_ImageList.Add(fileinfo.hIcon); disktree = m_treeFile.InsertItem(pdriverstr,imindex,imindex,TVI_ROOT,TVI_LAST); pdriverstr += driversize+1; driversize = strlen(pdriverstr); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}void CCFileTreeDlgDlg::OnDblclkTREEFile(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CFileFind filefd; HTREEITEM parent; HTREEITEM item = m_treeFile.GetSelectedItem(); if(m_treeFile.GetChildItem(item))return; parent = item; CString rootstr = m_treeFile.GetItemText(item); CString temp; CString lstr; if(rootstr.Find("\\") == 2)
{ lstr.Format("%s*.*",rootstr); } else { CString strparent; while(1) { parent = m_treeFile.GetParentItem(parent); strparent = m_treeFile.GetItemText(parent); if(strparent.Find("\\") == 2) goto end;
//CString root = m_treeFile.GetItemText(parent); //lstr.Format("%s%s%s\\*.*",root,temp,rootstr); temp += strparent; temp += "\\"; }
end: CString root = m_treeFile.GetItemText(parent); lstr.Format("%s%s%s\\*.*",root,temp,rootstr); } BOOL bfinded = filefd.FindFile(lstr); while(bfinded) { bfinded = filefd.FindNextFile(); if(filefd.IsDirectory()&&!filefd.IsDots()) { SHGetFileInfo(filefd.GetFilePath(),0,&fileinfo,sizeof(fileinfo),SHGFI_ICON); imindex = m_ImageList.Add(fileinfo.hIcon); m_treeFile.InsertItem(filefd.GetFileName(),imindex,imindex,item); } }
*pResult = 0;
}
你可以直接用, 也可以参考它的源码自己重载个.
函数功能:获取驱动器参数说明:hParent 获取树控件的父节点**************************************************************************/
void CTreeViewDlg::GetLogicalDrives(HTREEITEM hParent)
{
size_t szAllDriveStrings = GetLogicalDriveStrings(0,NULL);//获取磁盘中的各个盘符的信息
char *pDriveStrings = new char[szAllDriveStrings + sizeof(_T(""))];
GetLogicalDriveStrings(szAllDriveStrings,pDriveStrings);
size_t szDriveString = strlen(pDriveStrings);
while(szDriveString > 0)
{
m_tree.InsertItem(pDriveStrings,hParent);
pDriveStrings += szDriveString + 1;
szDriveString = strlen(pDriveStrings);
}
}
/**********************************************************************
函数功能:获取磁盘路径信息参数说明:hParent 树控件的父节点每个文件夹都有下面两个特殊子文件夹:
(1) .
表示本文件夹自己
(2) ..
表示本文件夹的父文件夹
显然,在文件夹遍历的时候,这两个子文件夹需要特殊处理,
否则将陷入死循环。 IsDots()
就是判断是不是这两个文件夹的一个. ***********************************************************************/void CTreeViewDlg::GetDriveDir(HTREEITEM hParent)
{
HTREEITEM hChild = m_tree.GetChildItem(hParent); //获取此节点的孩子节点的句柄
while(hChild)
{
CString strText = m_tree.GetItemText(hChild); //获取此孩子几点的文本信息
if(strText.Right(1) != "\\") //其右端不含有路径分隔符
strText += _T("\\"); //获取的是其文件的绝对路径
strText += "*.*"; //使用通配符来标示文件为*.*
CFileFind file;
BOOL bContinue = file.FindFile(strText); //通过其文本标示来标示是否有这个文件
while(bContinue)
{
bContinue = file.FindNextFile();
if(file.IsDirectory() && !file.IsDots())
m_tree.InsertItem(file.GetFileName(),hChild);
}
GetDriveDir(hChild); //获取还在节点的磁盘路径
hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
}
}
/**********************************************************************
函数功能:展开事件函数,添加TVN_EXPANDED消息处理函数,当一项展开时,为其子项添加下一级目录参数说明:
***********************************************************************/
void CTreeViewDlg::OnItemexpandedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
HTREEITEM hChild = m_tree.GetChildItem(item.hItem);
while(hChild)
{
AddSubDir(hChild);
hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
}
*pResult = 0;
}
/**********************************************************************
函数功能:获取其绝对路径信息参数说明: hParent
***********************************************************************/
CString CTreeViewDlg::GetFullPath(HTREEITEM hCurrent)
{
CString strTemp;
CString strReturn = ""; //定义其目录的决定路径的字符串标示
while(hCurrent != m_hRoot) //不为根节点的话
{
strTemp = m_tree.GetItemText(hCurrent); //获取当前节点的文本信息
if(strTemp.Right(1) != "\\")
strTemp += "\\";
strReturn = strTemp + strReturn;
hCurrent = m_tree.GetParentItem(hCurrent);
}
return strReturn;
}
/**********************************************************************
函数功能:添加子目录参数说明: hParent
***********************************************************************/
void CTreeViewDlg::AddSubDir(HTREEITEM hParent)
{
CString strPath = GetFullPath(hParent);
if(strPath.Right(1) != "\\")
strPath += "\\";
strPath += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strPath);
while(bContinue)
{
bContinue = file.FindNextFile();
if(file.IsDirectory() && !file.IsDots())
m_tree.InsertItem(file.GetFileName(),hParent);
}
}
/**********************************************************************
函数功能:选中节点触发的事件来列表显示其文件夹中的文件参数说明:
shiftinfo 包含一个文件的信息
***********************************************************************/
void CTreeViewDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
m_list.DeleteAllItems();
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
CString str = GetFullPath(item.hItem);
if(str.Right(1) != "\\")
str += "\\";
str += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(str);
while(bContinue)
{
bContinue = file.FindNextFile();
if(!file.IsDirectory() && !file.IsDots())
{
SHFILEINFO info; //包含一个文件的信息
CString temp = str;
int index = temp.Find("*.*");
temp.Delete(index,3);
SHGetFileInfo(temp + file.GetFileName(),0,&info,sizeof(&info),SHGFI_DISPLAYNAME | SHGFI_ICON);
int i = m_ImageList.Add(info.hIcon);
m_list.InsertItem(i,info.szDisplayName,i);
}
}
*pResult = 0;
}在网上找的,做个参考