2010-12-16 16 views
7

Potrzebuję obrócić tekst w etykiecie i wyrównać go w lewo, w prawo lub w środku. Do tej pory jestem w stanie wykonać rotację z tym kodem w metodzie onPaint pochodzącej z etykiety:Wyrównanie tekstu obróconego w języku 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(); 

I działa dobrze. Widzę tekst obrócony o 270 stopni.

Ale kiedy próbuję ustawić wyrównanie w stringFormat, to szaleje i nie mogę rozgryźć, co się dzieje.

Jak mogę obrócić tekst o 270 stopni i wyrównać do góry?

+0

Co ty wyrównanie ustawienia? – Aliostad

+0

Blisko początkowo, ale potem chcę zmienić na Far i Center –

+0

, kiedy zmienisz grafikę, cały "Świat" zostanie przekształcony, tak aby najbliższy nie był tak blisko. Nie możesz ustawić go w żądanej pozycji? – Aliostad

Odpowiedz

23

Jeśli ktoś szukał wskazówek, tutaj jest rozwiązanie dla obrotu 0, 90, 180, 270 i 360 stopni, gdzie działa StringAligment.

Jedna rzecz polegała na wybraniu właściwego punktu przesuwania punktu początkowego, a drugim na modyfikacji prostokąta wyświetlacza zgodnie z obrotem.

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

Umieszczenie tego kodu w wydarzeniu OnPaint na etykiecie spowoduje czterokrotne wyświetlenie tytułu obróconej postaci.

0

Rozszerzenie odpowiedź Adrian Serafina jeśli trzeba zwrócić na 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();