2010-12-16 6 views
7

레이블에서 텍스트를 회전하고 왼쪽, 오른쪽 또는 가운데에 정렬 할 수 있어야합니다. 지금까지 파생 된 레이블의 onPaint 메서드에서이 코드로 회전 할 수 있습니다.C에서 회전 된 텍스트 정렬

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도 회전하여 위로 정렬 할 수 있습니까?

+0

당신이 설정하는 어떤 정렬? – Aliostad

+0

처음에는 근처에 있었지만 그래픽을 변형 시키면 멀리와 중심으로 바뀌려고합니다. –

+0

전체 "세계"가 변형되어 거의 비슷하지 않습니다. 당신이 원하는 위치에 그것을 설정할 수 있습니까? – Aliostad

답변

23

누군가가 팁을 찾고있는 경우, 여기서 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 이벤트에 넣으면 순환 양식의 제목이 네 번 표시됩니다. 아드리안 세라 핀의 대답의

0

확장이 아닌 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();