我在寻找一种方式来显示多种颜色在一个单一的C#/.净的标签。E.g标是显示一系列的csv分离的价值,每采取一个颜色取决于一桶他们落入。我宁愿不要使用多个标签,作为价值观都是可变的长度,我不想要玩具动态的布局。是有一个地支持这个吗?

有帮助吗?

解决方案

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

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

在.NET中编写UserControl并不困难,这种不寻常的问题正是自定义UserControls的用途。

更新:这是一个简单的方法,您可以使用它来渲染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中似乎存在一个小故障,使其返回的宽度比所需的宽几个像素宽,因此多色字符串显示为拉伸 - 您可能想要调整该部分。

为UserControl修改此代码应该很简单。

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

更新忘记使用TextRenderer。这是狗慢。

其他提示

您可以尝试使用RichTextBox,以便您可以为字符串获取多种颜色,然后将其设置为只读并删除边框。将背景颜色更改为与其所在的表格相同,您可能会使用它。

作为替代方法,您可以在合适的控件(例如WebBrowser)中将其作为rtf或html执行。它可能需要更多你想要的资源,但它会很快起作用。

如果要为XP及以上版本的人构建Windows应用程序,可以使用WPF。即使它是Windows窗体应用程序,您也可以添加WPF UserControl。

然后我会使用Label,并设置“Foreground”和“Foreground”。属性是一种渐变的颜色。

或者,在Windows窗体(没有WPF)中,您可以只使用“流面板”,然后在for循环中添加多个标签作为您的句子的片段......它们将全部“流动”。好像是一个标签一样。

稍微偏离话题...你可以检查:

我经常使用彩色标签来标记红色等关键字。 就像Phil Wright的回答一样,我使用RichTextBox控件,删除边框并将背景颜色设置为SystemColors.Control。

要编写彩色文本,首先清除控件,然后使用此功能附加彩色文本:

private void rtb_AppendText(Font selfont, Color color, Color bcolor, 
                        string text, RichTextBox box)
    {
            // append the text to the RichTextBox control
            int start = box.TextLength;
            box.AppendText(text);
            int end = box.TextLength;

            // select the new text
            box.Select(start, end - start);
            // set the attributes of the new text
            box.SelectionColor = color;
            box.SelectionFont = selfont;
            box.SelectionBackColor = bcolor;
            // unselect
            box.Select(end, 0);

            // only required for multi line text to scroll to the end
            box.ScrollToCaret();
    }

如果您想使用“mono”运行此功能然后在每个新的彩色文本之前添加一个空格,或者mono不会正确设置新的颜色。这不是.NET

所必需的

用法:

myRtb.Text = "";
rtb_AppendText(new Font("Courier New", (float)10), 
                   Color.Red, SystemColors.Control, " my red text", myRtb);
rtb_AppendText(new Font("Courier New", (float)10), 
                   Color.Blue, SystemColors.Control, " followed by blue", myRtb);

您可以简单地使用多个标签。设置所需的字体属性,然后使用左侧。顶部和宽度属性,用于显示要以不同方式显示的单词。这假设您使用的是Windows窗体。

试试这个,

   labelId.Text = "Successfully sent to" + "<a style='color:Blue'> "  + name + "</a>"; 

没有原生支持;您将需要使用多个标签或找到将提供此功能的第三方控件。

我不这么认为。你应该自己创建一个。

根据您的问题,您的要求很简单 lable.Text =&quot;这个颜色是红色的,所以它必须显示这样的文字 “颜色是”将是蓝色和“红色”将是红色.. 这可以像这样完成

lable.Text = "<span style='Color:Blue'>" + " The color is " +"</span>" + "<span style='Color:Red'>"Red"</span>"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top