Using C++builder, we can easily implement a directory tree of Windows-like explorer. Ok,let ' s go.
We need a ttree and a timagelist control. How do I get the system's drive letter? Borland gives us a tdrivetype class, what? Help can not find, oh, this is the implementation of a class used Tdirectorycombobox, the help is of course not, it is defined as follows:
typedef enum ATTR { atDirectory,atFile} Attr;
struct ITEMPROPERTY
{
Attr ItemAttr;
}ItemProperty;
typedef enum TDRIVETYPE {dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,dtRAM}TDriveType;
So how do you know if these are floppy drives or hard disks? This requires the use of the WINAPI function GetDriveType. The definition is as follows:
UINT GetDriveType (LPCTSTR lprootpathname); For details please refer to the Help document, I will not be here to delay everyone's time. Finally, we use the BCB findfirst,findnext function to achieve the directory traversal.
The following two functions Initdriectorytree are used to obtain the disk characters and their properties, browsedriectory the recursive method to achieve the directory traversal, specifically implemented as follows:
BOOL __fastcall Tform1::initdirectorytree ()
{
Drivecount=getalldrive ();
Drivetype=new Tdrivetype[4];
for (int i=drivecount;i>0;i--)
{
ttreenode* node=dirtree->items->item[i];
Drivetype[i]=tdrivetype (GetDriveType (Node->text.c_str ()));
Switch (Drivetype[i])
{
Case Dtfloppy://Floppy drive
Case Dtfixed:browsedirectory (node->text,node); break;//Hard Drive
Case Dtnetwork:browsedirectory (node->text,node); break;//Network Drive
Case DTCDROM://Optical drive
Case Dtram:browsedirectory (Node->text,node); Memory virtual disk? Not quite clear, probably like the E: Disk generated by the Win98 boot disk
};
}
return true;
Todo:add Your source code here
}
//---------------------------------------------------------------------------
void __fastcall tform1::browsedirectory (ansistring Dir, ttreenode* Node)
{
Tsearchrec SR;
ttreenode* Subnode=node;
if (FindFirst (dir+ "*.*", FADIRECTORY,SR) ==0)
while (1)
{if (Sr. ATTR==FADIRECTORY&&SR. Name!= "." &&SR. Name!= "..")
{
Subnode=dirtree->items->addchild (NODE,SR. Name);
Browsedirectory (DIR+SR. Name+ "\", subnode);
}
if (FindNext (SR)!=0) break;
}
FindClose (SR);
Return
Todo:add Your source code here
}
//---------------------------------------------------------------------------
Above we use C++builder to achieve the access to the computer file system, in fact, we can use the above function to create a file system can access the VCL control to replace the BCB provided Tdirectorylistbox and Tdirectorycombobox, Make your program more Win9x style.