0
我想在树节点的一部分但不通过用户(不使用“选定的节点”) 因此,DrawMode不帮助我。颜色树节点与2种颜色,但不是通过用户
我使用C#
比如我想要的文字与空间中的所有树节点将在绿色的一侧和另一侧的红色。
谢谢!
我想在树节点的一部分但不通过用户(不使用“选定的节点”) 因此,DrawMode不帮助我。颜色树节点与2种颜色,但不是通过用户
我使用C#
比如我想要的文字与空间中的所有树节点将在绿色的一侧和另一侧的红色。
谢谢!
DrawMode是要走的路。您必须将其设置为OwnerDrawText,并订阅DrawNode事件。 I .: .:
this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText;
this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode);
这只是如何绘制方法的样本。您需要修改它以获得良好的图形效果,但它可以让您了解如何去做。
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
string txt = e.Node.Text;
int idx = txt.IndexOf(' ');
string greenTxt;
string redTxt;
if (idx >= 0)
{
greenTxt = txt.Substring(0, idx);
redTxt = txt.Substring(idx);
}
else
{
greenTxt = txt;
redTxt = string.Empty;
}
Rectangle greenRect = new Rectangle(e.Bounds.Location, new Size((int)Math.Ceiling(e.Graphics.MeasureString(greenTxt, nodeFont).Width), e.Bounds.Height));
Rectangle redRect = new Rectangle(e.Bounds.Location + new Size(greenRect.Width, 0), new Size((int)Math.Ceiling(e.Graphics.MeasureString(redTxt, nodeFont).Width), e.Bounds.Height));
e.Graphics.DrawString(greenTxt, nodeFont, Brushes.Green, greenRect);
if (!string.IsNullOrEmpty(redTxt))
e.Graphics.DrawString(redTxt, nodeFont,
Brushes.Red, redRect);
}
你可以找到一个更复杂的例子here。
嗨!谢谢!!!它对我有很大的帮助 – 2013-02-11 12:15:54
我的代码有问题 – 2013-02-11 12:29:25
我的代码有问题,当我展开树时,我在树的顶部注册了所有的扩展对象另一个,然后我滚动它消失 你说什么? – 2013-02-11 12:32:33