Rotated text align in C#

前端 未结 2 507
春和景丽
春和景丽 2020-12-29 13:27

I need to be able to rotate text in a label and align it to the left, right or center. So far I am able to do rotation with this code in the derived label\'s onPaint method:

2条回答
  •  别那么骄傲
    2020-12-29 14:25

    Extension of Adrian Serafin's answer if you need to draw at a non-0 X,Y:

    //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();
    

提交回复
热议问题