如何用TREE控件显示一个文件夹下的所有文件包括其中的文件夹

解决方案 »

  1.   

    http://www.vckbase.com/document/viewdoc.asp?id=806
      

  2.   

    FindFirstFile,FindNextFile找到所有文件,然后InsertItem插入到treectrl中
      

  3.   

    主要算法如下:
    void CFileTreeDlg::AddFile(CString StrPath, HTREEITEM faItem )
    //StrPath为传递过来的目录层次,本次函数调用中搜索的文件都是它的下一层的。
    //faItem为传递过来的Tree节点,本次函数调用中添加的Tree节点都是它的子节点。
    {
    CFileFind OneFile;
    CString FName, DirName;
    BOOL BeWorking; 
    HTREEITEM NewItem; DirName = StrPath+"\\*.*";
    BeWorking = OneFile.FindFile( DirName );
    while ( BeWorking ) {   //BeWorking非零,指找了文件或目录 //查找同级的目录
    BeWorking = OneFile.FindNextFile(); if ( OneFile.IsDirectory() && !OneFile.IsDots() )        //如果查找的结果是目录又不是".."或"."
    {
    //向Tree1中添加目录;
    DirName = OneFile.GetFilePath();
    FName = OneFile.GetFileTitle(); 
    //IDC_TREE1
    NewItem = m_Tree.InsertItem( FName, faItem );    //NewItem取得节点,其目的是为了下一层中
     //添加节点方便,递归时把它传过去。
    //进入下一层递归调用。
    AddFile(DirName, NewItem);
    } //退出递归时,到了这里!!!
    if ( !OneFile.IsDirectory() && !OneFile.IsDots() )       //如果查找结果是文件
    {
    //向Tree1中添加文件
    FName = OneFile.GetFileTitle();  //注意这里用的是GetFileTitle,因为
     //这里是添加文件。
    m_Tree.InsertItem( FName, faItem );
    }
    }// end of while

    OneFile.Close(); //记着用完CFileFild实例要关闭
    }      希望对你有所帮助!