希望以下这段转发代码会对你有所帮助。
--------------------------
// 获取用户组线程
TGetUserGroupsThead = class(TThread)
private
FOwnerHandle: HWND;
FUnitId: WideString;
FParentNode, tmpParentNode: TTreeNode;
FtvUnits: TTreeView;
FRecursive: Boolean;
FIsExpandAll: Boolean;
FRIO: TMyRIO;
// 私有变量
aUserGroup: TUserGroup;
p: ^TObject;
aNode: TTreeNode;
// 线程结束时通知父窗口
procedure FinalAct(Sender: TObject);
// 查询
procedure ListChildNode(parentId: String;
parentNode: TTreeNode);
// 同步添加节点
procedure AddNode;
protected
// 线程执行体
procedure Execute;
override;
public
// 构造函数
constructor Create(// 所有者窗口句柄
const OwnerHandle: HWND;
// 远程操作组件
RIO: TMyRIO;
// 单元ID
UnitId: WideString;
// 是否查询子单元
IsRecursive: Boolean;
// 父节点
ParentNode: TTreeNode;
// 节点
tvUnits: TTreeView;
// 是否全部展开
IsExpandAll: Boolean=true);
end;
{ TGetUserGroupsThead }
// 操作VCL组件,该方法必须同步调用
procedure TGetUserGroupsThead.AddNode;
begin
aNode := FtvUnits.Items.AddChildObject(tmpParentNode, aUserGroup.UnitName, p);
if FIsExpandAll then
tmpParentNode.Expand(True);
end;
constructor TGetUserGroupsThead.Create(const OwnerHandle: HWND;
RIO: TMyRIO;
UnitId: WideString;
IsRecursive: Boolean;
ParentNode: TTreeNode;
tvUnits: TTreeView;
IsExpandAll: Boolean);
begin
FOwnerHandle := OwnerHandle;
FRIO := RIO;
FUnitId := UnitId;
FRecursive := IsRecursive;
FParentNode := ParentNode;
FtvUnits := tvUnits;
FIsExpandAll := IsExpandAll;
OnTerminate := FinalAct;
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TGetUserGroupsThead.Execute;
begin
CoInitialize(nil);
ListChildNode(FUnitId, FParentNode);
end;
procedure TGetUserGroupsThead.FinalAct(Sender: TObject);
begin
// 通知窗口线程已经结束
PostMessage(FOwnerHandle, WM_GetUnitsTheadDoneMsg, 0, 0);
end;
procedure TGetUserGroupsThead.ListChildNode(parentId: String;
parentNode: TTreeNode);
var
rs: ArrayOf_tns1_UserGroup;
i: Integer;
begin
// 下面是通过WebService取得用户组列表,你不用管
FRIO.Acquire;
try
rs := FRIO.ua.userGroupSearchUserGroups(FRIO.UserAuthConnection.LoginId,
FRIO.UserAuthConnection.LoginPwd, parentId, UA_FALSE);
finally
FRIO.Release;
end;
// 递归添加树型节点,因为只能每次取得当前节点的子节点所以使用了递归(服务器端接口的限制)
for i := Low(rs) to High(rs)do
begin
aUserGroup := TUserGroup.Create;
aUserGroup.aUserGroup := rs;
aUserGroup.UserAuthConnection := FRIO.UserAuthConnection;
new(p);
p^ := aUserGroup;
tmpParentNode := parentNode;
// 使用同步方法添加到form上的TreeView控件中
Synchronize(AddNode);
aUserGroup.Owner := aNode;
if FRecursive then
ListChildNode(aUserGroup.UserGroupId, aNode);
end;
end;