用TreeView浏览目录
2008-03-08 12:57:44 来源:WEB开发网核心提示:void __fastcall TForm1::FormCreate(TObject *Sender) { CurrentDir=GetCurrentDir(); UpdateTreeView(); UpdateListView(); } //--- void __fastcall TForm1::UpdateTree
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;
}
更多精彩
赞助商链接