8

我需要能够旋转标签中的文本并将其向左、向右或居中对齐。到目前为止,我可以在派生标签的 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 度并将其向上对齐?

4

2 回答 2

25

如果有人在寻找提示,这里是 0、90、180、270 和 360 度旋转的解决方案,其中 StringAligment 起作用。

一件事是选择正确的点将原点移动到,第二件事是根据旋转修改显示矩形。

StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;

SizeF txt = e.Graphics.MeasureString(Text, this.Font);
SizeF sz = e.Graphics.VisibleClipBounds.Size;

//90 degrees
e.Graphics.TranslateTransform(sz.Width, 0);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format);
e.Graphics.ResetTransform();

//180 degrees
e.Graphics.TranslateTransform(sz.Width, sz.Height);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();

//270 degrees
e.Graphics.TranslateTransform(0, sz.Height);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format);
e.Graphics.ResetTransform();

//0 = 360 degrees
e.Graphics.TranslateTransform(0, 0);
e.Graphics.RotateTransform(0);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();

如果将此代码放在标签的 OnPaint 事件中,它将显示四次旋转表单的标题。

于 2010-12-16T14:35:23.550 回答
0

如果您需要在非 0 X,Y 处绘制,则扩展 Adrian Serafin 的答案:

//90 degrees
e.Graphics.TranslateTransform(sz.Width, 0);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(Text, this.Font, Brushes.Black,
  new RectangleF(sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format);
e.Graphics.ResetTransform();
//180 degrees
e.Graphics.TranslateTransform(sz.Width, sz.Height);
e.Graphics.RotateTransform(180 this.Font, Brushes.Black,
  new RectangleF(-sz.ToPointF().X, -sz.ToPointF().Y, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();
//270 degrees
e.Graphics.TranslateTransform(0, sz.Height);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(Text, this.Font, Brushes.Black,
  new RectangleF(-sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format);
//0 = 360 degrees
e.Graphics.TranslateTransform(0, 0);
e.Graphics.RotateTransform(0);
e.Graphics.DrawString(Text, this.Font, Brushes.Black,
  new RectangleF(sz.ToPointF().X, sz.ToPointF().Y, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();
于 2017-11-11T15:44:39.010 回答