>I want to use a Treeview to display a list like Outlook Expresses
mail box.
>Each item is a file NOT a folder, but looks like a folder in the treeview.
>
If you merely want to represent a hierarchical list, you can simply
add
nodes, otherwise you might want to add objects, ie nodes + data (eg
you
might need the file data for each file you add to the node, so you
use a
record to hold the data, then create a new instance and fill it every
time
you you add an item).
//Each TTreeView rebuild, get rid of any old stuff
TV.items.Clear;
//Add your root node
rootNode := TV.items.Add(nil, nodeTitle);
//Or add a root object
rootNode := TV.items.AddObject(nil, nodeTitle, myRecord);
{-------------------------------------------------------------}
//Add child nodes
thisNode := TV.items.AddChild(refNode, nodeTitle);
//Or add child objects
thisNode := TV.items.AddChildObject(refNode, nodeTitle, myRecord);
{-------------------------------------------------------------}
//Let's have a simple record:
type
pfData = ^fData;
fileData = record
fileSize: longInt;
isDir: boolean;
end;
{-------------------------------------------------------------}
//Start the ball rolling by filling the root node
pData.fileSize := 0;
pData.isDir := true;
rootNode := TV.items.AddObject(nil, 'Root directory', pData);
//Then recurse through the directories:
fillTree('C:\', rootNode);
{-------------------------------------------------------------}
unit recfiles;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
TV: TTreeView;
mtree: TButton;
procedure fillTree(currentDir: string; refNode:
TTreeNode);
procedure mtreeClick(Sender: TObject);
procedure TVMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{Private declarations }
rootNode: TTreeNode;
expCol: boolean;
public
{Public declarations }
end;
type
pfData = ^fileData;
fileData = record
fileSize: longInt;
isDir: boolean;
end;
var
Form1: TForm1;
SearchRec: TSearchRec;
pData: pfData;
implementation
{$R *.DFM}
procedure TForm1.fillTree(currentDir: string; refNode: TTreeNode);
var
thisNode: TTreeNode;
res: longInt;
sRec: TSearchRec;
begin
res := FindFirst(currentDir + '*.*', faAnyFile, SearchRec);
while res = 0 do
begin
new(pData);
pData.fileSize := SearchRec.Size;
pData.isDir := (SearchRec.Attr = faDirectory);
if pData.isDir then
begin
//Exclude '.' and '..' directories from
the recursion
if (SearchRec.Name <> '.') and (SearchRec.Name
<> '..') then
begin
thisNode := TV.items.AddChildObject(refNode,
SearchRec.Name, pData);
sRec := SearchRec;
//Save SearchRec till we return
fillTree(currentDir + SearchRec.Name
+ '\', thisNode);
SearchRec := sRec;
//and restore it
end;
end
else
TV.items.AddChildObject(refNode, SearchRec.Name,
pData);
res := FindNext(SearchRec);
end;
end;
procedure TForm1.mtreeClick(Sender: TObject);
var
currentTVD: pfData;
begin
//Start the ball rolling by filling the root node
SetCurrentDirectory('C:\');
TV.items.Clear;
new(pData);
pData.fileSize := 0;
pData.isDir := true;
rootNode := TV.items.AddObject(nil, 'Root directory', pData);
TV.Items.BeginUpdate;
//Then recurse through the directories:
fillTree('C:\', rootNode);
//Hardwired C:\
rootNode.Expand(false);
TV.Items.EndUpdate;
end;
procedure TForm1.TVMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
MyHitTest : THitTests;
currentTVD: pfData;
begin
MyHitTest := TV.GetHitTestInfoAt(X,Y); //Let's make
sure we're on a label
if htOnLabel in MyHitTest then
//
begin
currentTVD := pfData(TV.Selected.Data);
if currentTVD.isDir then
messageDlg(TV.Selected.Text + ' is a
directory', mtInformation,
[mbOk], 0)
else
messageDlg('Size of file ' + TV.Selected.Text
+ ' is ' +
IntToStr(currentTVD.fileSize), mtInformation, [mbOk], 0);
end
else
expCol := false
end;
end.
----------