C# .NET 标签中的多种颜色 [英] Multiple colors in a C# .NET label

查看:34
本文介绍了C# .NET 标签中的多种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在单个 C#/.NET 标签中显示多种颜色的方法.例如,标签显示一系列 csv 分隔值,每个值根据它们落入的桶呈现颜色.我不想使用多个标签,因为这些值是可变长度的,我不想使用动态布局.是否有本地支持?

I'm looking for a way to display multiple colors in a single C#/.NET label. E.g the label is displaying a series of csv separated values that each take on a color depending on a bucket they fall into. I would prefer not to use multiple labels, as the values are variable length and I don't want to play with dynamic layouts. Is there a native support for this?

推荐答案

.NET 中没有执行此操作的本机控件.最好的办法是编写自己的 UserControl(称为 RainbowLabel 或其他名称).通常,您会直接从 Label 继承自定义标签控件,但由于您无法在一个标签中获得多色文本,因此您只需从 UserControl 继承即可.

There is no native control in .NET that does this. Your best bet is to write your own UserControl (call it RainbowLabel or something). Normally you would have a custom label control inherit directly from Label, but since you can't get multi-colored text in one label, you would just inherit from UserControl.

为了呈现文本,您的 UserControl 可以用逗号分割文本,然后为每个块动态加载不同颜色的标签.但是,更好的方法是使用 Graphics 命名空间中的 DrawString 和 MeasureString 方法将文本直接呈现到您的 UserControl 上.

For rendering the text, your UserControl could split the text on commas and then dynamically load a differently-colored Label for each chunk. A better way, however, would be to render the text directly onto your UserControl using the DrawString and MeasureString methods in the Graphics namespace.

在.NET中编写UserControls真的不难,这种异常的问题正是自定义UserControls的用途.

Writing UserControls in .NET is really not difficult, and this kind of unusual problem is exactly what custom UserControls are for.

更新:这里有一个简单的方法,可用于在图片框上呈现多色文本:

Update: here's a simple method you can use for rendering the multi-colored text on a PictureBox:

public void RenderRainbowText(string Text, PictureBox pb)
{
    // PictureBox needs an image to draw on
    pb.Image = new Bitmap(pb.Width, pb.Height);
    using (Graphics g = Graphics.FromImage(pb.Image))
    {
        // create all-white background for drawing
        SolidBrush brush = new SolidBrush(Color.White);
        g.FillRectangle(brush, 0, 0,
            pb.Image.Width, pb.Image.Height);
        // draw comma-delimited elements in multiple colors
        string[] chunks = Text.Split(',');
        brush = new SolidBrush(Color.Black);
        SolidBrush[] brushes = new SolidBrush[] { 
            new SolidBrush(Color.Red),
            new SolidBrush(Color.Green),
            new SolidBrush(Color.Blue),
            new SolidBrush(Color.Purple) };
        float x = 0;
        for (int i = 0; i < chunks.Length; i++)
        {
            // draw text in whatever color
            g.DrawString(chunks[i], pb.Font, brushes[i], x, 0);
            // measure text and advance x
            x += (g.MeasureString(chunks[i], pb.Font)).Width;
            // draw the comma back in, in black
            if (i < (chunks.Length - 1))
            {
                g.DrawString(",", pb.Font, brush, x, 0);
                x += (g.MeasureString(",", pb.Font)).Width;
            }
        }
    }
}

显然,如果您的文本中有超过 4 个逗号分隔的元素,这会中断,但您明白了.此外,MeasureString 中似乎存在一个小故障,使其返回的宽度比所需的宽度宽几个像素,因此多色字符串看起来被拉长了 - 您可能需要调整该部分.

Obviously this will break if you have more than 4 comma-delimited elements in your text, but you get the idea. Also, there appears to be a small glitch in MeasureString that makes it return a width that is a couple pixels wider than necessary, so the multi-colored string appears stretched out - you might want to tweak that part.

修改 UserControl 的代码应该很简单.

It should be straightforward to modify this code for a UserControl.

注意:TextRenderer 是用于绘制和测量字符串的更好类,因为它使用整数.Graphics.DrawString 和 .MeasureString 使用浮点数,因此您会在这里和那里出现逐个像素的错误.

Note: TextRenderer is a better class to use for drawing and measuring strings, since it uses ints. Graphics.DrawString and .MeasureString use floats, so you'll get off-by-a-pixel errors here and there.

更新:忘记关于使用 TextRenderer.狗慢.

Update: Forget about using TextRenderer. It is dog slow.

这篇关于C# .NET 标签中的多种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆