怎么样:
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Font normal = new Font("Tahoma", 10, FontStyle.Regular))
using (Font bold = new Font("Tahoma", 10, FontStyle.Bold))
using (StringFormat format =
(StringFormat)StringFormat.GenericTypographic.Clone())
{
format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
Rectangle temp = ClientRectangle;
DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "AB", normal);
DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "CDE ", bold);
DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "FG", normal);
}
}
void DrawString(Graphics g, Brush brush, ref Rectangle rect, StringFormat format, string text, Font font)
{
using (StringFormat copy = (StringFormat)format.Clone())
{
copy.SetMeasurableCharacterRanges(new CharacterRange[] {
new CharacterRange(0, text.Length)});
Region[] regions = g.MeasureCharacterRanges(text, font, rect, copy);
g.DrawString(text, font, brush, rect, format);
int width = (int)(regions[0].GetBounds(g).Width);
rect.X += width;
rect.Width -= width;
}
}
从这里获得。