I am implementing a solution for a cursor drag selection of nodes within a visible rectangle, just like we do in Windows Explorer with files and folders. I have the rectangle part working and have some slick method of getting the nodes on the edge of the rectangle. However, if my rectangle is dragged past the nodes I cannot see them.
I am pretty sure there is a way to determine if a control falls within the bounds of a rectangle. Is there a way to do this with the tree nodes? I can use GetNodeFromPoint but only when I have a specific point. Is there a GetNodeLocation and then compare that to the bounds of the rectangle?
Any points on this would be great. Even just how to determine if a specific location falls within the rectangle location. Thanks.
I figured it out. Took a little math and problem solving. I was hoping there was some kind of Rectangle.Compare function but unfortunately not. Here is the sub routine. Make sure the rectangle bounds you pass in are according to the ultratree control and not the screen coordinates.
Public Function NodeIsInBounds(ByVal n As UltraTreeNode, ByVal rec As Rectangle) As Boolean
'GET THE UI ELEMENT FOR THE NODE
'
Dim indent As Integer = 20 'ACCOUNTS FOR THE INDENTION OF THE NODES
If n.UIElement.Rect.X + indent >= rec.X AndAlso n.UIElement.Rect.Y <= rec.Y + rec.Height AndAlso n.UIElement.Rect.Y + n.UIElement.Rect.Height >= rec.Y AndAlso Not (n.UIElement.Rect.X + indent > rec.X + rec.Width) Then
Return True
End If
If n.UIElement.Rect.X + n.UIElement.Rect.Width >= rec.X AndAlso n.UIElement.Rect.Y <= rec.Y + rec.Height AndAlso n.UIElement.Rect.Y >= rec.Y AndAlso Not (n.UIElement.Rect.X + indent > rec.X + rec.Width) Then
If n.UIElement.Rect.X + n.UIElement.Rect.Width >= rec.X AndAlso n.UIElement.Rect.Y + n.UIElement.Rect.Height >= rec.Y AndAlso Not (n.UIElement.Rect.Y > rec.Y + rec.Height) AndAlso Not (n.UIElement.Rect.X + indent > rec.X + rec.Width) Then
Return False
End Function
I think the method you are looking for it Rectangle.Intersect.
Yes that is it! Thanks. That would have saved me a lot of work. Here is what the NodeIsInBounds Function should look like now. Much more simple: (Notice I have to get the NodeTextUIElement specifically because otherwise if you touch the expansion indicator it will select the node)
Try
If Not rc.IsEmpty Then
End Try