今天在软件里添加了一个树型目录,函数在网上找了一个可用的,然后自己做了一些修改,在这里记录下来以便日后使用。
void CJWYTreeViewd::BrowseFile(int CallNum, CString strFile)
{
CallNum++;
CFileFind m_Find;
CString szDir = strFile;
CTreeCtrl* pctlTree=&GetTreeCtrl();
if(szDir.Right(1) != "//")
szDir += "//";
szDir += "*.*";
BOOL res = m_Find.FindFile(szDir);
while(res)
{
res = m_Find.FindNextFile();
if(m_Find.IsDirectory() && !m_Find.IsDots())//目录是文件夹
{
//如果是一个子目录,用递归继续往深一层找
CString strPath = m_Find.GetFilePath(); //得到路径,做为递归调用的开始
CString strTitle = m_Find.GetFileTitle();//得到目录名,做为树控的结点
int i =0;
switch(CallNum)
{
case 1:
hRoot = pctlTree->InsertItem(strTitle,NULL);
break;
case 2:
hCurr = pctlTree->InsertItem(strTitle,hRoot);
break;
default:
hChild = pctlTree->InsertItem(strTitle,hCurr);
break;
}
BrowseFile(CallNum,strPath);//递归调用
}
else if(!m_Find.IsDirectory() && !m_Find.IsDots())//到达最低层的文件
{
//显示当前访问的文件
CString strPath;
CString strTitle;
strPath = m_Find.GetFilePath();
strTitle = m_Find.GetFileName();
switch(CallNum)
{
case 1:
hRoot = pctlTree->InsertItem(strTitle,NULL);
break;
case 2:
hCurr = pctlTree->InsertItem(strTitle,hRoot);
break;
default:
hChild = pctlTree->InsertItem(strTitle,hCurr);
break;
}
}
}
m_Find.Close();//关闭
}
可直接在初始化中调用
BrowseFile(0,"JMData");//遍历"JMData"文件夹内的所有目录