<b>用TreeView浏览目录</b>[VC/C++编程]
本文“<b>用TreeView浏览目录</b>[VC/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;
}
以上是“<b>用TreeView浏览目录</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |