VirtualTreeView - achando um no pelo index |
Top Previous Next |
|
function TForm1.GetNode(InitialNode: PVirtualNode; Index: Integer): PVirtualNode; var i, j: Integer; aNode: PVirtualNode;
//--------------- local function --------------------------------------------
function Estimate(Node: PVirtualNode; Dst: Integer): PVirtualNode; begin with VirtualStringTree1 do begin // The trick is to move the scrollbar OffsetY:= OffsetY - ((Dst *(Integer(RangeY) - Height)) div Integer(VisibleCount)); Result:= TopNode; // if we're in a branch we must climb up while GetNodeLevel(Result) > GetNodeLevel(Node) do Result:= Result.Parent; // if one of the following will happen we've jumped too far if GetNodeLevel(Result) < GetNodeLevel(Node) then Result:= Estimate(Node, Dst div 3 *2); if Result.Parent <> Node.Parent then Result:= Estimate(Node, Dst div 3 *2); end end;
//--------------- end local function ----------------------------------------
begin if InitialNode = nil then begin if VirtualStringTree1.FocusedNode = nil then aNode:= VirtualStringTree1.GetFirst else aNode:= VirtualStringTree1.FocusedNode; end else aNode:= InitialNode; Result:= nil; if aNode <> nil then begin aNode:= Estimate(aNode, Index -Integer(aNode.Index)); j:= aNode.Index; if j < Index then for i:= j to Index -1 do aNode:= VirtualStringTree1.GetNextSibling(aNode) else if j > Index then for i:= j downto Index +1 do aNode:= VirtualStringTree1.GetPreviousSibling(aNode); Result:= aNode; end; end; |