代码之家  ›  专栏  ›  技术社区  ›  xsl Fredrik Hedblad

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

  •  0
  • xsl Fredrik Hedblad  · 技术社区  · 14 年前

    我正在寻找一种使用.NET Compact Framework突出显示文本标签中多个字符的方法。E、 g.在包含文本的标签中 Hello World ,我想要 H 以及 r

    小时 埃洛沃 ld

    & 给目标字符加下划线,但不幸的是,它只会给一个字符加下划线。我不在乎这些字符是用不同的颜色,粗体的还是下划线的,唯一重要的是它们很显眼。

    1 回复  |  直到 13 年前
        1
  •  0
  •   xsl Fredrik Hedblad    14 年前

    预览:

    Preview

    更新:

    代码:

    在.NETCompactFramework3.5、WindowsMobile6SDK上测试过,可能也可以在.NETFramework上工作。

    /// <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