是这样子吗?
下面的示例在矩形中绘制文本。每行文本都居中,整个文本块(从上到下)在矩形内居中。
string text = "Use StringFormat and RectangleF objects to center text in a rectangle.";
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
12, FontStyle.Bold,
GraphicsUnit.Point);
Rectangle rect = new Rectangle(30, 10, 120, 140);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
// Center each line of text.
stringFormat.Alignment = StringAlignment.Center;
// Center the block of text (top to bottom) in the rectangle.
stringFormat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(text, font, solidBrush, rect, stringFormat);
Pen pen = Pens.Black;
e.Graphics.DrawRectangle(pen, rect);