/// <summary> /// Retrieves the tree node that is at the specified vertical location. /// </summary> /// <returns>The TreeNode at the specified location, in tree view coordinates.</returns> /// <remarks> /// <para>You can pass the MouseEventArgs.X and MouseEventArgs.Y coordinates of the /// MouseDown event as the x and y parameters.</para> /// </remarks> /// <param name="y">The Y position to evaluate and retrieve the node from.</param> /// <param name="tree">Tree control to find node at.</param> /// <param name="paintedOnly">Enumerates rendered nodes only.</param> public static NodeHitTestInfo GetNodeAt(AdvTree tree, int y, bool paintedOnly, bool isUserInterfaceHitTest) { NodeHitTestInfo info = new NodeHitTestInfo(); if (tree.Nodes.Count == 0 || !tree.DisplayRectangle.Contains(1, y) && tree.Zoom == 1) return info; Point displayOffset = tree.NodeDisplay.Offset; // Enumerate painted node first if (tree.DisplayRectangle.Contains(1, y)) { ArrayList paintedNodes = tree.NodeDisplay.PaintedNodes; foreach (Node paintedNode in paintedNodes) { if (!paintedNode.IsDisplayed && !(paintedNode.Expanded && paintedNode.HasColumns)) continue; Rectangle r = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.NodeBounds, paintedNode, displayOffset); if (y >= r.Y && y <= r.Bottom) { info.NodeAt = paintedNode; return info; } else if(NodeDisplay.HasColumnsVisible(paintedNode)) { Rectangle headerBounds = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.ColumnsBounds, paintedNode, displayOffset); if (y >= headerBounds.Y && y <= headerBounds.Bottom) { info.ColumnsAt = paintedNode.NodesColumns; return info; } } } } if (paintedOnly) return info; Node node = tree.Nodes[0]; Node retNode = null; if (tree.DisplayRootNode != null) node = tree.DisplayRootNode; while (node != null) { Rectangle r = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.NodeBounds, node, displayOffset); if (node.Visible && y >= r.Y && y <= r.Bottom) { retNode = node; break; } else if (NodeDisplay.HasColumnsVisible(node)) { Rectangle headerBounds = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.ColumnsBounds, node, displayOffset); if (y >= headerBounds.Y && y <= headerBounds.Bottom) { info.ColumnsAt = node.NodesColumns; return info; } } if (isUserInterfaceHitTest && (r.Bottom + 16 < y || r.Y + 16 > y)) break; r = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.ChildNodeBounds, node, displayOffset); if (!(y >= r.Y && y <= r.Bottom)) node = GetNextVisibleSibling(node); else node = node.NextVisibleNode; } info.NodeAt = retNode; return info; }
/// <summary> /// Retrieves the tree node that is at the specified location. /// </summary> /// <returns>The TreeNode at the specified location, in tree view coordinates.</returns> /// <remarks> /// <para>You can pass the MouseEventArgs.X and MouseEventArgs.Y coordinates of the /// MouseDown event as the x and y parameters.</para> /// </remarks> /// <param name="tree">Tree control to find node at.</param> /// <param name="x">The X position to evaluate and retrieve the node from.</param> /// <param name="y">The Y position to evaluate and retrieve the node from.</param> /// <param name="paintedOnly">Enumerates rendered nodes only.</param> public static NodeHitTestInfo GetNodeAt(AdvTree tree, int x, int y, bool paintedOnly, bool isUserInterfaceHitTest) { NodeHitTestInfo info = new NodeHitTestInfo(); if (tree.Nodes.Count == 0 || !tree.DisplayRectangle.Contains(x, y) && tree.Zoom == 1) return info; Node node=tree.Nodes[0]; Node retNode=null; bool displayRootSet = false; if (tree.DisplayRootNode != null) { node = tree.DisplayRootNode; displayRootSet = true; } Point displayOffset = tree.NodeDisplay.Offset; // Enumerate painted nodes first if(tree.DisplayRectangle.Contains(x, y)) { ArrayList paintedNodes = tree.NodeDisplay.PaintedNodes; foreach (Node paintedNode in paintedNodes) { if (!paintedNode.IsDisplayed) continue; Rectangle r = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.NodeBounds, paintedNode, displayOffset); if (r.Contains(x, y)) { info.NodeAt = paintedNode; return info; } else if (NodeDisplay.HasColumnsVisible(paintedNode)) { Rectangle headerBounds = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.ColumnsBounds, paintedNode, displayOffset); if (headerBounds.Contains(x, y)) { info.ColumnsAt = paintedNode.NodesColumns; return info; } } } } if (paintedOnly) return info; while (node != null) { Rectangle r = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.NodeBounds, node, displayOffset); if (r.Contains(x, y)) { retNode = node; break; } else if (NodeDisplay.HasColumnsVisible(node)) { Rectangle headerBounds = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.ColumnsBounds, node, displayOffset); if (headerBounds.Contains(x, y)) { info.ColumnsAt = node.NodesColumns; return info; } } if (r.Y > y) break; r = NodeDisplay.GetNodeRectangle(eNodeRectanglePart.ChildNodeBounds, node, displayOffset); if (!r.Contains(x, y)) node = GetNextVisibleSibling(node); else node = node.NextVisibleNode; if (displayRootSet && !IsChildNode(tree.DisplayRootNode, node)) break; } info.NodeAt = retNode; return info; }