对你的问题,有如下两中情况:
1、帐户是从windows系统中取得登录帐户
2、你自己在程序中维护帐户和密码信息,并且在用户进入程序时时校验用名/密码
第一种情况可以采用我上面贴的方法解决。
对第二种情况,说明如下:
首先我觉得你的思路有点问题,要达到你的目的,可以根据用户登录的用户名来动态
生成树,即当登录的用户时普通用户时,只生成此用户的节点,而当是超级用户登录
时,生成所有的节点。
实现如下:
// 在树中添加一个节点
// 参数说明:
// vTreeView:要动态添加节点的树
// vParentNode: 节点的父节点,为nil时添加根节点
// NodeName: 节点名
// ImageNO: 此节点的图标在TImageList控件中的位置(首先定义一个TImageList,并和
树的Images以及stateImages属性关联上)
function TreeAddNode(vTreeView:TTreeView;vParentNode:TTreeNode;NodeName:string;ImageNO:integer):TTreeNode;
var
TmpNode:TTreeNode;
begin
with vTreeView.Items do
begin
if vParentNode = nil then
TmpNode:=Add(nil,NodeName)
else
TmpNode:=Addchild(vParentNode,NodeName);
with TmpNode do
if ImageNo< 5 then
begin
ImageIndex :=ImageNo;
SelectedIndex:=ImageNo+6;
end else
begin
ImageIndex := 5;
SelectedIndex:= 11; //5+6
end;
end;
Result:=TmpNode;
end;
再为树写一个添加树节点的procedure 便可以了。下面是我
为一个邮件收发程序写的动态树生成过程。
首先它从以下记录格式的system.ms中读取信息
[system Paramaters]
UserNumber=3
[User1]
UserName=stonepine
MailAddr=D:/share/Mailspirit/Mail/stonepine
....
再根据mailAddr路径下的mail.ms文件中以下记录格式
[MailBoxInfo]
UserID=stonepine
mailBox1=收件箱#stonepine#In
mailBox2=发件箱#stonepine#Out
mailBox3=已发件箱#stonepine#send
mailBox4=垃圾箱#stonepine#trash
mailBox5=new1#收件箱#stonepine#New1
mailBoxnum=5
AccessPassword=×××
...
来生成用户邮箱树.
Procedure TMainForm.Tree1AddNode ;
Const
ParentStr:string ='#';
var
tmpUserNum,MaxUserNum,MailBoxnum,tmpNum,CurrentIndex,TmpLevel:integer;
PersonalInfoFile,NodeName,tmpStr:string;
vParentNode,CurrentNOde:TTreeNode;
TmpMyIniFile:TiniFile;
begin
MaxUserNum := sysIniFile.ReadInteger('system Paramaters','UserNumber',0);
for tmpUserNum:=1 to MaxUserNum do
begin
UserMailPath:=sysIniFile.Readstring('User'+intToStr(tmpUserNum),'MailAddr',sAppworkPath+'/mail');;
PersonalInfoFile:=UserMailPath+'/mail.ms';
tmpmyIniFile:=Tinifile.Create(PersonalInfoFile);
with tmpmyIniFile do begin
vParentNode:=TreeAddNode(TreeView1,nil,sysIniFile.Readstring('User'+intToStr(tmpUserNum),'UserName',''),0);
//初始化省缺用户
if ReadString('MailBoxInfo','AccessPassWord','')<>'' then vParentNode.StateIndex :=12;
if tmpUserNum = MaxUserNum then
begin
TreeView1.Selected:=vParentNode;
TreeView1.Selected.Collapse(False);
end;
MailBoxnum:=Readinteger('MailBoxInfo','mailBoxnum',0);
CurrentINdex:=0;
while CurrentIndex < MailBoxnum do
begin
//父接点下移
if CurrentIndex<>0 then vParentNode:=Treeview1.Items.Item[vParentNode.AbsoluteIndex+1];
CurrentNode:= vParentNode; TmpLevel:=vParentNode.Level; ParentStr:='#';
while TmpLevel>= 0 do
begin
ParentStr:=ParentStr+CurrentNode.Text+'#';
if TmpLevel=0 then Break;
CurrentNode:=CurrentNode.Parent;
Dec(TmpLevel);
end;
for tmpnum:=1 to MailBoxnum do
begin
tmpstr:=Readstring('MailBoxInfo','mailBox'+intTostr(tmpNum), '');
NodeName:=GetToken(ParentStr,tmpStr);
if (NodeName<>'') and (Pos('#',NodeName)=0) then
begin
TreeAddNode(TreeView1,vParentNode,NodeName,tmpNum);
CurrentIndex:=CurrentIndex+1;
end;
end;
end;
end;
end;
end;