树视图控制多个颜色的节点文本 [英] tree view controll node text in multiple color
本文介绍了树视图控制多个颜色的节点文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
i使用下面的代码绘制我的树节点
i想要两个画它对于所有子子节点。
一步一步,即如果Drawing是一个孩子的complet,而不仅仅是为了下一个孩子的drw,请帮助我。
hi
i am using the below code for drawing my tree node
i want two draw it for all sub child node .
step by step i.e if Drawing is complet for one child than only it drw for next child please help me on this.
private void treeViewExec_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if ((e.Node == treeViewExec.Nodes[0].Nodes[0]))
{
using (Font font = new Font(this.Font, FontStyle.Regular))
{
using (Brush brush = new SolidBrush(Color.Black))
{
if (m_strParentNodeText == "")
{
e.Graphics.DrawString(treeViewExec.Nodes[0].Nodes[0].Text + "-(", font, brush, e.Bounds.Left, e.Bounds.Top);
}
else
{
e.Graphics.DrawString(m_strParentNodeText + "-(", font, brush, e.Bounds.Left, e.Bounds.Top);
}
}
using (Brush brush = new SolidBrush(Color.Green))
{
e.Graphics.DrawString(m_nTestcasePass.ToString(), font, brush, e.Bounds.Left + 35, e.Bounds.Top);
}
using (Brush brush = new SolidBrush(Color.Black))
{
e.Graphics.DrawString(")", font, brush, e.Bounds.Left + 49, e.Bounds.Top);
}
using (Brush brush = new SolidBrush(Color.Black))
{
e.Graphics.DrawString(",", font, brush, e.Bounds.Left + 40, e.Bounds.Top);
}
using (Brush brush = new SolidBrush(Color.Red))
{
SizeF s = e.Graphics.MeasureString(m_nTestcasePass.ToString(), font);
e.Graphics.DrawString(m_nTestcaseFailed.ToString(), font, brush, e.Bounds.Left + 35 + (int)s.Width, e.Bounds.Top);
}
}
}
else
{
e.DrawDefault = true;
}
}
推荐答案
有一个尝试下面的代码,似乎工作,但缺少第一个节点的树线,我不知道为什么。
Hi,
have a try on the code below, it seems working but missing the tree line of the first node, I am not sure why.
private void treeViewExec_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (e.Node == treeViewExec.Nodes[0].Nodes[0])
{
TreeNode node = treeViewExec.Nodes[0].Nodes[0];
do
{
int leftBound = node.Bounds.Left ;
string str = "";
if (m_strParentNodeText == "")
{
str = node.Text + "-(";
}
else
{
str = m_strParentNodeText + "-(";
}
using (Font font = new Font(this.Font, FontStyle.Regular))
{
DoDrawing(str, e.Graphics, font, Color.Black, leftBound, node.Bounds.Top);
leftBound = CalculateLeftBoundry(str, e.Graphics, leftBound, font);
DoDrawing(m_nTestcasePass.ToString(), e.Graphics, font, Color.Green, leftBound, node.Bounds.Top);
leftBound = CalculateLeftBoundry(m_nTestcasePass.ToString(), e.Graphics, leftBound, font);
DoDrawing(")", e.Graphics, font, Color.Black, leftBound, node.Bounds.Top);
leftBound = CalculateLeftBoundry(")", e.Graphics, leftBound, font);
DoDrawing(",", e.Graphics, font, Color.Black, leftBound, node.Bounds.Top);
leftBound = CalculateLeftBoundry(",", e.Graphics, leftBound, font);
DoDrawing(m_nTestcaseFailed.ToString(), e.Graphics, font, Color.Red, leftBound, node.Bounds.Top);
}
} while ((node = node.NextNode) != null);
}
else
{
e.DrawDefault = true;
}
}
private int CalculateLeftBoundry(string text, Graphics graphics, int currentLeftBound, Font font)
{
SizeF s = graphics.MeasureString(text, font);
return (currentLeftBound + (int)s.Width);
}
private void DoDrawing(string text, Graphics graphics, Font font, Color color, int leftBoundry, int topBoundry)
{
using (Brush brush = new SolidBrush(color))
{
graphics.DrawString(text, font, brush, leftBoundry, topBoundry);
}
}
它似乎以您想要的颜色显示所有子节点。我也重构了你的代码,使其更具可读性。
问候
Jegan
It seems to display all child nodes in the colour you wanted. Also I have refactored your code to make more readable.
Regards
Jegan
这篇关于树视图控制多个颜色的节点文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文