【发布时间】:2011-10-22 15:36:22
【问题描述】:
所以有一个很棒的开源tutorial on creating TreeNode class。太好了。但是我想知道如何改变它的 Arrange 函数来让它绘制indented tree?
函数如下:
// Arrange the node and its children in the allowed area.
// Set xmin to indicate the right edge of our subtree.
// Set ymin to indicate the bottom edge of our subtree.
public void Arrange(Graphics gr, ref float xmin, ref float ymin)
{
// See how big this node is.
SizeF my_size = Data.GetSize(gr, MyFont);
// Recursively arrange our children,
// allowing room for this node.
float x = xmin;
float biggest_ymin = ymin + my_size.Height;
float subtree_ymin = ymin + my_size.Height + Voffset;
foreach (TreeNode<T> child in Children)
{
// Arrange this child's subtree.
float child_ymin = subtree_ymin;
child.Arrange(gr, ref x, ref child_ymin);
// See if this increases the biggest ymin value.
if (biggest_ymin < child_ymin) biggest_ymin = child_ymin;
// Allow room before the next sibling.
x += Hoffset;
}
// Remove the spacing after the last child.
if (Children.Count > 0) x -= Hoffset;
// See if this node is wider than the subtree under it.
float subtree_width = x - xmin;
if (my_size.Width > subtree_width)
{
// Center the subtree under this node.
// Make the children rearrange themselves
// moved to center their subtrees.
x = xmin + (my_size.Width - subtree_width) / 2;
foreach (TreeNode<T> child in Children)
{
// Arrange this child's subtree.
child.Arrange(gr, ref x, ref subtree_ymin);
// Allow room before the next sibling.
x += Hoffset;
}
// The subtree's width is this node's width.
subtree_width = my_size.Width;
}
// Set this node's center position.
Center = new PointF(
xmin + subtree_width / 2,
ymin + my_size.Height / 2);
// Increase xmin to allow room for
// the subtree before returning.
xmin += subtree_width;
// Set the return value for ymin.
ymin = biggest_ymin;
}
现在的样子:
缩进树的样子(图片基于DmitryG s grate answer):
那么..如何让它以缩进形式绘制图形?
【问题讨论】:
-
为什么不使用内置的TreeView 来可视化您的树?
-
它的主要观点 owner-drawn=)
-
@Kabumbus
its main point of owner-drawn这是作业吗?或者,您在寻找分包商吗?
标签: c# .net winforms treeview tree