可通过以下语句来设置文本的对齐方式:
StringFormat sF = new StringFormat()
sF.Alignment = StringAlignment.Far;
sF.LineAlignment = StringAlignment.Far;
代码:、
View Code
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Font f = new Font("Aris", 15, FontStyle.Italic);
string s = "this is my name";
StringFormat sF = new StringFormat();
sF.Alignment = StringAlignment.Far;
sF.LineAlignment = StringAlignment.Far;
SizeF sf = g.MeasureString(s, f,400);
RectangleF rf = new RectangleF(20, 20,200, sf.Height*6);
Console.WriteLine(sf.Width);
g.DrawRectangle(Pens.Red, rf.Left, rf.Top, rf.Width, rf.Height);
g.DrawString(s, f, Brushes.Black, rf, sF);
}
{
Graphics g = e.Graphics;
Font f = new Font("Aris", 15, FontStyle.Italic);
string s = "this is my name";
StringFormat sF = new StringFormat();
sF.Alignment = StringAlignment.Far;
sF.LineAlignment = StringAlignment.Far;
SizeF sf = g.MeasureString(s, f,400);
RectangleF rf = new RectangleF(20, 20,200, sf.Height*6);
Console.WriteLine(sf.Width);
g.DrawRectangle(Pens.Red, rf.Left, rf.Top, rf.Width, rf.Height);
g.DrawString(s, f, Brushes.Black, rf, sF);
}