多种颜色在C#.NET标签多种、颜色、标签、NET

2023-09-02 10:22:13 作者:笑若扶风

我正在寻找一种方法来在一个C#/。NET的标签显示多种颜色。例如标签显示的是一系列的CSV分隔值,每个取这取决于它们落入水桶的颜色。我想preFER不使用多个标签,因为值是变长,我不想与动态布局玩。有没有原生支持呢?

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中没有原生控件做到这一点。最好的办法是写自己的用户控件(称之为RainbowLabel或东西)。通常你会直接有一个自定义标签控件继承标签,但由于无法获得多种颜色的文字在一个标签,你只从用户控件继承。

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.

有关渲染的文字,你的用户控件可以分割文字上的逗号,然后动态地加载每个块不同颜色的标签。一个更好的办法,但是,这是使用束带和MeasureString方法在Graphics命名空间中直接呈现文本到您的用户控件。

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编写用户控件是真的不困难,而这种不寻常的问题是什么自定义用户控件是。

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.

这应该是直接修改此code的用户控件。

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.