2010-11-04 4 views
0

Je cherche un moyen de mettre en évidence plusieurs caractères dans une étiquette de texte en utilisant le .NET Compact Framework. Par exemple. dans une étiquette contenant le texte Hello World, je veux le H et le r mis en évidence comme dans cet exemple:Surligner plusieurs caractères dans une étiquette de texte

H ello Wo r ld

Ma solution initiale était à une mauvaise utilisation & pour souligner les caractères cibles, mais malheureusement, il ne fera que souligner un caractère. Peu m'importe si les personnages sont de couleur différente, gras ou souligné, la seule chose importante est qu'ils se démarquent.

Répondre

0

Preview:

Preview

Mises à jour:

du surlignage couleur ajoutée.

code:

Testé sur NET Compact Framework 3.5, Windows Mobile 6 SDK, devrait probablement travailler sur le framework .NET aussi.

/// <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); 
     } 
    } 
} 

Utilisation:

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