首页  编辑  

树和路径间转换

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:树和路径间转换

说明:比如这样的路径:"C:\Windows\System\user32.dll"

设计:Zswang

日期:2002-05-02

支持:wjhu111@21cn.com

//*)

///////Begin Source

function StrLeft(const mStr: string; mDelimiter: string): string;

begin

 Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);

end; { StrLeft }

function ListCount(mList: string; mDelimiter: string = ','): Integer;

var

 I, L: Integer;

begin

 Result := 0;

 if mList = '' then Exit;

 L := Length(mList);

 I := Pos(mDelimiter, mList);

 while I > 0 do begin

   mList := Copy(mList, I + Length(mDelimiter), L);

   I := Pos(mDelimiter, mList);

   Inc(Result);

 end;

 Inc(Result);

end; { ListCount }

function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;

var

 I, L, K: Integer;

begin

 L := Length(mList);

 I := Pos(mDelimiter, mList);

 K := 0;

 Result := '';

 while (I > 0) and (K <> mIndex) do begin

   mList := Copy(mList, I + Length(mDelimiter), L);

   I := Pos(mDelimiter, mList);

   Inc(K);

 end;

 if K = mIndex then Result := StrLeft(mList + mDelimiter, mDelimiter);

end; { ListValue }

function TreeNodeString(mTreeNode: TTreeNode; mDelimiter: string = '\'): string;

begin

 Result := '';

 while Assigned(mTreeNode) do begin

   Result := mTreeNode.Text + mDelimiter + Result;

   mTreeNode := mTreeNode.Parent;

 end;

 Delete(Result, Length(Result) - Length(mDelimiter) + 1, MaxInt);

end; { TreeNodeString }

function TreeViewToLineText(mTreeView: TTreeView; mStrings: TStrings;

 mDelimiter: string = '\'): Boolean;

var

 I: Integer;

begin

 Result := False;

 if not (Assigned(mTreeView) and Assigned(mStrings)) then Exit;

 mStrings.Clear;

 for I := 0 to mTreeView.Items.Count - 1 do

   if mTreeView.Items[I].Count = 0 then

     mStrings.Add(TreeNodeString(mTreeView.Items[I], mDelimiter));

 Result := True;

end; { TreeViewToLineText }

function LineTextToTreeView(mStrings: TStrings; mTreeView: TTreeView;

 mDelimiter: string = '\'): Boolean;

var

 I, J, K: Integer;

 vStrPath: string;

 vStrText: string;

 vTreeNode: TTreeNode;

 vBoolFind: Boolean;

begin

 Result := False;

 if not (Assigned(mTreeView) and Assigned(mStrings)) then Exit;

 mTreeView.Items.Clear;

 for I := 0 to mStrings.Count - 1 do begin

   vStrPath := '';

   vTreeNode := nil;

   for J := 0 to ListCount(mStrings[I], mDelimiter) - 1 do begin

     vStrText := ListValue(mStrings[I], J, mDelimiter);

     vStrPath := vStrPath + mDelimiter + vStrText;

     vBoolFind := False;

     for K := 0 to mTreeView.Items.Count - 1 do

       if mDelimiter + TreeNodeString(mTreeView.Items[K], mDelimiter) =

         vStrPath then begin

         vTreeNode := mTreeView.Items[K];

         vBoolFind := True;

         Break;

       end;

     if vBoolFind then Continue;

     vTreeNode := mTreeView.Items.AddChild(vTreeNode, vStrText);

   end;

 end;

 Result := True;

end; { LineTextToTreeView }

///////End Source

///////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

 LineTextToTreeView(Memo1.Lines, TreeView1);

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

 TreeViewToLineText(TreeView1, Memo1.Lines);

end;

///////End Demo