Treenode text different colored words

别等时光非礼了梦想. 提交于 2019-11-29 12:45:30

Try this:

    private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        string[] texts = e.Node.Text.Split();
        using (Font font = new Font(this.Font, FontStyle.Regular))
        {
            using (Brush brush = new SolidBrush(Color.Red))
            {
                e.Graphics.DrawString(texts[0], font, brush, e.Bounds.Left, e.Bounds.Top);
            }

            using (Brush brush = new SolidBrush(Color.Blue))
            {
                SizeF s = e.Graphics.MeasureString(texts[0], font);
                e.Graphics.DrawString(texts[1], font, brush, e.Bounds.Left + (int)s.Width, e.Bounds.Top);
            }
        }
    }

You must manage State of node to do appropriated actions.

UPDATE

Sorry, my mistake see updated version. There is no necessary to measure space size because it already contains in texts[0].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!