如何像:
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;
}
}
從here獲得。
我猜想,這是非常多的情況下,但是textrenderer.drawtext函數的大拇指:) – 2012-07-27 18:06:41