Multiple font colors in one cell
What is the best solution, in terms of performance, for allowing the font text in a cell to have multiple colors, if possible?
0
-
Hi,
You should implement Grid.PaintCell event handler. An example:
grid.PaintCell += delegate(object sender, PaintCellEventArgs e)
{
//Do default drawing without text
e.Parts &= e.Parts ^ PaintPart.Text;
e.PaintAll();
e.Handled = true;
if(!string.IsNullOrEmpty(e.Text))
{
SolidBrush[] brushes = new SolidBrush[] {
new SolidBrush(Color.Red),
new SolidBrush(Color.Green),
new SolidBrush(Color.Blue),
new SolidBrush(Color.Purple) };
float x = 0;
string text = e.Text;
for (int i = 0; i < text.Length; i++)
{
// draw text in whatever color
string character = text[i].ToString();
e.Graphics.DrawString(character,
e.Font,
brushes[i % brushes.Length],
e.Cell.VirtualBounds.X + x,
e.Cell.VirtualBounds.Y);
// measure text and advance x
x += (e.Graphics.MeasureString(character, e.Font)).Width;
}
}
};Best regards,
Dapfor
MultipleFontColorsInCell.png0 -
Thank you.
0
Please sign in to leave a comment.
Comments
2 comments