Treenode text different colored words

后端 未结 1 1040
灰色年华
灰色年华 2020-12-18 13:38

I have a TreeView and each of it\'s Node.Text has two words. The first and second words should have different colors. I\'m already changing the col

相关标签:
1条回答
  • 2020-12-18 14:25

    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].

    0 讨论(0)
提交回复
热议问题