2010-12-16 59 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 2010-12-16 11:28:58

+0

近乎乍一看,但是當我改變圖形時,我想改變爲遠和中心 – 2010-12-16 11:35:30

+0

,整個「世界」得到改變,所以附近不是很近。你能不能把它放在你想要的位置? – Aliostad 2010-12-16 13:51:44

回答

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