唐納德的評論中的Link指向dar7yl的出色答案讓它恰到好處。
所有的榮譽dar7yl!下面就是一個例子,其結果,很好地排列在同一行:
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics G = panel2.CreateGraphics())
{
fonts.Add(new DrawFont(G, new FontFamily("Arial"), 7f));
fonts.Add(new DrawFont(G, new FontFamily("Arial"), 12f));
fonts.Add(new DrawFont(G, new FontFamily("Arial"), 17f));
fonts.Add(new DrawFont(G, new FontFamily("Consolas"), 8f));
fonts.Add(new DrawFont(G, new FontFamily("Consolas"), 10f));
fonts.Add(new DrawFont(G, new FontFamily("Consolas"), 14f));
fonts.Add(new DrawFont(G, new FontFamily("Times"), 9f));
fonts.Add(new DrawFont(G, new FontFamily("Times"), 12f));
fonts.Add(new DrawFont(G, new FontFamily("Times"), 20f));
fonts.Add(new DrawFont(G, new FontFamily("Segoe Print"), 6f));
fonts.Add(new DrawFont(G, new FontFamily("Segoe Print"), 12f));
fonts.Add(new DrawFont(G, new FontFamily("Segoe Print"), 24f));
}
}
List<DrawFont> fonts = new List<DrawFont>();
class DrawFont
{
public Font Font { get; set; }
public float baseLine { get; set; }
public DrawFont(Graphics G, FontFamily FF, float height, FontStyle style)
{
Font = new Font(FF, height, style);
float lineSpace = FF.GetLineSpacing(Font.Style);
float ascent = FF.GetCellAscent(Font.Style);
baseLine = Font.GetHeight(G) * ascent/lineSpace;
}
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
float x = 5f;
foreach (DrawFont font in fonts)
{
e.Graphics.DrawString("Fy", font.Font, Brushes.DarkSlateBlue, x, 80 - font.baseLine);
x += 50;
}
e.Graphics.DrawLine(Pens.LightSlateGray, 0, 80, 999, 80);
}
我用Graphics.DrawString
,但TextRenderer
應該工作一樣好..
這可能是有用的,如果你張貼了你看到的圖像,和你想要的圖像。通常,如果您對齊到「底部」,則所有文本的底部(不論大小)是否與字母底部對齊(不包括下降) – dodald
另外,您可能會發現此問題的答案很有用:http:// stackoverflow.com/questions/1006069/how-do-i-get-the-position-of-the-text-baseline-in-a-label-and-a-numericupdown – dodald