【发布时间】:2010-12-16 11:25:44
【问题描述】:
我需要能够旋转标签中的文本并将其左对齐、右对齐或居中对齐。到目前为止,我可以在派生标签的 onPaint 方法中使用此代码进行旋转:
float width = graphics.MeasureString(Text, this.Font).Width;
float height = graphics.MeasureString(Text, this.Font).Height;
double angle = (_rotationAngle / 180) * Math.PI;
graphics.TranslateTransform(
(ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
(ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
graphics.RotateTransform(270f);
graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat);
graphics.ResetTransform();
而且效果很好。我可以看到文字旋转了 270 度。
但是当我尝试在 stringFormat 中设置对齐时,它变得疯狂,我无法弄清楚发生了什么。
如何让文本旋转 270 度并向上对齐?
【问题讨论】:
-
你在设置什么对齐方式?
-
一开始是Near,后来想换成Far和Center
-
当你变换图形时,整个“世界”都会被变换,所以近处与近处并不完全相同。不能设置在你想要的位置吗?
标签: c# rotation drawstring