F
Fre
Hi all,
I'm working on this for some time now, but it keeps giving me
headaches. I'm drawing strings in a label and in a
DataGridViewTextBoxCell. Why? Subparts of the string need other
colors. It's working, but not as should be. I'm not able to fully
understand how drawing works with TextFormatFlags and StringFormat.
I've the feeling I tried all possible combinations of flag settings,
but it's not working as should be (which is well formatted strings and
backgrounds, well formed spacing...). This is a complete example that
draws a string in a label (to test, create a project with a Form
(Form1) and a Label (label1). As you can see after testing the code,
the result is buggy. How does MS draws the strings?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestDrawing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new Size(600, 80);
label1.Location = new Point(5, 5);
label1.Size = new Size(585, 21);
}
private void label1_Paint(object sender, PaintEventArgs e)
{
Rectangle rec = e.ClipRectangle;
Graphics g = e.Graphics;
Font f = new Font("Courier New", 10, FontStyle.Regular);
string[] writeThis = { "This drawing ",
"gives",
" me ",
"a",
" serious ",
"headache",
"! Why",
" isn't it ",
"w",
"orkin",
"g?"
};
// text format flags
TextFormatFlags flags = TextFormatFlags.Left;
flags |= TextFormatFlags.NoPadding;
flags |= TextFormatFlags.PreserveGraphicsClipping;
flags |= TextFormatFlags.NoPrefix;
flags |= TextFormatFlags.GlyphOverhangPadding;
// more text layout
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.None;
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
sf.FormatFlags |= StringFormatFlags.NoWrap;
sf.FormatFlags |= StringFormatFlags.NoFontFallback;
bool b = false;
Size propsize = new Size(int.MaxValue, int.MaxValue);
foreach (string s in writeThis)
{
Size size = TextRenderer.MeasureText(g, s, f, propsize,
flags);
rec.Width = size.Width;
// fill rectangle and draw string (toggle colors)
b = !b;
g.FillRectangle(b ? Brushes.Indigo : Brushes.LightGreen, rec);
g.DrawString(s, f, b ? Brushes.Wheat : Brushes.Black, rec,
sf);
// update the start position of our rectangle
rec.X = rec.X + size.Width;
}
}
}
}
Thanks in advance for your feedback.
Frederik
I'm working on this for some time now, but it keeps giving me
headaches. I'm drawing strings in a label and in a
DataGridViewTextBoxCell. Why? Subparts of the string need other
colors. It's working, but not as should be. I'm not able to fully
understand how drawing works with TextFormatFlags and StringFormat.
I've the feeling I tried all possible combinations of flag settings,
but it's not working as should be (which is well formatted strings and
backgrounds, well formed spacing...). This is a complete example that
draws a string in a label (to test, create a project with a Form
(Form1) and a Label (label1). As you can see after testing the code,
the result is buggy. How does MS draws the strings?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestDrawing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new Size(600, 80);
label1.Location = new Point(5, 5);
label1.Size = new Size(585, 21);
}
private void label1_Paint(object sender, PaintEventArgs e)
{
Rectangle rec = e.ClipRectangle;
Graphics g = e.Graphics;
Font f = new Font("Courier New", 10, FontStyle.Regular);
string[] writeThis = { "This drawing ",
"gives",
" me ",
"a",
" serious ",
"headache",
"! Why",
" isn't it ",
"w",
"orkin",
"g?"
};
// text format flags
TextFormatFlags flags = TextFormatFlags.Left;
flags |= TextFormatFlags.NoPadding;
flags |= TextFormatFlags.PreserveGraphicsClipping;
flags |= TextFormatFlags.NoPrefix;
flags |= TextFormatFlags.GlyphOverhangPadding;
// more text layout
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.None;
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
sf.FormatFlags |= StringFormatFlags.NoWrap;
sf.FormatFlags |= StringFormatFlags.NoFontFallback;
bool b = false;
Size propsize = new Size(int.MaxValue, int.MaxValue);
foreach (string s in writeThis)
{
Size size = TextRenderer.MeasureText(g, s, f, propsize,
flags);
rec.Width = size.Width;
// fill rectangle and draw string (toggle colors)
b = !b;
g.FillRectangle(b ? Brushes.Indigo : Brushes.LightGreen, rec);
g.DrawString(s, f, b ? Brushes.Wheat : Brushes.Black, rec,
sf);
// update the start position of our rectangle
rec.X = rec.X + size.Width;
}
}
}
}
Thanks in advance for your feedback.
Frederik