程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 用TreeView浏覽目錄

用TreeView浏覽目錄

編輯:關於C++

void __fastcall TForm1::FormCreate(TObject *Sender)
{
CurrentDir=GetCurrentDir();
UpdateTreeView();
UpdateListView();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::UpdateTreeView()
{
TTreeNode *rNode,*mNode;
TreeView1->Items->Clear();
rNode=TreeView1->Items->Add(TreeView1->Selected,"我的電腦");
rNode->ImageIndex=1;
rNode->SelectedIndex=1;
mNode=TreeView1->Items->AddChild(rNode,"A:");
mNode->ImageIndex=2;
mNode->SelectedIndex=2;
AddDirectory("A:",mNode);
for(int i=1;i<DriveComboBox1->Items->Count;i++)
  {
  mNode=TreeView1->Items->AddChild(rNode,AnsiString(char('B'+i))+":");
  mNode->ImageIndex=3;
  mNode->SelectedIndex=3;
  AddDirectory(AnsiString(char('B'+i))+":",mNode);
  }
}
//------------------------------------------------------------------------
void TForm1::AddDirectory(AnsiString path,TTreeNode *fNode)
{
TSearchRec sr;
TTreeNode *mNode;
if(FindFirst(path+"\\*.*" ,faDirectory,sr)==0)
  {
  if(sr.Attr==faDirectory)
   {
   if(sr.Name!="."&&sr.Name!="..")
    {
    mNode=TreeView1->Items->AddChild(fNode,sr.Name);
    mNode->ImageIndex=0;
    mNode->SelectedIndex=4;
    // AddDirectory(path+"\\"+sr.Name,mNode);
    }
   }
  }
while(FindNext(sr)==0)
  {
  if(sr.Attr==faDirectory)
   {
   if(sr.Name!="."&&sr.Name!="..")
    {
    mNode=TreeView1->Items->AddChild(fNode,sr.Name);
    mNode->ImageIndex=0;
    mNode->SelectedIndex=4;
    // AddDirectory(path+"\\"+sr.Name,mNode);
    }
   }
  }
FindClose(sr);
}
//--------------------------------------------------------------------
void __fastcall TForm1::TreeView1Changing(TObject *Sender, TTreeNode *Node, bool &AllowChange)
{
Screen->Cursor=crHourGlass;
TSearchRec sr;
TTreeNode *NodeTmp=Node;
AnsiString DirName,DirTmp;
DirName=Node->Text;
for(int i=Node->Level;i>1;i--)
  {
  NodeTmp=NodeTmp->Parent;
  DirTmp=NodeTmp->Text;
  if(DirTmp.SubString(DirTmp.Length(),1)!="\\")
  DirTmp+="\\";
  DirName.Insert(DirTmp,0);
  }
if(DirName.SubString(DirName.Length(),1)!="\\")
  DirName+="\\";
for(int j=0;j<Node->Count;j++)
  {
  if(Node->Item[j]->Count==0);
   {
   if(FindFirst(DirName+Node->Item[j]->Text+"\\*.*", faAnyFile, sr) == 0)
    {
    do
     {
     //Node->Item[j]->ImageIndex=5;
     //Node->Item[j]->SelectedIndex=4;
     if((sr.Attr&faDirectory)&&sr.Name!="."&&sr.Name!="..")
      {
      Node->Item[j]->ImageIndex=0;
      Node->Item[j]->SelectedIndex=4;
      TreeView1->Items->AddChild(Node->Item[j],sr.Name);
      }
     }
     while (FindNext(sr)==0);
     FindClose(sr);
    }
   }
  }
Screen->Cursor=crDefault;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved