用TreeView浏览目录

2008-02-23 05:38:58来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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;
}




标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 如何取得Memo的行和列

下一篇: 改变DBGrid的颜色

热门词条
热门标签