kk2000:经过修改,基本上可以了。完整代码:
procedure TForm1.SetNodeState(Node: TTreeNode; Check: Boolean);
var
ParentNode,
ParentNode_First,
vNode: TTreeNode;
TvItem: TTVItem;
begin
if Node = nil then Exit;
TreeView_Sum :=TreeView_Sum + 1;
while Node <> nil do
begin
ParentNode :=Node.Parent ;
if isParent = false then
begin
if (TreeView_Sum = 1) and
(ParentNode = nil) //选择的是顶级节点
then
isParent :=true
else
begin
if (TreeView_Sum = 1) and
(ParentNode <> nil) //选择的不是顶级节点
then
begin
isParent :=true;
ParentNode_First :=Node.Parent;
end
else
isParent :=false;
end;
end;
//开始递归循环
vNode := Node.getFirstChild;
FillChar(TvItem, Sizeof(TvItem), 0);
TvItem.hItem := Node.ItemId;
TvItem.mask := TVIF_STATE;
TvItem.stateMask := TVIS_STATEIMAGEMASK ;
if Check then
TvItem.state := TVS_CHECK
else
TvItem.state := TVS_CHECK shr 1;
TreeView_SetItem(Node.TreeView.Handle, TvItem);
SetNodeState(vNode, Check);
vNode := Node.getNextSibling; //获取下一个兄弟节点
if vNode <> nil then
begin
Node :=vNode;
if vNode.Parent =ParentNode_First then
exit;
end
else
exit;
if isParent = false then
begin
if (ParentNode <> nil) and
(ParentNode = vNode.Parent)
then //是同级别兄弟节点
exit;
end;
end;
end;
再次谢谢kk2000,加分了!