突出显示文本标签中的多个字符

发布于 2024-09-30 17:35:01 字数 308 浏览 6 评论 0原文

我正在寻找一种使用 .NET Compact Framework 突出显示文本标签中的多个字符的方法。例如,在包含文本 Hello World 的标签中,我希望突出显示 Hr,如下例所示:

你好世界rld

我最初的解决方案是滥用 & 为目标字符添加下划线,但不幸的是它只会为一个字符添加下划线。我不在乎角色是否采用不同的颜色、粗体或下划线,唯一重要的是它们脱颖而出。

I'm searching for a way to highlight multiple characters in a text label using the .NET Compact Framework. E.g. in a label which contains the text Hello World, i want the H and the r highlighted like in this example:

Hello World

My initial solution was to misuse & to underline the target characters, but unfortunately it will only underline one character. I don't care if the characters are in a different color, bold or underlined, the only important thing is that they stand out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2024-10-07 17:35:01

预览:

预览

更新:

添加了颜色突出显示支持。

代码

在 .NET Compact Framework 3.5、Windows Mobile 6 SDK 上进行测试,可能也适用于 .NET 框架。

/// <summary>
/// A label which offers you the possibility to highlight characters 
/// at defined positions.
/// See <see cref="HighlightPositions"/>, <see cref="HighlightStyle"/> and
/// <see cref="HighlightColor"/>
/// The text in the Text property will be displayed.
/// </summary>
public partial class HighlightLabel : Control
{
    /// <summary>
    /// Initializes a new instance of the class.
    /// </summary>
    public HighlightLabel()
    {
        InitializeComponent();
    }
    /// <summary>
    /// An array of all positions in the text to be highlighted.
    /// </summary>
    public int[] HighlightPositions { get; set; }

    /// <summary>
    /// Gets or sets the highlight style.
    /// </summary>
    public FontStyle HighlightStyle { get; set; }

    /// <summary>
    /// Gets or sets the highlight color.
    /// </summary>
    public Color HighlightColor { get; set; }

    // Paints the string and applies the highlighting style.
    protected override void OnPaint(PaintEventArgs e)
    {
        if (HighlightPositions == null)
            HighlightPositions = new int[] { };

        var usedOffsets = new List<float>();

        for (var i = 0; i < Text.Length; i++)
        {
            var characterToPaint =
                Text[i].ToString(CultureInfo.CurrentCulture);

            var selectedFont = Font;
            var selectedColor = ForeColor;

            if (HighlightPositions.Contains(i))
            {
                selectedColor = HighlightColor;
                selectedFont = new Font(Font.Name, Font.Size, 
                    HighlightStyle);
            }

            var currentOffset = usedOffsets.Sum();

            e.Graphics.DrawString(characterToPaint, selectedFont,
                new SolidBrush(selectedColor),
                new RectangleF(e.ClipRectangle.X + currentOffset,
                    e.ClipRectangle.Y, e.ClipRectangle.Width,
                    e.ClipRectangle.Height));

            var offset = e.Graphics.MeasureString(characterToPaint,
                selectedFont).Width;

            usedOffsets.Add(offset);
        }
    }
}

用法:

highlightLabel.HighlightPositions = new[] { 1, 8 };
highlightLabel.HighlightStyle = FontStyle.Bold;
highlightLabel.HighlightColor = Color.Red

Preview:

Preview

Updates:

Added color highlighting support.

Code:

Tested on .NET Compact Framework 3.5, Windows Mobile 6 SDK, should probably work on the .NET framework too.

/// <summary>
/// A label which offers you the possibility to highlight characters 
/// at defined positions.
/// See <see cref="HighlightPositions"/>, <see cref="HighlightStyle"/> and
/// <see cref="HighlightColor"/>
/// The text in the Text property will be displayed.
/// </summary>
public partial class HighlightLabel : Control
{
    /// <summary>
    /// Initializes a new instance of the class.
    /// </summary>
    public HighlightLabel()
    {
        InitializeComponent();
    }
    /// <summary>
    /// An array of all positions in the text to be highlighted.
    /// </summary>
    public int[] HighlightPositions { get; set; }

    /// <summary>
    /// Gets or sets the highlight style.
    /// </summary>
    public FontStyle HighlightStyle { get; set; }

    /// <summary>
    /// Gets or sets the highlight color.
    /// </summary>
    public Color HighlightColor { get; set; }

    // Paints the string and applies the highlighting style.
    protected override void OnPaint(PaintEventArgs e)
    {
        if (HighlightPositions == null)
            HighlightPositions = new int[] { };

        var usedOffsets = new List<float>();

        for (var i = 0; i < Text.Length; i++)
        {
            var characterToPaint =
                Text[i].ToString(CultureInfo.CurrentCulture);

            var selectedFont = Font;
            var selectedColor = ForeColor;

            if (HighlightPositions.Contains(i))
            {
                selectedColor = HighlightColor;
                selectedFont = new Font(Font.Name, Font.Size, 
                    HighlightStyle);
            }

            var currentOffset = usedOffsets.Sum();

            e.Graphics.DrawString(characterToPaint, selectedFont,
                new SolidBrush(selectedColor),
                new RectangleF(e.ClipRectangle.X + currentOffset,
                    e.ClipRectangle.Y, e.ClipRectangle.Width,
                    e.ClipRectangle.Height));

            var offset = e.Graphics.MeasureString(characterToPaint,
                selectedFont).Width;

            usedOffsets.Add(offset);
        }
    }
}

Usage:

highlightLabel.HighlightPositions = new[] { 1, 8 };
highlightLabel.HighlightStyle = FontStyle.Bold;
highlightLabel.HighlightColor = Color.Red
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文